Пример #1
0
    def __init__(self, fmt_string, *a: Union[str, Type[Option]],
                 **kw: Union[str, Type[Option]]):
        """
        :param fmt_string:
        :param args:
        :param kwargs:
        :return:
        """
        super().__init__()
        self.fmt_string = fmt_string
        self.args = tuple(coerce(arg) for arg in a)
        self.kwargs = {k: coerce(v) for k, v in kw.items()}

        args, kwargs = "", ""
        if self.args:
            args = (
                ", with {} as positional {}".format(
                    ', '.join(option_reference(opt) for opt in self.args),
                    "arguments" if len(self.args) > 1 else "argument")
            )

        if self.kwargs:
            kwargs = (
                ", with {} as keyword {}"
                .format(', '.join("{}={}".format(
                    key, option_reference(opt))
                    for key, opt in self.kwargs.items()),
                 "arguments" if len(self.args) > 1 else "argument")
            )

        self.__doc__ = (
            "Will be computed from the format string :python:`{!r}`{}{}."
            .format(self.fmt_string, args, kwargs)
        )
Пример #2
0
 def __init__(self, fmt_string, *args: Union[str, type(Option)],
              **kwargs: Union[str, type(Option)]):
     """
     :param fmt_string:
     :param args:
     :param kwargs:
     :return:
     """
     super().__init__()
     self.fmt_string = fmt_string
     self.args = tuple(coerce(arg) for arg in args)
     self.kwargs = {k: coerce(v) for k, v in kwargs}
Пример #3
0
 def __init__(self, other_option):
     super().__init__()
     self.other_option = coerce(other_option)
     self.__doc__ = (
         "Must be contained in the networks configured with {}"
         .format(option_reference(self.other_option))
     )
Пример #4
0
    def __init__(self, other: Union[str, Type[Option]]):
        super().__init__()
        self.other_name = coerce(other)
        if not isinstance(self.other_name, str):
            raise TypeError("Expected Option subclass or str, was {}"
                            .format(type(self.other_name)))

        self.__doc__ = "Equal to {}".format(option_reference(self.other_name))
Пример #5
0
def deferred_format(fmt_string, *args, **kwargs):
    """
    Evaluate a format string using values from others config options.

    Names of options are given as positional arguments and the corresponding
    values can be referred to using numbers in the format string.
    Keywords arguments can be used as well to bind other option values to
    specific names that are available in the format string.
    :param fmt_string:
    :param args:
    :param kwargs:
    :return:
    """
    args = tuple(coerce(arg) for arg in args)
    kwargs = {k: coerce(v) for k, v in kwargs}

    # noinspection PyDecorator,PyUnusedLocal
    @classmethod
    def f(cls, config):
        fmt_args = tuple(config[a] for a in args)
        fmt_kwargs = {k: config[v] for k, v in kwargs}
        return fmt_string.format(*fmt_args, **fmt_kwargs)

    return f
Пример #6
0
def ip_range_in_networks(other_option):
    other_option = coerce(other_option)

    # noinspection PyDecorator
    @classmethod
    def checker(cls, config, value):
        networks = config[other_option]
        first = netaddr.IPAddress(value.first)
        last = netaddr.IPAddress(value.last)
        contained = any(first in network and last in network
                        for network in networks)
        if not contained:
            raise OptionCheckError("Range not contained in any of the "
                                   "networks {}".format(', '.join(networks)),
                                   option=cls.__name__)

    return checker
Пример #7
0
def equal_to(other):
    other_name = coerce(other)
    if not isinstance(other_name, str):
        raise TypeError("Expected Option subclass or str, was {}".format(
            type(other_name)))

    # noinspection PyDecorator
    @classmethod
    def f(cls, config):
        try:
            return config[other_name]
        except MissingOptionError as e:
            raise ConfigError("Can not set equal to option {}, option is not "
                              "defined".format(other_name),
                              option=cls.__name__) from e

    return f
Пример #8
0
 def __init__(self, other: Union[str, type(Option)]):
     super().__init__()
     self.other_name = coerce(other)
     if not isinstance(self.other_name, str):
         raise TypeError("Expected Option subclass or str, was {}"
                         .format(type(self.other_name)))
Пример #9
0
 def __init__(self, other_option):
     super().__init__()
     self.other_option = coerce(other_option)