Changeset View
Changeset View
Standalone View
Standalone View
pykolab/setup/components.py
Show First 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | |||||
def list_components(*args, **kw): | def list_components(*args, **kw): | ||||
""" | """ | ||||
List components | List components | ||||
""" | """ | ||||
__components = {} | __components = {} | ||||
for component in components.keys(): | for component in components: | ||||
if isinstance(component, tuple): | if isinstance(component, tuple): | ||||
component_group, component = component | component_group, component = component | ||||
__components[component_group] = { | __components[component_group] = { | ||||
component: components[(component_group,component)] | component: components[(component_group,component)] | ||||
} | } | ||||
else: | else: | ||||
__components[component] = components[component] | __components[component] = components[component] | ||||
_components = __components.keys() | _components = __components.keys() | ||||
_components.sort() | _components.sort() | ||||
for _component in _components: | for _component in _components: | ||||
if __components[_component].has_key('function'): | if 'function' in __components[_component]: | ||||
# This is a top-level component | # This is a top-level component | ||||
if not __components[_component]['description'] == None: | if not __components[_component]['description'] == None: | ||||
print "%-25s - %s" % (_component.replace('_','-'),__components[_component]['description']) | print("%-25s - %s" % (_component.replace('_','-'),__components[_component]['description'])) | ||||
else: | else: | ||||
print "%-25s" % (_component.replace('_','-')) | print("%-25s" % (_component.replace('_','-'))) | ||||
for _component in _components: | for _component in _components: | ||||
if not __components[_component].has_key('function'): | if 'function' not in __components[_component]: | ||||
# This is a nested component | # This is a nested component | ||||
print "\n" + _("Command Group: %s") % (_component) + "\n" | print("\n" + _("Command Group: %s") % (_component) + "\n") | ||||
___components = __components[_component].keys() | ___components = __components[_component].keys() | ||||
___components.sort() | ___components.sort() | ||||
for __component in ___components: | for __component in ___components: | ||||
if not __components[_component][__component]['description'] == None: | if not __components[_component][__component]['description'] == None: | ||||
print "%-4s%-21s - %s" % ('',__component.replace('_','-'),__components[_component][__component]['description']) | print("%-4s%-21s - %s" % ('',__component.replace('_','-'),__components[_component][__component]['description'])) | ||||
else: | else: | ||||
print "%-4s%-21s" % ('',__component.replace('_','-')) | print("%-4s%-21s" % ('',__component.replace('_','-'))) | ||||
def _list_components(*args, **kw): | def _list_components(*args, **kw): | ||||
""" | """ | ||||
List components and return API compatible, parseable lists and | List components and return API compatible, parseable lists and | ||||
dictionaries. | dictionaries. | ||||
""" | """ | ||||
__components = {} | __components = {} | ||||
for component in components.keys(): | for component in components: | ||||
if isinstance(component, tuple): | if isinstance(component, tuple): | ||||
component_group, component = component | component_group, component = component | ||||
__components[component_group] = { | __components[component_group] = { | ||||
component: components[(component_group,component)] | component: components[(component_group,component)] | ||||
} | } | ||||
else: | else: | ||||
__components[component] = components[component] | __components[component] = components[component] | ||||
_components = __components.keys() | _components = __components.keys() | ||||
_components.sort() | _components.sort() | ||||
return _components | return _components | ||||
def cli_options_from_component(component_name, *args, **kw): | def cli_options_from_component(component_name, *args, **kw): | ||||
global components_included_in_cli | global components_included_in_cli | ||||
if component_name in components_included_in_cli: | if component_name in components_included_in_cli: | ||||
return | return | ||||
if components[component_name].has_key('group'): | if 'group' in components[component_name]: | ||||
group = components[component_name]['group'] | group = components[component_name]['group'] | ||||
component_name = components[component_name]['component_name'] | component_name = components[component_name]['component_name'] | ||||
try: | try: | ||||
exec("from %s.setup_%s import cli_options as %s_%s_cli_options" % (group,component_name,group,component_name)) | exec("from %s.setup_%s import cli_options as %s_%s_cli_options" % (group,component_name,group,component_name)) | ||||
exec("%s_%s_cli_options()" % (group,component_name)) | exec("%s_%s_cli_options()" % (group,component_name)) | ||||
except ImportError, e: | except ImportError: | ||||
pass | pass | ||||
else: | else: | ||||
try: | try: | ||||
exec("from setup_%s import cli_options as %s_cli_options" % (component_name,component_name)) | exec("from setup_%s import cli_options as %s_cli_options" % (component_name,component_name)) | ||||
exec("%s_cli_options()" % (component_name)) | exec("%s_cli_options()" % (component_name)) | ||||
except ImportError, e: | except ImportError: | ||||
pass | pass | ||||
components_included_in_cli.append(component_name) | components_included_in_cli.append(component_name) | ||||
def execute(component_name, *args, **kw): | def execute(component_name, *args, **kw): | ||||
if component_name == '': | if component_name == '': | ||||
log.debug( | log.debug( | ||||
_("No component selected, continuing for all components"), | _("No component selected, continuing for all components"), | ||||
level=8 | level=8 | ||||
) | ) | ||||
while 1: | while 1: | ||||
for component in _list_components(): | for component in _list_components(): | ||||
execute_this = True | execute_this = True | ||||
if component in executed_components: | if component in executed_components: | ||||
execute_this = False | execute_this = False | ||||
if component == "help": | if component == "help": | ||||
execute_this = False | execute_this = False | ||||
if execute_this: | if execute_this: | ||||
if components[component].has_key('after'): | if 'after' in components[component]: | ||||
for _component in components[component]['after']: | for _component in components[component]['after']: | ||||
if not _component in executed_components: | if not _component in executed_components: | ||||
execute_this = False | execute_this = False | ||||
if execute_this: | if execute_this: | ||||
execute(component) | execute(component) | ||||
executed_components.append(component) | executed_components.append(component) | ||||
executed_all = True | executed_all = True | ||||
for component in _list_components(): | for component in _list_components(): | ||||
if not component in executed_components and not component == "help": | if not component in executed_components and not component == "help": | ||||
executed_all = False | executed_all = False | ||||
if executed_all: | if executed_all: | ||||
break | break | ||||
return | return | ||||
else: | else: | ||||
for component in _list_components(): | for component in _list_components(): | ||||
cli_options_from_component(component) | cli_options_from_component(component) | ||||
if not components.has_key(component_name): | if component_name not in components: | ||||
log.error(_("No such component.")) | log.error(_("No such component.")) | ||||
sys.exit(1) | sys.exit(1) | ||||
if not components[component_name].has_key('function') and \ | if 'function' not in components[component_name] and \ | ||||
not components[component_name].has_key('group'): | 'group' not in components[component_name]: | ||||
log.error(_("No such component.")) | log.error(_("No such component.")) | ||||
sys.exit(1) | sys.exit(1) | ||||
conf.finalize_conf() | conf.finalize_conf() | ||||
if len(conf.cli_args) >= 1: | if len(conf.cli_args) >= 1: | ||||
_component_name = conf.cli_args.pop(0) | _component_name = conf.cli_args.pop(0) | ||||
else: | else: | ||||
Show All 22 Lines | def register(component_name, func, group=None, description=None, aliases=[], after=[], before=[]): | ||||
if not group == None: | if not group == None: | ||||
component = "%s_%s" % (group,component_name) | component = "%s_%s" % (group,component_name) | ||||
else: | else: | ||||
component = component_name | component = component_name | ||||
if isinstance(aliases, basestring): | if isinstance(aliases, basestring): | ||||
aliases = [aliases] | aliases = [aliases] | ||||
if components.has_key(component): | if component in components: | ||||
log.fatal(_("Command '%s' already registered") % (component)) | log.fatal(_("Command '%s' already registered") % (component)) | ||||
sys.exit(1) | sys.exit(1) | ||||
if callable(func): | if callable(func): | ||||
if group == None: | if group == None: | ||||
components[component_name] = { | components[component_name] = { | ||||
'function': func, | 'function': func, | ||||
'description': description, | 'description': description, | ||||
Show All 18 Lines | if callable(func): | ||||
'description': _("Alias for %s") % (component_name) | 'description': _("Alias for %s") % (component_name) | ||||
} | } | ||||
## | ## | ||||
## 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) |