Thoughts on how an integration of a CLI (command line interface) toolkit for the Pyramid web framework might look like and could be integrated into the configuration mechanisms of Pyramid itself.

This is a follow up to my thoughts on using Click for a Pyramid CLI from a few days ago.

Goal

Easy to create CLI interfaces for Pyramid, should be a plugin which can be added via the include mechanisms of Pyramid.

Leverage as much as possible the existing features of Click, only provide the integration which helps with the following issues:

  • Automatically bootstrap the Pyramid framework based on an INI file as configuration.
  • Provide an API to register subcommands.

Draft Snippets

Integration

Two parts are needed: Inclusion for the configurator and a registration to generate a console script.

Including the package

The application will have to include the package, so that we can register our custom directives.

from pyramid.config import Configurator


def my_app(global_config, **settings):

    config = Configurator(settings=settings)
    config.include('johbo_pyramid_cli')

    # ...

Registration of the console script

Register via an entry in your setup.py as a console script:

setup(
    # ...
    entry_points="""
        [console_scripts]
        pcli = johbo_pyramid_cli:main
    """,
)

Registering a subcommand

Assuming that we will bootstrap the framework anyway, registering a subcommand can be done via the regular configuration mechanism.

from . import cli


def includeme(config):

    config.add_cli_subcommand(cli.my_command)

Hooks - Events

There should be two events fired:

  • BeforeCliCommand
  • AfterCliCommand

This way it is possible to run extra code around each subcommand invocation.

Providing the INI file

It could then be called on the command line like this:

pcli production.ini my-command --option=value

Conclusions

I am still not sure if a CLI is still the right thing to have, and especially using a tool like prequest seems to provide a fallback. On the other side, if there is something to call on the command like, I very much like to have a nice interface.

After implementing a few CLI scripts based on the ideas from last week I found that the integration into the framework itself could be nicer. Using the existing infrastructure based on pyramid.config.Configurator would be nice, since this is already part of every Pyramid application and no new explanations would be needed.


Comments

comments powered by Disqus