Commit 12ecdeb7 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Complete new structure in one command (exprmtal)

parent 0d249b3e
......@@ -31,7 +31,7 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.command
#from kamaki.cli.new import command
from kamaki.cli.new import get_cmd_terms # , command
from kamaki.cli.commands import _command_init
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.argument import FlagArgument
......@@ -44,22 +44,15 @@ _commands = [
]
def command(cmd_tree_list, prefix='', descedants_depth=1):
"""Load a class as a command
spec_cmd0_cmd1 will be command spec cmd0
@cmd_tree_list is initialized in cmd_spec file and is the structure
where commands are loaded. Var name should be _commands
@param prefix if given, load only commands prefixed with prefix,
@param descedants_depth is the depth of the tree descedants of the
prefix command. It is used ONLY if prefix and if prefix is not
a terminal command
"""
print('Command Terms: ', get_cmd_terms())
def command(cmd_tree_list, prefix='', descedants_depth=1):
def wrap(cls):
cls_name = cls.__name__
spec = cls_name.split('_')[0]
cmd_tree = _commands[0] if spec == 'sample' else _commands[1]
cmd_tree = _commands[0] if cls_name.startswith('sample')\
else _commands[1]
if not cmd_tree:
return cls
......@@ -79,7 +72,7 @@ class _test_init(_command_init):
print(self.__class__)
for v in args:
print('\t\targ: %s' % v)
for k, v in kwargs:
for k, v in kwargs.items():
print('\t\tkwarg: %s: %s' % (k, v))
......
......@@ -47,13 +47,6 @@ _verbose = False
_colors = False
def command(*args, **kwargs):
"""Dummy command decorator - replace it with UI-specific decorator"""
def wrap(cls):
return cls
return wrap
def _construct_command_syntax(cls):
spec = getargspec(cls.main.im_func)
args = spec.args[1:]
......@@ -114,7 +107,7 @@ def _update_best_match(name_terms, prefix=[]):
return False
def _command_load_best_match(cmd_tree_list, prefix='', descedants_depth=1):
def command(cmd_tree_list, prefix='', descedants_depth=1):
"""Load a class as a command
spec_cmd0_cmd1 will be command spec cmd0
@cmd_tree_list is initialized in cmd_spec file and is the structure
......@@ -155,6 +148,12 @@ def _command_load_best_match(cmd_tree_list, prefix='', descedants_depth=1):
return cls
return wrap
def get_cmd_terms():
global command
return [term for term in command.func_defaults[0]\
if not term.startswith('-')]
cmd_spec_locations = [
'kamaki.cli.commands',
'kamaki.commands',
......@@ -264,20 +263,32 @@ def _print_error_message(cli_err):
print_list(cli_err.details)
def _get_best_match_from_cmd_tree(cmd_tree, unparsed):
matched = [term for term in unparsed if not term.startswith('-')]
while matched:
try:
return cmd_tree.get_command('_'.join(matched))
except KeyError:
matched = matched[:-1]
return None
def _exec_cmd(instance, cmd_args, help_method):
try:
return instance.main(*cmd_args)
except TypeError as err:
if err.args and err.args[0].startswith('main()'):
print(magenta('Syntax error'))
if instance.get_argument('verbose'):
if _debug:
raise err
if _verbose:
print(unicode(err))
help_method()
else:
raise
except CLIError as err:
if instance.get_argument('debug'):
raise
if _debug:
raise err
_print_error_message(err)
return 1
......@@ -290,8 +301,6 @@ def one_cmd(parser, unparsed, arguments):
exit(0)
global command
global _command_load
command = _command_load_best_match
def_params = list(command.func_defaults)
def_params[0] = unparsed
command.func_defaults = tuple(def_params)
......@@ -302,7 +311,15 @@ def one_cmd(parser, unparsed, arguments):
cmd_tree = _get_cmd_tree_from_spec(group, spec_module._commands)
cmd = cmd_tree.get_command('_'.join(_best_match))
if _best_match:
cmd = cmd_tree.get_command('_'.join(_best_match))
else:
cmd = _get_best_match_from_cmd_tree(cmd_tree, unparsed)
_best_match = cmd.path.split('_')
if cmd is None:
if _debug or _verbose:
print('Unexpected error: failed to load command')
exit(1)
_update_parser_help(parser, cmd)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment