Skip to content

Commit

Permalink
feat(app): subcmds can be callables
Browse files Browse the repository at this point in the history
  • Loading branch information
ankostis committed Jan 20, 2017
1 parent 4708d31 commit 5637976
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docs/source/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ Subcommands are specified as a dictionary on :class:`~traitlets.config.Applicati
instances, mapping subcommand names to 2-tuples containing:

1. The application class for the subcommand, or a string which can be imported
to give this.
to give this application class, or a callable with a single argument for
the parent application (i.e. to read configs from) returning a not yet *initialized*
application instance.
2. A short description of the subcommand for use in help output.

To see a list of the available aliases, flags, and subcommands for a configurable
Expand Down
27 changes: 16 additions & 11 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ def _flags_changed(self, change):
assert isinstance(value[1], six.string_types), "Bad flag: %r:%s" % (key, value)


# subcommands for launching other applications
# if this is not empty, this will be a parent Application
# this must be a dict of two-tuples,
# the first element being the application class/import string
# and the second being the help string for the subcommand
#: subcommands for launching other applications
#: if this is not empty, this will be a parent Application
#: this must be a dict of two-tuples,
#: the first element is the application class, or and import string
#: or a callable with a single argument for the parent returnng
#: instanciated application, and
#: the second being the help string for the subcommand
subcommands = Dict()
# parse_command_line will initialize a subapp, if requested
subapp = Instance('traitlets.config.application.Application', allow_none=True)
Expand Down Expand Up @@ -464,13 +466,16 @@ def initialize_subcommand(self, subc, argv=None):
"""Initialize a subcommand with argv."""
subapp,help = self.subcommands.get(subc)

if isinstance(subapp, six.string_types):
subapp = import_item(subapp)
if callable(subapp):
self.subapp = subapp(self)
else:
if isinstance(subapp, six.string_types):
subapp = import_item(subapp)

# clear existing instances
self.__class__.clear_instance()
# instantiate
self.subapp = subapp.instance(parent=self)
# clear existing instances
self.__class__.clear_instance()
# instantiate
self.subapp = subapp.instance(parent=self)
# and initialize subapp
self.subapp.initialize(argv)

Expand Down

0 comments on commit 5637976

Please sign in to comment.