def get_command_dict(self, option): """Describes CommandOption instance as a dict.""" result = yaml_utils.OrderedYamlDict(type=option.type) for attr in ["default", "choices", "help"]: value = getattr(option, attr, None) if value is not None: result[attr] = copy.copy(value) for attr in ["positional", "required", "hidden"]: value = getattr(option, attr, False) if value: result[attr] = copy.copy(value) return result
def get_plugin_args(self, cls): """Collects the args from the plugin.""" args = yaml_utils.OrderedYamlDict() for subclass in cls.__mro__: for definition in getattr(subclass, "_%s__args" % subclass.__name__, []): # Definitions can be just simple dicts. if isinstance(definition, dict): definition = plugin.CommandOption(**definition) # We have seen this arg before. previous_definition = args.get(definition.name) if previous_definition: # Since we traverse the definition in reverse MRO order, # later definitions should be masked by earlier (more # derived) definitions. continue args[definition.name] = self.get_command_dict(definition) return args
def generate_api(self): # All plugins are registered with the base plugin. for plugin_name, cls in sorted(six.iteritems(self.classes)): if not cls.name: continue docstring = self._clean_up_doc(cls.__doc__ or cls.__init__.__doc__ or "") # Control the order of entries in the yaml file. result = yaml_utils.OrderedYamlDict() result["plugin"] = plugin_name result["name"] = cls.name result["description"] = docstring args = self.get_plugin_args(cls) if args: result["args"] = args result["active_modes"] = self.get_active_modes(cls) yield result