main.py¶
The main application is defined in main.py
:
1import sys
2
3from cliff.app import App
4from cliff.commandmanager import CommandManager
5
6
7class DemoApp(App):
8
9 def __init__(self):
10 super(DemoApp, self).__init__(
11 description='cliff demo app',
12 version='0.1',
13 command_manager=CommandManager('cliff.demo'),
14 deferred_help=True,
15 )
16
17 def initialize_app(self, argv):
18 self.LOG.debug('initialize_app')
19
20 def prepare_to_run_command(self, cmd):
21 self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
22
23 def clean_up(self, cmd, result, err):
24 self.LOG.debug('clean_up %s', cmd.__class__.__name__)
25 if err:
26 self.LOG.debug('got an error: %s', err)
27
28
29def main(argv=sys.argv[1:]):
30 myapp = DemoApp()
31 return myapp.run(argv)
32
33
34if __name__ == '__main__':
35 sys.exit(main(sys.argv[1:]))
The DemoApp
class inherits from App
and overrides
__init__()
to set the program description and version number. It also
passes a CommandManager
instance configured to look for plugins in the
cliff.demo
namespace.
The initialize_app()
method of DemoApp
will be invoked after the
main program arguments are parsed, but before any command processing is
performed and before the application enters interactive mode. This hook is
intended for opening connections to remote web services, databases, etc. using
arguments passed to the main application.
The prepare_to_run_command()
method of DemoApp
will be invoked
after a command is identified, but before the command is given its arguments
and run. This hook is intended for pre-command validation or setup that must be
repeated and cannot be handled by initialize_app()
.
The clean_up()
method of DemoApp
is invoked after a command
runs. If the command raised an exception, the exception object is passed to
clean_up()
. Otherwise the err
argument is None
.
The main()
function defined in main.py
is registered as a console
script entry point so that DemoApp
can be run from the command line
(see the discussion of setup.py
below).