How to use the confuse.OneOf function in confuse

To help you get started, we’ve selected a few confuse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github beetbox / confuse / test / test_valid.py View on Github external
def test_validate_no_choice_in_list(self):
        config = _root({'foo': None})
        with self.assertRaises(confuse.ConfigValueError):
            config['foo'].get(confuse.OneOf([
                confuse.String(),
                confuse.Integer(),
            ]))
github beetbox / confuse / test / test_valid.py View on Github external
def test_validate_first_good_choice_in_list(self):
        config = _root({'foo': 3.14})
        valid = config['foo'].get(confuse.OneOf([
            confuse.Integer(),
            confuse.Number(),
        ]))
        self.assertEqual(valid, 3)
github beetbox / confuse / test / test_valid.py View on Github external
def test_default_value(self):
        config = _root({})
        valid = config['foo'].get(confuse.OneOf([], default='bar'))
        self.assertEqual(valid, 'bar')
github beetbox / confuse / test / test_valid.py View on Github external
def test_list_as_template(self):
        typ = confuse.as_template(list())
        self.assertIsInstance(typ, confuse.OneOf)
        self.assertEqual(typ.default, confuse.REQUIRED)
github beetbox / confuse / test / test_valid.py View on Github external
def test_validate_bad_template(self):
        class BadTemplate(object):
            pass
        config = _root({})
        with self.assertRaises(confuse.ConfigTemplateError):
            config.get(confuse.OneOf([BadTemplate()]))
        del BadTemplate
github beetbox / confuse / test / test_valid.py View on Github external
def test_validate_good_choice_in_list(self):
        config = _root({'foo': 2})
        valid = config['foo'].get(confuse.OneOf([
            confuse.String(),
            confuse.Integer(),
        ]))
        self.assertEqual(valid, 2)
github beetbox / confuse / example / __init__.py View on Github external
"""An example application using Confuse for configuration."""
from __future__ import division, absolute_import, print_function
import confuse
import argparse


template = {
    'library': confuse.Filename(),
    'import_write': confuse.OneOf([bool, 'ask', 'skip']),
    'ignore': confuse.StrSeq(),
    'plugins': list,

    'paths': {
        'directory': confuse.Filename(),
        'default': confuse.Filename(relative_to='directory'),
    }
}

config = confuse.LazyConfig('ConfuseExample', __name__)


def main():
    parser = argparse.ArgumentParser(description='example Confuse program')
    parser.add_argument('--library', '-l', dest='library', metavar='LIBPATH',
                        help='library database file')
github beetbox / beets / beetsplug / fetchart.py View on Github external
'coverart', 'itunes', 'amazon', 'albumart'],
            'google_key': None,
            'google_engine': u'001442825323518660753:hrh5ch1gjzm',
            'fanarttv_key': None,
            'store_source': False,
            'high_resolution': False,
        })
        self.config['google_key'].redact = True
        self.config['fanarttv_key'].redact = True

        self.minwidth = self.config['minwidth'].get(int)
        self.maxwidth = self.config['maxwidth'].get(int)

        # allow both pixel and percentage-based margin specifications
        self.enforce_ratio = self.config['enforce_ratio'].get(
            confuse.OneOf([bool,
                           confuse.String(pattern=self.PAT_PX),
                           confuse.String(pattern=self.PAT_PERCENT)]))
        self.margin_px = None
        self.margin_percent = None
        if type(self.enforce_ratio) is six.text_type:
            if self.enforce_ratio[-1] == u'%':
                self.margin_percent = float(self.enforce_ratio[:-1]) / 100
            elif self.enforce_ratio[-2:] == u'px':
                self.margin_px = int(self.enforce_ratio[:-2])
            else:
                # shouldn't happen
                raise confuse.ConfigValueError()
            self.enforce_ratio = True

        cover_names = self.config['cover_names'].as_str_seq()
        self.cover_names = list(map(util.bytestring_path, cover_names))