def format_preprocessing_list(self): buffer = io.StringIO() cmd = Command.discover(PreprocessCommand) print(cmd.usage(), file=buffer) print("", file=buffer) print("Configuration:", file=buffer) print(format_info_text(_PREPROCESS_HELP.strip(), indent=2), file=buffer) print("", file=buffer) ops = {} for k in self.plugins.keys('vergeml.operation'): plugin = self.plugins.get('vergeml.operation', k) op = Operation.discover(plugin) topic = op.topic or "general" descr = op.descr ops.setdefault(topic, []) ops[topic].append((k, descr)) for k, v in sorted(ops.items()): topic = k.capitalize() print(f"{topic} Operations:", file=buffer) print(format_info_text(_get_table(v), indent=2), file=buffer) print("", file=buffer) return buffer.getvalue().strip()
def format_model(self, name): plugin = self.plugins.get("vergeml.model", name) model = Model.discover(plugin) buffer = io.StringIO() if model.descr: print(format_info_text(model.descr), file=buffer) if model.long_descr: if model.descr: print("", file=buffer) print(format_info_text(model.long_descr), file=buffer) return buffer.getvalue()
def format_glossary(self): buffer = io.StringIO() for k, v in sorted(glossary.LONG_DESCR.items()): print(f"{k}:", file=buffer) print(format_info_text(v, indent=2), file=buffer) print("", file=buffer) return buffer.getvalue().strip()
def format_source_or_operation(self, name, group, what): op = what.discover(self.plugins.get(group, name)) buffer = io.StringIO() descr = op.long_descr or op.descr if descr: print(descr, file=buffer) else: print(name, file=buffer) print("", file=buffer) options = op.options if options: print("Options:", file=buffer) for opt in options: print("", file=buffer) tp_descr = opt.human_type() if tp_descr: print(f"{opt.name}: {tp_descr}", file=buffer) else: print(f"{opt.name}", file=buffer) if opt.descr: print(format_info_text(opt.descr, indent=2), file=buffer) return buffer.getvalue().strip()
def format_topics(self, env): buffer = io.StringIO() print("General Help:", file=buffer) for topic, descr in _GENERAL_HELP: print(" {:<16} {}".format(topic, descr), file=buffer) print("", file=buffer) print("Commands:", file=buffer) for cmd_name in self.plugins.keys('vergeml.cmd'): descr = Command.discover(self.plugins.get('vergeml.cmd', cmd_name)).descr print(" {:<16} {}".format(cmd_name, descr), file=buffer) print("", file=buffer) if env and env.model: print("Model Commands:", file=buffer) for fn in Command.find_functions(env.model): cmd = Command.discover(fn) print(" {:<16} {}".format(cmd.name, cmd.descr), file=buffer) print("", file=buffer) print("Configuration:", file=buffer) for topic, descr in _CONFIGURATION_HELP: print(" {:<16} {}".format(topic, descr), file=buffer) print("", file=buffer) inputs = [] for k in self.plugins.keys('vergeml.io'): plugin = self.plugins.get('vergeml.io', k) source = Source.discover(plugin) inputs.append((k, source.descr)) print("Data Input:", file=buffer) print(_get_table(inputs, IND=2, colon=False), file=buffer) print("", file=buffer) ops = {} for k in self.plugins.keys('vergeml.operation'): plugin = self.plugins.get('vergeml.operation', k) op = Operation.discover(plugin) topic = op.topic or "general" descr = op.descr ops.setdefault(topic, []) ops[topic].append((k, descr)) for k, v in sorted(ops.items()): topic = k.capitalize() print(f"{topic} Operations:", file=buffer) print(format_info_text(_get_table(v), indent=2), file=buffer) print("", file=buffer) models = [] for name in self.plugins.keys("vergeml.model"): plugin = self.plugins.get('vergeml.model', name) model = Model.discover(plugin) models.append((name, model.descr)) if models: print(_get_table(models), file=buffer) print("Glossary:", file=buffer) items = ", ".join(glossary.LONG_DESCR.keys()) print(format_info_text(items, indent=2), file=buffer) return buffer.getvalue().strip()
def get_help(self, env=None, topic="", short=False): if topic: model_commands = {} if env and env.model: for fn in Command.find_functions(env.model): cmd = Command.discover(fn) model_commands[cmd.name] = cmd if topic == "-a": return self.format_topics(env) # show VergeML options elif topic == "options": return self.format_options() # display the glossary elif topic == "glossary": return self.format_glossary() # show available models elif topic == "models": return _with_header(self.format_models(), help="models", topic=topic) # explain the data.input section elif topic == "input": return _with_header(self.format_input_list(), help="configuration", topic=topic) # explain the data.cache section elif topic == "cache": return _with_header(format_info_text(_CACHE_HELP), help="configuration", topic=topic) # explain the data.output section elif topic == "output": return _with_header(format_info_text(_OUTPUT_HELP), help="configuration", topic=topic) # explain preprocessing elif topic in ("preprocess", "preprocessing"): return _with_header(self.format_preprocessing_list(), help="configuration", topic=topic) # explain the data section elif topic == "data": return _with_header(format_info_text(_DATA_HELP.strip()), help="configuration", topic=topic) # explain the device section elif topic == "device": return _with_header(format_info_text(_DEVICE_HELP.strip()), help="configuration", topic=topic) # show a random robot elif topic == "random robot": robot = ascii_robot(datetime.datetime.now(), random_robot_name(datetime.datetime.now())) return f"\n{robot}\n" elif ":" in topic and topic.split( ":", 1)[0] in self.plugins.keys('vergeml.cmd'): command, subcommand = topic.split(":") cmd = Command.discover(self.plugins.get( 'vergeml.cmd', command)) subcommand_option = next( filter(lambda o: bool(o.subcommand), cmd.options), None) if not subcommand_option: raise VergeMLError(f"{command} takes no subcommand", help_topic=command) plugin = self.plugins.get(subcommand_option.subcommand, subcommand) if not plugin: raise VergeMLError(f"Invalid {subcommand_option.name}", help_topic=command) cmd = Command.discover(plugin) return cmd.usage(short, parent_command=command) # display model command help elif topic in model_commands: return model_commands[topic].usage(short) # display command help elif topic in self.plugins.keys('vergeml.cmd'): cmd = Command.discover(self.plugins.get('vergeml.cmd', topic)) return cmd.usage(short) elif topic in self.plugins.keys('vergeml.operation'): return _with_header(self.format_source_or_operation( topic, 'vergeml.operation', Operation), help="preprocessing operation", topic=topic) elif topic in self.plugins.keys('vergeml.io'): return _with_header(self.format_source_or_operation( topic, 'vergeml.io', Source), help="data source", topic=topic) elif topic in self.plugins.keys('vergeml.model'): return _with_header(self.format_model(topic), help="models", topic=topic) # show a glossary entry elif glossary.long_descr(topic): topic = glossary.SYNONYMS.get(topic, topic) return _with_header(format_info_text( glossary.long_descr(topic)), help="glossary", topic=topic) # show base options help elif topic in dict(HELP_OPTIONS): return _with_header(format_info_text( dict(HELP_OPTIONS).get(topic)), help="base options", topic=topic) else: candidates = set() candidates.update(map(lambda h: h[0], _GENERAL_HELP)) candidates.update(self.plugins.keys("vergeml.cmd")) candidates.update(map(lambda h: h[0], _CONFIGURATION_HELP)) candidates.update(self.plugins.keys("vergeml.io")) candidates.update(self.plugins.keys("vergeml.operation")) candidates.update(self.plugins.keys("vergeml.model")) if env and env.model: for fn in Command.find_functions(env.model): cmd = Command.discover(fn) candidates.add(cmd.name) candidates.update(glossary.LONG_DESCR.keys()) candidates.update(glossary.SYNONYMS.keys()) suggestion = did_you_mean(list(candidates), topic) if suggestion: return f"No help found for topic '{topic}'. " + suggestion else: return f"No help found for topic '{topic}'." else: return self.format_general_help(env, short=short)