tests: add loading of ctl classes
This commit is contained in:
parent
1dc60256bc
commit
570627ed14
|
@ -0,0 +1,6 @@
|
|||
import wcs.qommon.ctl
|
||||
|
||||
def test_loading():
|
||||
ctl = wcs.qommon.ctl.Ctl(cmd_prefixes=['wcs.ctl'])
|
||||
ctl.load_all_commands(ignore_errors=False)
|
||||
assert 'start' in ctl.get_commands().keys()
|
|
@ -92,7 +92,7 @@ class Ctl(object):
|
|||
callback=self.print_help,
|
||||
help=_("Display this help and exit"))
|
||||
|
||||
def load_all_commands(self):
|
||||
def load_all_commands(self, ignore_errors=True):
|
||||
for cmd_prefix in self.cmd_prefixes:
|
||||
if not cmd_prefix in sys.modules:
|
||||
__import__(cmd_prefix)
|
||||
|
@ -111,15 +111,19 @@ class Ctl(object):
|
|||
continue
|
||||
try:
|
||||
__import__('%s.%s' % (cmd_prefix, name))
|
||||
except ImportError:
|
||||
pass
|
||||
except ImportError as e:
|
||||
if not ignore_errors:
|
||||
raise e
|
||||
|
||||
def get_commands(self):
|
||||
return qommon._commands
|
||||
|
||||
def print_help(self, *args):
|
||||
self.parser.print_help()
|
||||
self.load_all_commands()
|
||||
print
|
||||
|
||||
commands = [(x.name, x.doc) for x in qommon._commands.values()]
|
||||
commands = [(x.name, x.doc) for x in self.get_commands()]
|
||||
commands.sort()
|
||||
print 'Available commands:'
|
||||
for name, description in commands:
|
||||
|
@ -132,7 +136,7 @@ class Ctl(object):
|
|||
self.parser.error('You must use a command')
|
||||
command, args = args[0], args[1:]
|
||||
|
||||
if command not in qommon._commands:
|
||||
if command not in self.get_commands():
|
||||
# load a module named like the command, this is the common case
|
||||
for cmd_prefix in self.cmd_prefixes:
|
||||
try:
|
||||
|
@ -140,12 +144,12 @@ class Ctl(object):
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
if command not in qommon._commands:
|
||||
if command not in self.get_commands():
|
||||
# if the command could not be loaded from a same-name module,
|
||||
# go over all modules
|
||||
self.load_all_commands()
|
||||
|
||||
command_class = qommon._commands[command]
|
||||
command_class = self.get_commands()[command]
|
||||
cmd = command_class()
|
||||
return cmd.run(args, options)
|
||||
|
||||
|
|
Loading…
Reference in New Issue