Пример #1
0
    def __new__(cls, name, bases, attrs):
        for attr_name, attr_value in iteritems(attrs):
            if isinstance(
                    attr_value,
                    types.FunctionType) and attr_name.startswith('test_'):
                attrs[attr_name] = mock_api(attr_value)

        return super(CLIMetaClass, cls).__new__(cls, name, bases, attrs)
Пример #2
0
def add_parser_arguments(parser, args, group=None, prefix=DATA_PREFIX):
    """
    Helper method that populates parser arguments. The argument values can
    be later retrieved with `extract_arguments` method.

    The `args` argument to this method should be a dict with strings as
    keys and dicts as values. The keys will be used as keys in returned
    data. Their values will be passed as kwargs to `parser.add_argument`.
    There is special value `arg` that will be used as argument name if
    present, otherwise a name will be generated based on the key.

    If `group` is a string, it will be used as group header in help output.
    """
    if group:
        parser = parser.add_argument_group(group)
    for arg, kwargs in iteritems(args):
        arg_name = kwargs.pop('arg', arg.replace('_', '-'))
        if 'metavar' not in kwargs:
            kwargs['metavar'] = arg.upper()
        parser.add_argument('--' + arg_name, dest=prefix + arg, **kwargs)
Пример #3
0
def add_mutually_exclusive_args(parser, args, required=False, prefix=DATA_PREFIX):
    """
    Helper method that populates mutually exclusive arguments. The argument values can
    be later retrieved with `extract_arguments` method.

    The `args` argument to this method should be a dict with strings as
    keys and dicts as values. The keys will be used as keys in returned
    data. Their values will be passed as kwargs to `parser.add_argument`.
    There is special value `arg` that will be used as argument name if
    present, otherwise a name will be generated based on the key.

    ``required`` will be passed to `parser.add_mutually_exclusive_group` to
    to indicate that at least one of the mutually exclusive arguments is required.
    """
    parser = parser.add_mutually_exclusive_group(required=required)
    for arg, kwargs in iteritems(args):
        arg_name = kwargs.pop('arg', arg.replace('_', '-'))
        if 'metavar' not in kwargs:
            kwargs['metavar'] = arg.upper()
        parser.add_argument('--' + arg_name, dest=prefix + arg, **kwargs)
def add_parser_arguments(parser, args, group=None, prefix=DATA_PREFIX):
    """
    Helper method that populates parser arguments. The argument values can
    be later retrieved with `extract_arguments` method.

    The `args` argument to this method should be a dict with strings as
    keys and dicts as values. The keys will be used as keys in returned
    data. Their values will be passed as kwargs to `parser.add_argument`.
    There is special value `arg` that will be used as argument name if
    present, otherwise a name will be generated based on the key.

    If `group` is a string, it will be used as group header in help output.
    """
    if group:
        parser = parser.add_argument_group(group)
    for arg, kwargs in iteritems(args):
        arg_name = kwargs.pop("arg", arg.replace("_", "-"))
        if "metavar" not in kwargs:
            kwargs["metavar"] = arg.upper()
        parser.add_argument("--" + arg_name, dest=prefix + arg, **kwargs)
def add_mutually_exclusive_args(parser, args, required=False, prefix=DATA_PREFIX):
    """
    Helper method that populates mutually exclusive arguments. The argument values can
    be later retrieved with `extract_arguments` method.

    The `args` argument to this method should be a dict with strings as
    keys and dicts as values. The keys will be used as keys in returned
    data. Their values will be passed as kwargs to `parser.add_argument`.
    There is special value `arg` that will be used as argument name if
    present, otherwise a name will be generated based on the key.

    ``required`` will be passed to `parser.add_mutually_exclusive_group` to
    to indicate that at least one of the mutually exclusive arguments is required.
    """
    parser = parser.add_mutually_exclusive_group(required=required)
    for arg, kwargs in iteritems(args):
        arg_name = kwargs.pop("arg", arg.replace("_", "-"))
        if "metavar" not in kwargs:
            kwargs["metavar"] = arg.upper()
        parser.add_argument("--" + arg_name, dest=prefix + arg, **kwargs)
Пример #6
0
def extract_arguments(args, prefix=DATA_PREFIX):
    """Return a dict of arguments created by `add_parser_arguments`.

    If the key in `args` contains two underscores, a nested dictionary will be
    created. Only keys starting with given prefix are examined. The prefix is
    stripped away and does not appear in the result.
    """
    data = {}
    for key, value in iteritems(args.__dict__):
        if key.startswith(prefix) and value is not None:
            parts = key[len(prefix):].split('__')
            # Think of `d` as a pointer into the resulting nested dictionary.
            # The `for` loop iterates over all parts of the key except the last
            # to find the proper dict into which the value should be inserted.
            # If the subdicts do not exist, they are created.
            d = data
            for p in parts[:-1]:
                assert p not in d or isinstance(d[p], dict)
                d = d.setdefault(p, {})
            # At this point `d` points to the correct dict and value can be
            # inserted.
            d[parts[-1]] = value if value != '' else None
    return data
def extract_arguments(args, prefix=DATA_PREFIX):
    """Return a dict of arguments created by `add_parser_arguments`.

    If the key in `args` contains two underscores, a nested dictionary will be
    created. Only keys starting with given prefix are examined. The prefix is
    stripped away and does not appear in the result.
    """
    data = {}
    for key, value in iteritems(args.__dict__):
        if key.startswith(prefix) and value is not None:
            parts = key[len(prefix) :].split("__")
            # Think of `d` as a pointer into the resulting nested dictionary.
            # The `for` loop iterates over all parts of the key except the last
            # to find the proper dict into which the value should be inserted.
            # If the subdicts do not exist, they are created.
            d = data
            for p in parts[:-1]:
                assert p not in d or isinstance(d[p], dict)
                d = d.setdefault(p, {})
            # At this point `d` points to the correct dict and value can be
            # inserted.
            d[parts[-1]] = value if value != "" else None
    return data
Пример #8
0
def _pprint_dict(file, data, indent):
    """Print a dict as an indented definition list."""
    for key, value in iteritems(data):
        _pprint_str(file, key + ':', indent)
        pretty_print(value, indent + 1, file)
    def __new__(cls, name, bases, attrs):
        for attr_name, attr_value in iteritems(attrs):
            if isinstance(attr_value, types.FunctionType) and attr_name.startswith('test_'):
                attrs[attr_name] = mock_api(attr_value)

        return super(CLIMetaClass, cls).__new__(cls, name, bases, attrs)