Changeset View
Changeset View
Standalone View
Standalone View
pykolab/cli/commands.py
Show All 39 Lines | for commands_path, dirnames, filenames in os.walk(commands_base_path): | ||||
for filename in filenames: | for filename in filenames: | ||||
if filename.startswith('cmd_') and filename.endswith('.py'): | if filename.startswith('cmd_') and filename.endswith('.py'): | ||||
module_name = filename.replace('.py','') | module_name = filename.replace('.py','') | ||||
cmd_name = module_name.replace('cmd_', '') | cmd_name = module_name.replace('cmd_', '') | ||||
#print "exec(\"from %s import __init__ as %s_register\"" % (module_name,cmd_name) | #print "exec(\"from %s import __init__ as %s_register\"" % (module_name,cmd_name) | ||||
try: | try: | ||||
exec("from %s import __init__ as %s_register" % (module_name,cmd_name)) | exec("from %s import __init__ as %s_register" % (module_name,cmd_name)) | ||||
except ImportError, errmsg: | except ImportError: | ||||
pass | pass | ||||
exec("%s_register()" % (cmd_name)) | exec("%s_register()" % (cmd_name)) | ||||
for dirname in dirnames: | for dirname in dirnames: | ||||
register_group(commands_path, dirname) | register_group(commands_path, dirname) | ||||
register('help', list_commands) | register('help', list_commands) | ||||
register('delete_user', not_yet_implemented, description="Not yet implemented") | register('delete_user', not_yet_implemented, description="Not yet implemented") | ||||
register('list_groups', not_yet_implemented, description="Not yet implemented") | register('list_groups', not_yet_implemented, description="Not yet implemented") | ||||
register('add_group', not_yet_implemented, description="Not yet implemented") | register('add_group', not_yet_implemented, description="Not yet implemented") | ||||
register('delete_group', not_yet_implemented, description="Not yet implemented") | register('delete_group', not_yet_implemented, description="Not yet implemented") | ||||
def list_commands(*args, **kw): | def list_commands(*args, **kw): | ||||
""" | """ | ||||
List commands | List commands | ||||
""" | """ | ||||
__commands = {} | __commands = {} | ||||
for command in commands.keys(): | for command in commands: | ||||
if isinstance(command, tuple): | if isinstance(command, tuple): | ||||
command_group, command = command | command_group, command = command | ||||
__commands[command_group] = { | __commands[command_group] = { | ||||
command: commands[(command_group,command)] | command: commands[(command_group,command)] | ||||
} | } | ||||
else: | else: | ||||
__commands[command] = commands[command] | __commands[command] = commands[command] | ||||
_commands = __commands.keys() | _commands = __commands.keys() | ||||
_commands.sort() | _commands.sort() | ||||
for _command in _commands: | for _command in _commands: | ||||
if __commands[_command].has_key('group'): | if 'group' in __commands[_command]: | ||||
continue | continue | ||||
if __commands[_command].has_key('function'): | if 'function' in __commands[_command]: | ||||
# This is a top-level command | # This is a top-level command | ||||
if not __commands[_command]['description'] == None: | if not __commands[_command]['description'] == None: | ||||
print "%-25s - %s" % (_command.replace('_','-'),__commands[_command]['description']) | print("%-25s - %s" % (_command.replace('_','-'),__commands[_command]['description'])) | ||||
else: | else: | ||||
print "%-25s" % (_command.replace('_','-')) | print("%-25s" % (_command.replace('_','-'))) | ||||
for _command in _commands: | for _command in _commands: | ||||
if not __commands[_command].has_key('function'): | if 'function' not in __commands[_command]: | ||||
# This is a nested command | # This is a nested command | ||||
print "\n" + _("Command Group: %s") % (_command) + "\n" | print("\n" + _("Command Group: %s") % (_command) + "\n") | ||||
___commands = __commands[_command].keys() | ___commands = __commands[_command].keys() | ||||
___commands.sort() | ___commands.sort() | ||||
for __command in ___commands: | for __command in ___commands: | ||||
if not __commands[_command][__command]['description'] == None: | if not __commands[_command][__command]['description'] == None: | ||||
print "%-4s%-21s - %s" % ('',__command.replace('_','-'),__commands[_command][__command]['description']) | print("%-4s%-21s - %s" % ('',__command.replace('_','-'),__commands[_command][__command]['description'])) | ||||
else: | else: | ||||
print "%-4s%-21s" % ('',__command.replace('_','-')) | print("%-4s%-21s" % ('',__command.replace('_','-'))) | ||||
def execute(cmd_name, *args, **kw): | def execute(cmd_name, *args, **kw): | ||||
if cmd_name == "": | if cmd_name == "": | ||||
execute("help") | execute("help") | ||||
sys.exit(0) | sys.exit(0) | ||||
if not commands.has_key(cmd_name): | if cmd_name not in commands: | ||||
log.error(_("No such command.")) | log.error(_("No such command.")) | ||||
sys.exit(1) | sys.exit(1) | ||||
if not commands[cmd_name].has_key('function') and \ | if 'function' not in commands[cmd_name] and \ | ||||
not commands[cmd_name].has_key('group'): | 'group' not in commands[cmd_name]: | ||||
log.error(_("No such command.")) | log.error(_("No such command.")) | ||||
sys.exit(1) | sys.exit(1) | ||||
if commands[cmd_name].has_key('group'): | if 'group' in commands[cmd_name]: | ||||
group = commands[cmd_name]['group'] | group = commands[cmd_name]['group'] | ||||
command_name = commands[cmd_name]['cmd_name'] | command_name = commands[cmd_name]['cmd_name'] | ||||
try: | try: | ||||
exec("from %s.cmd_%s import cli_options as %s_%s_cli_options" % (group,command_name,group,command_name)) | exec("from %s.cmd_%s import cli_options as %s_%s_cli_options" % (group,command_name,group,command_name)) | ||||
exec("%s_%s_cli_options()" % (group,command_name)) | exec("%s_%s_cli_options()" % (group,command_name)) | ||||
except ImportError, e: | except ImportError: | ||||
pass | pass | ||||
else: | else: | ||||
command_name = commands[cmd_name]['cmd_name'] | command_name = commands[cmd_name]['cmd_name'] | ||||
try: | try: | ||||
exec("from cmd_%s import cli_options as %s_cli_options" % (command_name,command_name)) | exec("from cmd_%s import cli_options as %s_cli_options" % (command_name,command_name)) | ||||
exec("%s_cli_options()" % (command_name)) | exec("%s_cli_options()" % (command_name)) | ||||
except ImportError, errmsg: | except ImportError: | ||||
pass | pass | ||||
conf.finalize_conf() | conf.finalize_conf() | ||||
commands[cmd_name]['function'](conf.cli_args, kw) | commands[cmd_name]['function'](conf.cli_args, kw) | ||||
def register_group(dirname, module): | def register_group(dirname, module): | ||||
commands_base_path = os.path.join(os.path.dirname(__file__), module) | commands_base_path = os.path.join(os.path.dirname(__file__), module) | ||||
Show All 15 Lines | def register(cmd_name, func, group=None, description=None, aliases=[]): | ||||
if not group == None: | if not group == None: | ||||
command = "%s_%s" % (group,cmd_name) | command = "%s_%s" % (group,cmd_name) | ||||
else: | else: | ||||
command = cmd_name | command = cmd_name | ||||
if isinstance(aliases, basestring): | if isinstance(aliases, basestring): | ||||
aliases = [aliases] | aliases = [aliases] | ||||
if commands.has_key(command): | if command in commands: | ||||
log.fatal(_("Command '%s' already registered") % (command)) | log.fatal(_("Command '%s' already registered") % (command)) | ||||
sys.exit(1) | sys.exit(1) | ||||
if callable(func): | if callable(func): | ||||
if group == None: | if group == None: | ||||
commands[cmd_name] = { | commands[cmd_name] = { | ||||
'cmd_name': cmd_name, | 'cmd_name': cmd_name, | ||||
'function': func, | 'function': func, | ||||
Show All 17 Lines | if callable(func): | ||||
'description': _("Alias for %s") % (cmd_name.replace('_','-')) | 'description': _("Alias for %s") % (cmd_name.replace('_','-')) | ||||
} | } | ||||
## | ## | ||||
## Commands not yet implemented | ## Commands not yet implemented | ||||
## | ## | ||||
def not_yet_implemented(*args, **kw): | def not_yet_implemented(*args, **kw): | ||||
print _("Not yet implemented") | print(_("Not yet implemented")) | ||||
sys.exit(1) | sys.exit(1) |