本文整理汇总了Python中solo.commands.descriptor.SoloCommandDescriptor.obtain方法的典型用法代码示例。如果您正苦于以下问题:Python SoloCommandDescriptor.obtain方法的具体用法?Python SoloCommandDescriptor.obtain怎么用?Python SoloCommandDescriptor.obtain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类solo.commands.descriptor.SoloCommandDescriptor
的用法示例。
在下文中一共展示了SoloCommandDescriptor.obtain方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _show_help
# 需要导入模块: from solo.commands.descriptor import SoloCommandDescriptor [as 别名]
# 或者: from solo.commands.descriptor.SoloCommandDescriptor import obtain [as 别名]
def _show_help(self, *args):
# equo help <foo> <bar>
if len(self._args) > 1:
# syntax error
return -10
parser = argparse.ArgumentParser(
description=_("Entropy Command Line Client, Equo"),
epilog="http://www.argentlinux.io",
formatter_class=ColorfulFormatter)
# filtered out in solo.main. Will never get here
parser.add_argument(
"--color", action="store_true",
default=None, help=_("force colored output"))
descriptors = SoloCommandDescriptor.obtain()
descriptors.sort(key = lambda x: x.get_name())
group = parser.add_argument_group("command", "available commands")
for descriptor in descriptors:
if descriptor.get_class().HIDDEN:
continue
aliases = descriptor.get_class().ALIASES
aliases_str = ", ".join([teal(x) for x in aliases])
if aliases_str:
aliases_str = " [%s]" % (aliases_str,)
name = "%s%s" % (purple(descriptor.get_name()),
aliases_str)
desc = descriptor.get_description()
group.add_argument(name, help=darkgreen(desc), action="store_true")
parser.print_help()
if not self._args:
return 1
return 0
示例2: bashcomp
# 需要导入模块: from solo.commands.descriptor import SoloCommandDescriptor [as 别名]
# 或者: from solo.commands.descriptor.SoloCommandDescriptor import obtain [as 别名]
def bashcomp(self, last_arg):
"""
Overridden from SoloCommand
"""
import sys
descriptors = SoloCommandDescriptor.obtain()
descriptors.sort(key = lambda x: x.get_name())
outcome = []
for descriptor in descriptors:
name = descriptor.get_name()
if name == SoloHelp.NAME:
# do not add self
continue
outcome.append(name)
aliases = descriptor.get_class().ALIASES
outcome.extend(aliases)
def _startswith(string):
if last_arg is not None:
return string.startswith(last_arg)
return True
outcome = sorted(filter(_startswith, outcome))
sys.stdout.write(" ".join(outcome) + "\n")
sys.stdout.flush()
示例3: main
# 需要导入模块: from solo.commands.descriptor import SoloCommandDescriptor [as 别名]
# 或者: from solo.commands.descriptor.SoloCommandDescriptor import obtain [as 别名]
def main():
is_color = "--color" in sys.argv
if is_color:
sys.argv.remove("--color")
if not is_color and not is_stdout_a_tty():
nocolor()
warn_version_mismatch()
install_exception_handler()
descriptors = SoloCommandDescriptor.obtain()
args_map = {}
catch_all = None
for descriptor in descriptors:
klass = descriptor.get_class()
if klass.CATCH_ALL:
catch_all = klass
args_map[klass.NAME] = klass
for alias in klass.ALIASES:
args_map[alias] = klass
args = sys.argv[1:]
# convert args to unicode, to avoid passing
# raw string stuff down to entropy layers
def _to_unicode(arg):
try:
return const_convert_to_unicode(
arg, enctype=etpConst['conf_encoding'])
except UnicodeDecodeError:
print_error("invalid argument: %s" % (arg,))
raise SystemExit(1)
args = list(map(_to_unicode, args))
is_bashcomp = False
if "--bashcomp" in args:
is_bashcomp = True
args.remove("--bashcomp")
# the first eit, because bash does:
# argv -> equo --bashcomp equo repo
# and we need to drop --bashcomp and
# argv[2]
if args:
args.pop(0)
cmd = None
last_arg = None
if args:
last_arg = args[-1]
cmd = args[0]
args = args[1:]
cmd_class = args_map.get(cmd)
yell_class = args_map.get("yell")
if cmd_class is None:
cmd_class = catch_all
cmd_obj = cmd_class(args)
if is_bashcomp:
try:
cmd_obj.bashcomp(last_arg)
except NotImplementedError:
pass
raise SystemExit(0)
# non-root users not allowed
allowed = True
if os.getuid() != 0 and \
cmd_class is not catch_all and \
not cmd_class.ALLOW_UNPRIVILEGED and \
"--help" not in args:
cmd_class = catch_all
allowed = False
if allowed:
if not cmd_class.ALLOW_UNPRIVILEGED:
if entropy.tools.islive():
warn_live_system()
func, func_args = cmd_obj.parse()
exit_st = func(*func_args)
if exit_st == -10:
# syntax error, yell at user
func, func_args = yell_class(args).parse()
func(*func_args)
raise SystemExit(10)
else:
yell_class.reset()
raise SystemExit(exit_st)
else:
# execute this anyway so that commands are
# incomplete or invalid, the command error
# message will take precedence.
_func, _func_args = cmd_obj.parse()
print_error(_("superuser access required"))
raise SystemExit(1)