本文整理汇总了Python中task.Register类的典型用法代码示例。如果您正苦于以下问题:Python Register类的具体用法?Python Register怎么用?Python Register使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Register类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_task
def parse_task(self, cmdline_args=None, main_task_cls=None):
parser = ErrorWrappedArgumentParser()
self.add_global_parameters(parser)
if main_task_cls:
self.add_task_parameters(parser, main_task_cls)
else:
orderedtasks = '{%s}' % ','.join(sorted(Register.get_reg().keys()))
subparsers = parser.add_subparsers(dest='command', metavar=orderedtasks)
for name, cls in Register.get_reg().iteritems():
subparser = subparsers.add_parser(name)
if cls == Register.AMBIGUOUS_CLASS:
continue
self.add_task_parameters(subparser, cls)
# Add global params here as well so that we can support both:
# test.py --global-param xyz Test --n 42
# test.py Test --n 42 --global-param xyz
self.add_global_parameters(subparser)
args = parser.parse_args(args=cmdline_args)
params = vars(args) # convert to a str -> str hash
if main_task_cls:
task_cls = main_task_cls
else:
task_cls = Register.get_task_cls(args.command)
# Notice that this is not side effect free because it might set global params
task = task_cls.from_str_params(params, Register.get_global_params())
return [task]
示例2: error
def error(self, message):
result = re.match("argument .+: invalid choice: '(\w+)'.+", message)
if result:
arg = result.group(1)
weightedTasks = [(self._editdistance(arg, task), task) for task in Register.get_reg().keys()]
orderedTasks = sorted(weightedTasks, key=lambda pair: pair[0])
candidates = [task for (dist, task) in orderedTasks if dist <= 5 and dist < len(task)]
displaystring = ""
if candidates:
displaystring = "No task %s. Did you mean:\n%s" % (arg, '\n'.join(candidates))
else:
displaystring = "No task %s." % arg
super(ErrorWrappedArgumentParser, self).error(displaystring)
else:
super(ErrorWrappedArgumentParser, self).error(message)
示例3: parse
def parse(self, cmdline_args=None, main_task_cls=None):
global_params = list(Register.get_global_params())
parser = PassThroughOptionParser()
tasks_str = '/'.join(sorted([name for name in Register.get_reg()]))
def add_task_option(p):
if main_task_cls:
p.add_option('--task', help='Task to run (' + tasks_str + ') [default: %default]', default=main_task_cls.task_family)
else:
p.add_option('--task', help='Task to run (%s)' % tasks_str)
def _add_parameter(parser, param_name, param):
description = [param_name]
if param.description:
description.append(param.description)
if param.has_default:
description.append(" [default: %s]" % (param.default,))
if param.is_list:
action = "append"
elif param.is_boolean:
action = "store_true"
else:
action = "store"
parser.add_option('--' + param_name.replace('_', '-'),
help=' '.join(description),
default=None,
action=action)
for param_name, param in global_params:
_add_parameter(parser, param_name, param)
add_task_option(parser)
options, args = parser.parse_args(args=cmdline_args)
task_cls_name = options.task
if self.__existing_optparse:
parser = self.__existing_optparse
else:
parser = optparse.OptionParser()
add_task_option(parser)
if task_cls_name not in Register.get_reg():
raise Exception('Error: %s is not a valid tasks (must be %s)' % (task_cls_name, tasks_str))
# Register all parameters as a big mess
task_cls = Register.get_reg()[task_cls_name]
if task_cls == Register.AMBIGUOUS_CLASS:
raise Exception('%s is ambiguous' % task_cls_name)
params = task_cls.get_nonglobal_params()
for param_name, param in global_params:
_add_parameter(parser, param_name, param)
for param_name, param in params:
_add_parameter(parser, param_name, param)
# Parse and run
options, args = parser.parse_args(args=cmdline_args)
params = {}
for k, v in vars(options).iteritems():
if k != 'task':
params[k] = v
task = task_cls.from_input(params, global_params)
return [task]
示例4: _add_global_parameters
def _add_global_parameters(parser):
for param_name, param in Register.get_global_params():
_add_parameter(parser, param_name, param)
示例5: add_global_parameters
def add_global_parameters(cls, parser):
for param_name, param in Register.get_global_params():
cls.add_parameter(parser, param_name, param)
示例6: add_task_option
def add_task_option(p):
if main_task_cls:
p.add_option('--task', help='Task to run (one of ' + Register.tasks_str() + ') [default: %default]', default=main_task_cls.task_family)
else:
p.add_option('--task', help='Task to run (one of %s)' % Register.tasks_str())
示例7: parse
def parse(self, cmdline_args=None, main_task_cls=None):
global_params = list(Register.get_global_params())
parser = PassThroughOptionParser()
def add_task_option(p):
if main_task_cls:
p.add_option('--task', help='Task to run (one of ' + Register.tasks_str() + ') [default: %default]', default=main_task_cls.task_family)
else:
p.add_option('--task', help='Task to run (one of %s)' % Register.tasks_str())
def _add_parameter(parser, param_name, param):
description = [param_name]
if param.description:
description.append(param.description)
if param.has_value:
description.append(" [default: %s]" % (param.value,))
if param.is_list:
action = "append"
elif param.is_boolean:
action = "store_true"
else:
action = "store"
parser.add_option('--' + param_name.replace('_', '-'),
help=' '.join(description),
default=None,
action=action)
for param_name, param in global_params:
_add_parameter(parser, param_name, param)
add_task_option(parser)
options, args = parser.parse_args(args=cmdline_args)
task_cls_name = options.task
if self.__existing_optparse:
parser = self.__existing_optparse
else:
parser = optparse.OptionParser()
add_task_option(parser)
task_cls = Register.get_task_cls(task_cls_name)
# Register all parameters as a big mess
params = task_cls.get_nonglobal_params()
for param_name, param in global_params:
_add_parameter(parser, param_name, param)
for param_name, param in params:
_add_parameter(parser, param_name, param)
# Parse and run
options, args = parser.parse_args(args=cmdline_args)
params = {}
for k, v in vars(options).iteritems():
if k != 'task':
params[k] = v
task = task_cls.from_str_params(params, global_params)
return [task]