def _get_help(self, *funcs, **kwargs): show_types = kwargs.pop('show_types', True) assert not kwargs parser = defopt._create_parser(*funcs, show_types=show_types, strict_kwonly=False) return parser.format_help()
def show(): """显示当前可用的插件""" app = get_current_omx() min_width = max(len(f.__name__) for f in app.plugins) + 1 print("插件名称".ljust(min_width), "插件介绍") for plugin in app.plugins: parsed = _create_parser(plugin) # TODO: 性能提升 print( plugin.__name__.replace("_", "-").ljust(min_width), parsed.description)
def _get_help(self, funcs, flags): self.assertLessEqual({*flags}, {'d', 't', 'n'}) parser = defopt._create_parser( funcs, show_defaults='d' in flags, show_types='t' in flags, no_negated_flags='n' in flags, strict_kwonly=False) return parser.format_help()
def main(): funcs = ( TableSet.run, TableSet.dataset, TableSet.extract, TableSet.reshape, TableSet.cluster, TableSet.integrate, TableSet.link, TableSet.coltypes, TableSet.score, TableSet.triples, runapp, ) def getclasses(mod): for _, obj in inspect.getmembers(mod): if isinstance(obj, type): if obj.__module__.startswith(mod.__name__): yield obj elif isinstance(obj, types.ModuleType): if hasattr(obj, "__name__") and obj.__name__.startswith( mod.__name__): yield from getclasses(obj) def parse_tableset_arg(x): try: return TableSet.load(**config.build(config.parse(x), assets)) except: return TableSet.load(x, executor=assets.get("executor")) parser = defopt._create_parser( funcs, strict_kwonly=False, parsers={ Dict: config.parse, Any: lambda _: None, **{ cls: lambda x: config.build(config.parse(x), assets) for cls in getclasses(takco) }, HashBag: HashBag, TableSet: parse_tableset_arg, }, argparse_kwargs={"description": __doc__}, ) for action in parser._actions: if isinstance(action, argparse._SubParsersAction): for _, subparser in action.choices.items(): subparser.add_argument( "-C", action=SetConfig, metavar="CONFIG", help="Use global configuration (see docs)", ) subparser.add_argument( "-X", action=SetExecutor, metavar="EXECUTOR", help="Use executor (see docs)", ) subparser.add_argument( "-O", "--out", help="Write output to file(s)", ) subparser.add_argument("-v", "--info", action=SetVerbosity, help="Log general information") subparser.add_argument( "-vv", "--debug", action=SetVerbosity, help="Log debugging information", ) args = parser.parse_args(sys.argv[1:]) if not hasattr(args, '_func'): parser.error('too few arguments') # Output result as json (or newline-delimited json if generator) result = defopt._call_function(parser, args._func, args) if result: log.info(f"End result is a {type(result)}") try: if isinstance(result, TableSet): result = result.tables if isinstance(result, HashBag): out = args.out if hasattr(args, "out") and args.out else sys.stdout log.info(f"Writing {result} to {out}") for _ in result.dump(out): pass elif isinstance( result, (types.GeneratorType, map, filter)): # type: ignore for r in result: print(json.dumps(r)) else: print(json.dumps(result)) except IOError: log.debug(f"IOError") try: sys.stdout.close() except IOError: pass else: log.debug(f"No results")