Ejemplo n.º 1
0
class Arguments(object):
    """
    Provides a wrapper around a list of :class:`Argument`. For example

    .. code-block:: python

        class PluginExample(Plugin):
            arguments = PluginArguments(
                PluginArgument("username",
                               help="The username for your account.",
                               metavar="EMAIL",
                               requires=["password"]),  // requires the password too
                PluginArgument("password",
                               sensitive=True,  // should be masked in logs, etc.
                               help="The password for your account.",
                               metavar="PASSWORD")
            )

    This will add the ``--plugin-username`` and ``--plugin-password`` arguments to the CLI
    (assuming the plugin module is ``plugin``).

    """
    def __init__(self, *args):
        self.arguments = OrderedDict((arg.name, arg) for arg in args)

    def __iter__(self):
        return iter(self.arguments.values())

    def get(self, name):
        return self.arguments.get(name)

    def requires(self, name):
        """
        Find all the arguments required by name

        :param name: name of the argument the find the dependencies

        :return: list of dependant arguments
        """
        results = {name}
        argument = self.get(name)
        for reqname in argument.requires:
            required = self.get(reqname)
            if not required:
                raise KeyError(
                    "{0} is not a valid argument for this plugin".format(
                        reqname))

            if required.name in results:
                raise RuntimeError("cycle detected in plugin argument config")
            results.add(required.name)
            yield required

            for r in self.requires(required.name):
                if r.name in results:
                    raise RuntimeError(
                        "cycle detected in plugin argument config")
                results.add(r.name)
                yield r
Ejemplo n.º 2
0
class Section(object):
    def __init__(self, name, params=None):
        self._name = name
        if params is None:
            self._params = OrderedDict()
        else:
            self._params = params
        self._sub_sections = OrderedDict()

    def __getitem__(self, index):
        return self.get_subsections_by_name(index)

    def __repr__(self):
        return 'Section %s (%s)' % (self.get_name(), self._params or '')

    def set_params(self, **params):
        self._params = params

    def get_params(self):
        return self._params

    def get_name(self):
        return self._name

    def add_sub_section(self, sec):
        key = sec.get_name()
        if key not in self._sub_sections:
            self._sub_sections[key] = []

        self._sub_sections[key].append(sec)
        return sec

    def get_subsections_by_name(self, name):
        return self._sub_sections.get(name, [])

    def get_subsections_by_param_val(self, **kwargs):
        ret = []
        for item in self.get_subsections():
            item_match = True
            for k, v in kwargs.iteritems():
                item_params = item.get_params()
                if k in item_params.keys():
                    if item_params[k] != v:
                        item_match = False
            if item_match:
                ret.append(item)
        return ret

    def get_subsections(self):
        for subsec in self._sub_sections.itervalues():
            for sub in subsec:
                yield sub

    def search_section_childs(self, section_name, param_name):
        return self._search_section_params(self, section_name, param_name)

    def _search_section_params(self, section, section_name, param_name):
        param_values = set()
        for s in section.get_subsections():
            if s.get_name() == section_name:
                for key, val in s.get_params().iteritems():
                    if key == param_name:
                        param_values.add(val)
            deeper_values = self._search_section_params(s, section_name, param_name)
            param_values |= deeper_values
        return param_values

    # Generator for subsections
    def __iter__(self):
        return self.get_subsections()