예제 #1
0
    def test_basic(self):
        class Namespace(object):
            pass

        obj = Namespace()
        setattr(obj, 'foo', 'bar')
        setattr(obj, 'foo_baz', 'bar')

        coe = ConfigObjEnv(obj)
        assert coe.get('foo') == 'bar'
        assert coe.get('FOO') == 'bar'
        assert coe.get('FOO_BAZ') == 'bar'
예제 #2
0
    def test_basic(self):
        class Namespace:
            pass

        obj = Namespace()
        setattr(obj, "foo", "bar")
        setattr(obj, "foo_baz", "bar")

        coe = ConfigObjEnv(obj)
        assert coe.get("foo") == "bar"
        assert coe.get("FOO") == "bar"
        assert coe.get("FOO_BAZ") == "bar"
예제 #3
0
    def test_basic(self):
        class Namespace(object):
            pass

        obj = Namespace()
        setattr(obj, 'foo', 'bar')
        setattr(obj, 'foo_baz', 'bar')

        coe = ConfigObjEnv(obj)
        assert coe.get('foo') == 'bar'
        assert coe.get('FOO') == 'bar'
        assert coe.get('FOO_BAZ') == 'bar'
예제 #4
0
    def test_with_argparse(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--debug', help='to debug or not to debug')
        parsed_vals = parser.parse_known_args([])[0]

        config = ConfigManager([ConfigObjEnv(parsed_vals)])

        assert config('debug', parser=bool, raise_error=False) is NO_VALUE

        parsed_vals = parser.parse_known_args(['--debug=y'])[0]

        config = ConfigManager([ConfigObjEnv(parsed_vals)])

        assert config('debug', parser=bool) is True
예제 #5
0
    def test_with_argparse_actions(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--debug',
                            help='to debug or not to debug',
                            action='store_true')
        parsed_vals = parser.parse_known_args([])[0]

        config = ConfigManager([ConfigObjEnv(parsed_vals)])

        # What happens is that argparse doesn't see an arg, so saves
        # debug=False. ConfigObjEnv converts that to "False". That gets parsed
        # as False by the Everett parse_bool function. That's kind of
        # roundabout but "works" for some/most cases.
        assert config('debug', parser=bool) is False

        parsed_vals = parser.parse_known_args(['--debug'])[0]

        config = ConfigManager([ConfigObjEnv(parsed_vals)])

        assert config('debug', parser=bool) is True
예제 #6
0
def run_program(app, args, parser=None):
    if parser is None:
        parser = argparse.ArgumentParser(prog=app.program_name)

    parser.add_argument(
        '--config',
        help=('Config file in ENV format. Setting options on the command line '
              'will override values in the config file.'),
    )

    options = app.get_required_config()

    for opt in options:
        # We don't enforce required here--we do that in a later pass so we can
        # take configuration files into account.
        kwargs = {
            'help': opt.doc,
            'type': handle_no_value(opt.parser),
            'action': 'store',
        }

        if opt.default is not NO_VALUE:
            kwargs['help'] += ' Default is %s.' % opt.default

        parser.add_argument('--%s' % opt.key.lower(), **kwargs)

    parser.set_defaults(handler=app)

    # Parse the args--this will exit if there's a --help
    vals, extra = parser.parse_known_args(args)

    config = ConfigManager([
        ConfigObjEnv(vals),

        # FIXME(willkg): It'd be better if this was an INI file.
        ConfigEnvFileEnv(vals.config),
    ])

    # Now go through and make sure all the required options were supplied.
    missing_opts = []
    parse_errors = []
    comp_config = config.with_options(app)
    for opt in options:
        try:
            comp_config(opt.key.lower())
        except ConfigurationMissingError as cme:
            missing_opts.append(opt.key.lower())
        except Exception as exc:
            parse_errors.append((opt.key.lower(), str(exc)))

    if missing_opts:
        parser.print_usage(sys.stderr)
        print_error('the following are required: %s\n' %
                    ', '.join([opt for opt in missing_opts]))
        return 2

    if parse_errors:
        parser.print_usage(sys.stderr)
        print_error('the following have value errors:\n')
        for opt, msg in parse_errors:
            print_error('%s:\n%s\n' % (opt, indent(msg)))
        return 2

    return vals.handler(config).invoke()