text
stringlengths
0
828
be recognised by this lookup.
Please consider using the decorator ``@cmd`` to declare your
subcommands in classes for instance.
""""""
subcmds = []
for label in dir(obj.__class__):
if label.startswith(""_""):
continue
if isinstance(getattr(obj.__class__, label, False), property):
continue
rvalue = getattr(obj, label)
if not callable(rvalue) or not is_cmd(rvalue):
continue
if isinstance(obj, types.MethodType) and \
label in (""im_func"", ""im_self"", ""im_class""):
continue
## potential command
command_name = getattr(rvalue, ""command_name"",
label[:-1] if label.endswith(""_"") else
label)
subcmds.append((command_name, rvalue))
return OrderedDict(subcmds)"
218,"def get_module_resources(mod):
""""""Return probed sub module names from given module""""""
path = os.path.dirname(os.path.realpath(mod.__file__))
prefix = kf.basename(mod.__file__, ("".py"", "".pyc""))
if not os.path.exists(mod.__file__):
import pkg_resources
for resource_name in pkg_resources.resource_listdir(mod.__name__, ''):
if resource_name.startswith(""%s_"" % prefix) and resource_name.endswith("".py""):
module_name, _ext = os.path.splitext(kf.basename(resource_name))
yield module_name
for f in glob.glob(os.path.join(path, '%s_*.py' % prefix)):
module_name, _ext = os.path.splitext(kf.basename(f))
yield module_name"
219,"def get_mod_subcmds(mod):
""""""Fetch action in same directory in python module
python module loaded are of this form: '%s_*.py' % prefix
""""""
## Look in modules attributes
subcmds = get_obj_subcmds(mod)
path = os.path.dirname(os.path.realpath(mod.__file__))
if mod.__package__ is None:
sys.path.insert(0, os.path.dirname(path))
mod.__package__ = kf.basename(path)
for module_name in get_module_resources(mod):
try:
mod = importlib.import_module("".%s"" % module_name, mod.__package__)
except ImportError as e:
msg.warn(""%r could not be loaded: %s""
% (module_name, e.message))
continue
except IOError as e:
print(""%s"" % module_name)
raise
if hasattr(mod, ""Command"") and is_cmd(mod.Command):
obj = mod.Command
if obj.__doc__ is None:
msg.warn(""Missing doc string for command from ""
""module %s"" % module_name)
continue
if isinstance(obj, type):
obj = obj() ## instanciate it.
name = module_name.split(""_"", 1)[1]
if name in subcmds:
raise ValueError(
""Module command %r conflicts with already defined object ""
""command.""
% name)
subcmds[name] = obj
return subcmds"
220,"def get_help(obj, env, subcmds):
""""""Interpolate complete help doc of given object
Assumption that given object as a specific interface:
obj.__doc__ is the basic help object.
obj.get_actions_titles() returns the subcommand if any.
""""""
doc = txt.dedent(obj.__doc__ or """")
env = env.copy() ## get a local copy
doc = doc.strip()
if not re.search(r""^usage:\s*$"", doc, flags=re.IGNORECASE | re.MULTILINE):
doc += txt.dedent(""""""