Example #1
0
    def _set_parser_defaults(self, data, *, parser_name):
        if not isinstance(data, dict):
            logger.warning(
                "Configuration option '{parser_name}' should be a dictionary".
                format_map(locals()))
            return

        defaults = {}
        destinations = self.get_destinations(recursive=False)
        argument_types = self._get_argument_types()
        for key, dest in destinations.items():
            if key in data:
                value = data[key]
                type_ = argument_types.get(dest)
                if type_ is not None:
                    # check if the value has the expected type
                    try:
                        self._check_argument_type(type_, value, key,
                                                  parser_name)
                    except TypeError:
                        continue
                defaults[dest] = wrap_default_value(value)
        unknown_keys = data.keys() - destinations.keys()
        if unknown_keys:
            unknown_keys_str = ', '.join(sorted(unknown_keys))
            logger.warn("Skipping unknown keys from '{self._config_path}' for "
                        "'{parser_name}': {unknown_keys_str}".format_map(
                            locals()))

        if defaults:
            logger.info(
                "Setting default values for parser '{parser_name}': {defaults}"
                .format_map(locals()))
            self._parser.set_defaults(**defaults)
def _custom_wrap_default_value(value):
    try:
        value = wrap_default_value(value)
    except ValueError:
        # avoid double wrapping and mark those default value to not unwrap them
        value._mixin_argument_already_default_value = True
    return value
 def add_arguments(self, *, parser, with_default):  # noqa: D102
     parser.add_argument(
         '--mp-base-paths',
         nargs='*',
         metavar='PATH',
         default=wrap_default_value(['.']) if with_default else None,
         help='The base paths to recursively crawl for packages' +
         (' (default: .)' if with_default else ''))
Example #4
0
 def add_arguments(  # noqa: D102
     self, *, parser, with_default, single_path=False
 ):
     parser.add_argument(
         '--paths',
         nargs='*' if not single_path else '?',
         metavar='PATH',
         default=wrap_default_value(['.']) if with_default else None,
         help='The paths to check for a package. Use shell wildcards '
              '(e.g. `src/*`) to select all direct subdirectories' +
              (' (default: .)' if with_default else ''))
def test_argument_default():
    values = [
        True,
        [1, 2, 3],
        'foo',
    ]
    for value in values:
        assert not is_default_value(value)
        with pytest.raises(ValueError):
            unwrap_default_value(value)
        default_value = wrap_default_value(value)
        assert is_default_value(default_value)
        assert type(default_value) != type(value)
        with pytest.raises(ValueError):
            wrap_default_value(default_value)
        unwrapped_value = unwrap_default_value(default_value)
        assert value == unwrapped_value

    value = 42
    unchanged_value = wrap_default_value(value)
    assert type(unchanged_value) == type(value)
    assert unchanged_value == value