Permalink
Browse files

Add namespacing or implicit sub-manager support to commands.

  • Loading branch information...
mehcode committed Oct 5, 2013
1 parent f7660d1 commit be512d3f839f2006cb89a66cf5d5e02c79185c5e
Showing with 47 additions and 2 deletions.
  1. +14 −2 flask_script/__init__.py
  2. +33 −0 tests.py
View
@@ -178,12 +178,13 @@ def get_options(self):
return self._options
- def add_command(self, *args):
+ def add_command(self, *args, **kwargs):
"""
Adds command to registry.
:param command: Command instance
:param name: Name of the command (optional)
+ :param namespace: Namespace of the command (optional; pass as kwarg)
"""
if len(args) == 1:
@@ -204,7 +205,18 @@ def add_command(self, *args):
if isinstance(command, Manager):
command.parent = self
- self._commands[name] = command
+ namespace = kwargs.get('namespace')
+ if not namespace:
+ namespace = getattr(command, 'namespace', None)
+
+ if namespace:
+ if namespace not in self._commands:
+ self.add_command(namespace, Manager())
+
+ self._commands[namespace]._commands[name] = command
+
+ else:
+ self._commands[name] = command
def command(self, func):
"""
View
@@ -82,6 +82,15 @@ def run(self):
print('OK')
+class NamespacedCommand(Command):
+ 'namespaced command'
+
+ namespace = 'ns'
+
+ def run(self):
+ print('OK')
+
+
class CommandWithArgs(Command):
'command with args'
@@ -195,6 +204,30 @@ def test_add_explicit_named_command(self):
assert name in manager._commands
assert isinstance(manager._commands[name], ExplicitNamedCommand)
+ def test_add_namespaced_command(self):
+
+ manager = Manager(self.app)
+ manager.add_command('one', NamespacedCommand())
+ manager.add_command('two', NamespacedCommand())
+
+ assert 'ns' in manager._commands
+ assert isinstance(manager._commands['ns'], Manager)
+ ns = manager._commands['ns']
+ assert isinstance(ns._commands['one'], NamespacedCommand)
+ assert isinstance(ns._commands['two'], NamespacedCommand)
+
+ def test_add_namespaced_simple_command(self):
+
+ manager = Manager(self.app)
+ manager.add_command('hello', SimpleCommand(), namespace='ns')
+ manager.add_command('world', SimpleCommand(), namespace='ns')
+
+ assert 'ns' in manager._commands
+ assert isinstance(manager._commands['ns'], Manager)
+ ns = manager._commands['ns']
+ assert isinstance(ns._commands['hello'], SimpleCommand)
+ assert isinstance(ns._commands['world'], SimpleCommand)
+
def test_simple_command_decorator(self, capsys):
manager = Manager(self.app)

0 comments on commit be512d3

Please sign in to comment.