Revision 78496d42
b/kamaki/cli/commands/test_cli.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A.command |
33 | 33 |
|
34 |
from kamaki.cli import command |
|
34 |
#from kamaki.cli import command
|
|
35 | 35 |
from kamaki.cli.commands import _command_init |
36 | 36 |
from kamaki.cli.command_tree import CommandTree |
37 | 37 |
|
38 | 38 |
|
39 |
API_DESCRIPTION = dict(test='Test sample') |
|
39 |
#API_DESCRIPTION = dict(test='Test sample')
|
|
40 | 40 |
|
41 |
_commands = CommandTree('test', 'Test sample') |
|
41 |
_commands = [ |
|
42 |
CommandTree('sample', 'Sample commands for developing your own'), |
|
43 |
CommandTree('test', 'Test commands for testing clients') |
|
44 |
] |
|
42 | 45 |
|
43 | 46 |
|
44 | 47 |
class _test_init(_command_init): |
... | ... | |
46 | 49 |
print(self.__class__) |
47 | 50 |
|
48 | 51 |
|
49 |
@command() |
|
52 |
#@command() |
|
53 |
class sample_cmd0(_test_init): |
|
54 |
""" test cmd""" |
|
55 |
|
|
56 |
def main(self, mant): |
|
57 |
super(self.__class__, self).main() |
|
58 |
|
|
59 |
|
|
60 |
#@command() |
|
61 |
class sample_cmd_all(_test_init): |
|
62 |
"""test cmd all""" |
|
63 |
|
|
64 |
def main(self): |
|
65 |
super(self.__class__, self).main() |
|
66 |
|
|
67 |
|
|
68 |
#@command() |
|
69 |
class sample_cmd_some(_test_init): |
|
70 |
"""test_cmd_some""" |
|
71 |
|
|
72 |
def main(self, opt='lala'): |
|
73 |
super(self.__class__, self).main() |
|
74 |
|
|
75 |
|
|
50 | 76 |
class test_cmd0(_test_init): |
51 | 77 |
""" test cmd""" |
52 | 78 |
|
53 | 79 |
def main(self, mant): |
54 | 80 |
super(self.__class__, self).main() |
55 | 81 |
|
56 |
@command() |
|
82 |
|
|
57 | 83 |
class test_cmd_all(_test_init): |
58 | 84 |
"""test cmd all""" |
59 | 85 |
|
60 | 86 |
def main(self): |
61 | 87 |
super(self.__class__, self).main() |
62 | 88 |
|
63 |
@command() |
|
89 |
|
|
64 | 90 |
class test_cmd_some(_test_init): |
65 | 91 |
"""test_cmd_some""" |
66 | 92 |
|
b/kamaki/cli/new.py | ||
---|---|---|
34 | 34 |
from sys import argv |
35 | 35 |
from os.path import basename |
36 | 36 |
from kamaki.cli.argument import _arguments, parse_known_args, init_parser |
37 |
from kamaki.cli.history import History |
|
38 |
from kamaki.cli.utils import print_dict |
|
37 | 39 |
|
38 | 40 |
_help = False |
39 | 41 |
_debug = False |
40 | 42 |
_verbose = False |
41 | 43 |
_colors = False |
42 | 44 |
|
45 |
cmd_spec_locations = [ |
|
46 |
'kamaki.cli.commands', |
|
47 |
'kamaki.commands', |
|
48 |
'kamaki.cli', |
|
49 |
'kamaki', |
|
50 |
''] |
|
51 |
|
|
43 | 52 |
|
44 | 53 |
def _init_session(arguments): |
45 | 54 |
global _help |
... | ... | |
52 | 61 |
_colors = arguments['config'].get('global', 'colors') |
53 | 62 |
|
54 | 63 |
|
55 |
def one_cmd(): |
|
56 |
print('ONE COMMAND') |
|
64 |
def get_command_group(unparsed, arguments): |
|
65 |
groups = arguments['config'].get_groups() |
|
66 |
for grp_candidate in unparsed: |
|
67 |
if grp_candidate in groups: |
|
68 |
unparsed.remove(grp_candidate) |
|
69 |
return grp_candidate |
|
70 |
return None |
|
71 |
|
|
72 |
|
|
73 |
def _load_spec_module(spec, arguments, module): |
|
74 |
spec_name = arguments['config'].get(spec, 'cli') |
|
75 |
pkg = None |
|
76 |
if spec_name is None: |
|
77 |
spec_name = '%s_cli' % spec |
|
78 |
for location in cmd_spec_locations: |
|
79 |
location += spec_name if location == '' else '.%s' % spec_name |
|
80 |
try: |
|
81 |
pkg = __import__(location, fromlist=[module]) |
|
82 |
except ImportError: |
|
83 |
continue |
|
84 |
return pkg |
|
85 |
|
|
86 |
|
|
87 |
def _groups_help(arguments): |
|
88 |
global _debug |
|
89 |
descriptions = {} |
|
90 |
for spec in arguments['config'].get_groups(): |
|
91 |
pkg = _load_spec_module(spec, arguments, '_commands') |
|
92 |
if pkg: |
|
93 |
cmds = None |
|
94 |
try: |
|
95 |
cmds = getattr(pkg, '_commands') |
|
96 |
except AttributeError: |
|
97 |
if _debug: |
|
98 |
print('Warning: No description for %s' % spec) |
|
99 |
try: |
|
100 |
for cmd in cmds: |
|
101 |
descriptions[cmd.name] = cmd.description |
|
102 |
except TypeError: |
|
103 |
if _debug: |
|
104 |
print('Warning: no cmd specs in module %s' % spec) |
|
105 |
elif _debug: |
|
106 |
print('Warning: Loading of %s cmd spec failed' % spec) |
|
107 |
print('\nOptions:\n - - - -') |
|
108 |
print_dict(descriptions) |
|
109 |
|
|
110 |
|
|
111 |
def one_cmd(parser, unparsed, arguments): |
|
112 |
group = get_command_group(unparsed, arguments) |
|
113 |
if not group: |
|
114 |
parser.print_help() |
|
115 |
_groups_help(arguments) |
|
57 | 116 |
|
58 | 117 |
|
59 | 118 |
def interactive_shell(): |
... | ... | |
64 | 123 |
exe = basename(argv[0]) |
65 | 124 |
parser = init_parser(exe, _arguments) |
66 | 125 |
parsed, unparsed = parse_known_args(parser, _arguments) |
126 |
print('PARSED: %s\nUNPARSED: %s' % parsed, unparsed) |
|
67 | 127 |
|
68 | 128 |
if _arguments['version'].value: |
69 | 129 |
exit(0) |
... | ... | |
71 | 131 |
_init_session(_arguments) |
72 | 132 |
|
73 | 133 |
if unparsed: |
74 |
one_cmd() |
|
134 |
_history = History(_arguments['config'].get('history', 'file')) |
|
135 |
_history.add(' '.join([exe] + argv[1:])) |
|
136 |
one_cmd(parser, unparsed, _arguments) |
|
75 | 137 |
elif _help: |
76 | 138 |
parser.print_help() |
77 | 139 |
else: |
Also available in: Unified diff