Revision 017d37ce kamaki/cli/utils.py
b/kamaki/cli/utils.py | ||
---|---|---|
38 | 38 |
return val |
39 | 39 |
red = yellow = magenta = bold |
40 | 40 |
|
41 |
from .errors import CLIUnknownCommand, CLICmdSpecError, CLIError |
|
41 |
from .errors import CLIUnknownCommand, CLICmdIncompleteError, CLICmdSpecError, CLIError
|
|
42 | 42 |
|
43 | 43 |
""" |
44 | 44 |
def magenta(val): |
... | ... | |
88 | 88 |
def __init__(self): |
89 | 89 |
self._commands = {} |
90 | 90 |
|
91 |
def set_groups(self, groups): |
|
92 |
for grp in groups: |
|
93 |
self._commands[grp] = {} |
|
94 |
|
|
95 |
def get_groups(self): |
|
96 |
return self._commands.keys() |
|
97 |
|
|
91 | 98 |
def _get_commands_from_prefix(self, prefix): |
99 |
if len(prefix) == 0: |
|
100 |
return self._commands |
|
92 | 101 |
path = get_pathlist_from_prefix(prefix) |
93 | 102 |
next_list = self._commands |
94 | 103 |
try: |
... | ... | |
116 | 125 |
except ValueError: |
117 | 126 |
return ret |
118 | 127 |
|
119 |
def is_full_command(self, command):
|
|
128 |
def get_class(self, command):
|
|
120 | 129 |
""" Check if a command exists as a full/terminal command |
121 | 130 |
e.g. store_list is full, store is partial, stort is not existing |
122 | 131 |
@param command can either be a cmd1_cmd2_... str or a ['cmd1, cmd2, ...'] list |
... | ... | |
124 | 133 |
@raise CLIUnknownCommand if command is unknown to this tree |
125 | 134 |
""" |
126 | 135 |
next_level = self._get_commands_from_prefix(command) |
127 |
if '_class' in next_level.keys(): |
|
128 |
return True |
|
129 |
return False |
|
136 |
try: |
|
137 |
return next_level['_class'] |
|
138 |
except KeyError: |
|
139 |
raise CLICmdIncompleteError(details='Cmd %s is not a full cmd'%command) |
|
130 | 140 |
|
131 | 141 |
def add(self, command, cmd_class): |
132 | 142 |
"""Add a command_path-->cmd_class relation to the path """ |
... | ... | |
146 | 156 |
try: |
147 | 157 |
cmds = cmds[cmd] |
148 | 158 |
except KeyError: |
149 |
raise CLIUnknownCommand('set_description to cmd %s failed: cmd not found'%command) |
|
159 |
raise CLIUnknownCommand(details='set_description to cmd %s failed: cmd not found'%command)
|
|
150 | 160 |
cmds['_description'] = description |
161 |
|
|
151 | 162 |
def load_spec_package(self, spec_package): |
152 | 163 |
loaded = False |
153 | 164 |
for location in self.cmd_spec_locations: |
... | ... | |
159 | 170 |
except ImportError: |
160 | 171 |
pass |
161 | 172 |
if not loaded: |
162 |
raise CLICmdSpecError('Cmd Spec Package %s load failed'%spec_package) |
|
173 |
raise CLICmdSpecError(details='Cmd Spec Package %s load failed'%spec_package)
|
|
163 | 174 |
|
164 | 175 |
def load_spec(self, spec_package, spec): |
165 | 176 |
"""Load spec from a non nessecery loaded spec package""" |
... | ... | |
177 | 188 |
raise CLICmdSpecError('Cmd Spec %s load failed'%spec) |
178 | 189 |
|
179 | 190 |
def get_pathlist_from_prefix(prefix): |
180 |
return prefix if isinstance(prefix,list) else unicode(prefix).split('_') |
|
191 |
if isinstance(prefix, list): |
|
192 |
return prefix |
|
193 |
if len(prefix) == 0: |
|
194 |
return [] |
|
195 |
return unicode(prefix).split('_') |
|
181 | 196 |
|
182 | 197 |
def pretty_keys(d, delim='_', recurcive=False): |
183 | 198 |
"""Transform keys of a dict from the form |
Also available in: Unified diff