interfaces + handlers
Every core service is an interface with a default handler. Register your own handler to replace it.
Cement 3
Cement handles config, arguments, logging, output, plugins and caching, so you write the commands. Core has no dependencies and every piece can be replaced. It works for a single-file script and for a multi-tier application.
pip install cement
from cement import App, Controller, ex
class Base(Controller):
class Meta:
label = 'base'
@ex(
help='say hello',
arguments=[
(['-n', '--name'], {'default': 'world'}),
],
)
def hello(self):
self.app.log.info(f'hello {self.app.pargs.name}')
class MyApp(App):
class Meta:
label = 'myapp'
handlers = [Base]
with MyApp() as app:
app.run()
$ python myapp.py hello --name cement
INFO: hello cement
All of this is in core and works with no setup. Each piece is a handler behind an interface, so you can replace it with your own.
Every core service is an interface with a default handler. Register your own handler to replace it.
Sub-commands and nested controllers, written as classes and decorated methods.
Defaults, config files and environment variables merge into one config object.
Command-line arguments are parsed with argparse and merged with config.
Console and file logging, set up from config.
Commands return a dict and an output handler renders it: JSON, YAML, a table or a template.
Load plugins at runtime so users can add commands to your app.
A cache interface, with Redis and memcached handlers available as extensions.
Run your code at defined points in the app lifecycle, or define your own hooks.
Extensions add handlers for other backends. The ones that need third-party packages declare them as extras, so core stays dependency-free.
Install extras with pip install "cement[yaml,colorlog]"
class MyApp(App):
class Meta:
label = 'myapp'
extensions = ['yaml', 'colorlog', 'jinja2']
config_handler = 'yaml'
log_handler = 'colorlog'
output_handler = 'jinja2'
$ pip install "cement[cli]"$ cement generate project ./myapp$ cd myapp && pip install -e .
$ myapp --helpThe 20-minute tutorial in the developer guide builds a todo app with a database backend and a CRUD controller.