Revision f724cd35 kamaki/cli/__init__.py
b/kamaki/cli/__init__.py | ||
---|---|---|
208 | 208 |
remove_colors() |
209 | 209 |
_silent = arguments['silent'].value |
210 | 210 |
_setup_logging(_silent, _debug, _verbose, _include) |
211 |
global_url = arguments['config'].get('global', 'url') |
|
212 |
global_token = arguments['config'].get('global', 'token') |
|
213 |
from kamaki.clients.astakos import AstakosClient as AuthCachedClient |
|
214 |
return AuthCachedClient(global_url, global_token) |
|
211 | 215 |
|
212 | 216 |
|
213 | 217 |
def _load_spec_module(spec, arguments, module): |
214 |
spec_name = arguments['config'].get(spec, 'cli')
|
|
215 |
if spec_name is None:
|
|
218 |
#spec_name = arguments['config'].get('cli', spec)
|
|
219 |
if not spec:
|
|
216 | 220 |
return None |
217 | 221 |
pkg = None |
218 | 222 |
for location in cmd_spec_locations: |
219 |
location += spec_name if location == '' else '.%s' % spec_name
|
|
223 |
location += spec if location == '' else '.%s' % spec
|
|
220 | 224 |
try: |
221 | 225 |
pkg = __import__(location, fromlist=[module]) |
222 | 226 |
return pkg |
223 |
except ImportError: |
|
227 |
except ImportError as ie:
|
|
224 | 228 |
continue |
229 |
if not pkg: |
|
230 |
kloger.debug('Loading cmd grp %s failed: %s' % (spec, ie)) |
|
225 | 231 |
return pkg |
226 | 232 |
|
227 | 233 |
|
... | ... | |
229 | 235 |
global _debug |
230 | 236 |
global kloger |
231 | 237 |
descriptions = {} |
232 |
for spec in arguments['config'].get_groups():
|
|
238 |
for cmd_group, spec in arguments['config'].get_cli_specs():
|
|
233 | 239 |
pkg = _load_spec_module(spec, arguments, '_commands') |
234 | 240 |
if pkg: |
235 |
cmds = None
|
|
236 |
try: |
|
237 |
_cnf = arguments['config']
|
|
238 |
cmds = [cmd for cmd in getattr(pkg, '_commands') if _cnf.get(
|
|
239 |
cmd.name, 'cli')]
|
|
240 |
except AttributeError: |
|
241 |
if _debug:
|
|
242 |
kloger.warning('No description for %s' % spec)
|
|
241 |
cmds = getattr(pkg, '_commands')
|
|
242 |
#try:
|
|
243 |
# #_cnf = arguments['config']
|
|
244 |
# #cmds = [cmd for cmd in getattr(pkg, '_commands') if _cnf.get(
|
|
245 |
# # 'cli', cmd.name)]
|
|
246 |
#except AttributeError:
|
|
247 |
# if _debug:
|
|
248 |
# kloger.warning('No description for %s' % cmd_group)
|
|
243 | 249 |
try: |
244 | 250 |
for cmd in cmds: |
245 | 251 |
descriptions[cmd.name] = cmd.description |
246 | 252 |
except TypeError: |
247 | 253 |
if _debug: |
248 |
kloger.warning('no cmd specs in module %s' % spec) |
|
254 |
kloger.warning( |
|
255 |
'No cmd description for module %s' % cmd_group) |
|
249 | 256 |
elif _debug: |
250 |
kloger.warning('Loading of %s cmd spec failed' % spec)
|
|
257 |
kloger.warning('Loading of %s cmd spec failed' % cmd_group)
|
|
251 | 258 |
print('\nOptions:\n - - - -') |
252 | 259 |
print_dict(descriptions) |
253 | 260 |
|
254 | 261 |
|
255 | 262 |
def _load_all_commands(cmd_tree, arguments): |
256 | 263 |
_cnf = arguments['config'] |
257 |
specs = [spec for spec in _cnf.get_groups() if _cnf.get(spec, 'cli')] |
|
258 |
for spec in specs:
|
|
264 |
#specs = [spec for spec in _cnf.get_groups() if _cnf.get(spec, 'cli')]
|
|
265 |
for cmd_group, spec in _cnf.get_cli_specs():
|
|
259 | 266 |
try: |
260 | 267 |
spec_module = _load_spec_module(spec, arguments, '_commands') |
261 | 268 |
spec_commands = getattr(spec_module, '_commands') |
262 | 269 |
except AttributeError: |
263 | 270 |
if _debug: |
264 | 271 |
global kloger |
265 |
kloger.warning('No valid description for %s' % spec)
|
|
272 |
kloger.warning('No valid description for %s' % cmd_group)
|
|
266 | 273 |
continue |
267 | 274 |
for spec_tree in spec_commands: |
268 |
if spec_tree.name == spec:
|
|
275 |
if spec_tree.name == cmd_group:
|
|
269 | 276 |
cmd_tree.add_tree(spec_tree) |
270 | 277 |
break |
271 | 278 |
|
... | ... | |
314 | 321 |
errmsg = red(errmsg) |
315 | 322 |
stdout.write(errmsg) |
316 | 323 |
for errmsg in cli_err.details: |
317 |
print('| %s' % errmsg) |
|
324 |
print('| %s' % errmsg)
|
|
318 | 325 |
|
319 | 326 |
|
320 | 327 |
def exec_cmd(instance, cmd_args, help_method): |
... | ... | |
358 | 365 |
|
359 | 366 |
# CLI Choice: |
360 | 367 |
|
361 |
def run_one_cmd(exe_string, parser): |
|
368 |
def run_one_cmd(exe_string, parser, auth_base):
|
|
362 | 369 |
global _history |
363 | 370 |
_history = History( |
364 | 371 |
parser.arguments['config'].get('history', 'file')) |
365 | 372 |
_history.add(' '.join([exe_string] + argv[1:])) |
366 | 373 |
from kamaki.cli import one_command |
367 |
one_command.run(parser, _help) |
|
374 |
one_command.run(auth_base, parser, _help)
|
|
368 | 375 |
|
369 | 376 |
|
370 |
def run_shell(exe_string, parser): |
|
377 |
def run_shell(exe_string, parser, auth_base):
|
|
371 | 378 |
from command_shell import _init_shell |
372 | 379 |
shell = _init_shell(exe_string, parser) |
373 | 380 |
_load_all_commands(shell.cmd_tree, parser.arguments) |
374 |
shell.run(parser) |
|
381 |
shell.run(auth_base, parser)
|
|
375 | 382 |
|
376 | 383 |
|
377 | 384 |
def main(): |
... | ... | |
389 | 396 |
filelog = logger.add_file_logger(__name__.split('.')[0]) |
390 | 397 |
filelog.info('* Initial Call *\n%s\n- - -' % ' '.join(argv)) |
391 | 398 |
|
392 |
_init_session(parser.arguments) |
|
399 |
auth_base = _init_session(parser.arguments)
|
|
393 | 400 |
|
394 | 401 |
from kamaki.cli.utils import suggest_missing |
395 | 402 |
suggest_missing() |
396 | 403 |
|
397 | 404 |
if parser.unparsed: |
398 |
run_one_cmd(exe, parser) |
|
405 |
run_one_cmd(exe, parser, auth_base)
|
|
399 | 406 |
elif _help: |
400 | 407 |
parser.parser.print_help() |
401 | 408 |
_groups_help(parser.arguments) |
402 | 409 |
else: |
403 |
run_shell(exe, parser) |
|
410 |
run_shell(exe, parser, auth_base)
|
|
404 | 411 |
except CLIError as err: |
405 | 412 |
print_error_message(err) |
406 | 413 |
if _debug: |
Also available in: Unified diff