Пример #1
0
def test_default_cmd():
    conf = """\
        clg:
            default_cmd: run
            subparsers:
                run:
                    help: 'run as:  $prog run'
                install:
                    help: 'run as:  $prog install | sudo bash'"""
    c = Config(conf, args=['netapplet.py'])
    assert 'run' == c.command0

    with exc(SystemExit) as e:
        c = Config(conf, args=['netapplet.py', '-help'])
    assert e().code.startswith('usage:')

    conf = """\
        clg:
            default_cmd: uninstall
            subparsers:
                run:
                    help: 'run as:  $prog run'"""
    with exc(SystemExit) as e:
        c = Config(conf, args=['netapplet.py'])
    assert e().code.startswith('usage:')
Пример #2
0
def test_run_namespace():
    conf = """\
        prog: netapplet.py
        clg:
            subparsers:
                install:
                    help: 'run as:  $prog install | sudo bash'"""

    class Prog(object):
        def __init__(self, c):
            self.__dict__.update(c)

        def install(self):
            return 'echo "Script lines to install {}."'.format(self.prog)

    # Ensure Prog class is reachable from this module. Equivalent to import
    #  test.test_config at the begginig of this module
    lname = __import__(__name__.partition('.')[0])
    for name in __name__.split('.')[1:]:
        lname = getattr(lname, name)
    lname.Prog = Prog
    c = Config(conf, args=['', 'install'])

    # Assert as a class namespace
    expected = 'echo "Script lines to install {}."'.format(c.prog)
    assert expected == c.run(Prog)

    # Assert as string namespace
    assert expected == c.run(__name__ + '.Prog')
Пример #3
0
def test_extra_config_single_line_properly_handled(f):
    '''Config adds extra {} on single-line strings as yaml require'''
    c = Config(args=['-E="hobbie: dancer"'])
    assert 'dancer' == c.hobbie
    # As usual, multiline dictionaries are indented on the next line
    c = Config(args=['-E="hobbie:\n  dancer"'])
    assert 'dancer' == c.hobbie
def serve_ctx():
    '''Functional serve context for testing core sphinxserve functionality'''

    c = Config('''\
        app: sphinxserve
        host: localhost
        port: 8888
        socket: $host:$port
        index_rst: |
            ================
            Test sphinxserve
            ================

            Simple sphinxserve test
        conf_py: |
            source_suffix = '.rst'
            master_doc = 'index'
        ''')
    with tempdir() as tmpdir:
        c.tmpdir = tmpdir
        with open(tmpdir + '/conf.py', 'w') as fh:
            fh.write(c.conf_py)
        with open(tmpdir + '/index.rst', 'w') as fh:
            fh.write(c.index_rst)

        c.proc = Process(target=main, args=(['sphinxserve', tmpdir],))
        c.proc.start()
        check_host(c.host, c.port, timeout=3)
        yield c
        c.proc.terminate()
        c.proc.join()
Пример #5
0
def main(args):
    c = Config(conf, args=args, version=__version__)
    if c.debug:
        log.root.setLevel(log.DEBUG)
    # Run command selected from cli (corresponding to conf subparser, and
    # Prog method. Eg: serve)
    c.run(Prog)
Пример #6
0
def main(args):
    c = Config(conf, args=args, version=__version__)

    logger.setLevel(c.loglevel)
    logging_stream = logging.StreamHandler()
    logging_format = '%(asctime)s %(name)s %(levelname)s %(message)s'
    if c.nocolor:
        formatter_cls = logging.Formatter
    else:
        formatter_cls = coloredlogs.ColoredFormatter
    logging_stream.setFormatter(formatter_cls(fmt=logging_format))
    logger.addHandler(logging_stream)

    # Run command selected from cli (corresponding to conf subparser, and
    # Prog method. Eg: serve)
    c.run(Prog)
Пример #7
0
def test_config(f):
    '''Test config process validate args, and generate envvars'''
    c = Config({'version': f.version, 'prog': f.prog},
               args=[f.prog, 'leon', '-E="{}"'.format(f.conf)])
    # Apparently clg return data out of order. Sort its output here.
    ret = '\n'.join(sorted(c.export().split('\n')))
    exp = dedent('''\
        export ARGS=""
        export DATA_FILE="$data_path/data.txt"
        export DOCKER__IMAGE="reg.gdl/debian"
        export EXTRA_CONFIG=""
        export HOST="leon"
        export PROG="dbuild"
        export SYSTEM_PATH="/data/salt/system"
        export VERSION="0.4.3"''')
    assert exp == ret
Пример #8
0
def test_read():
    '''Test yaml !read tag'''
    with tempfile() as fh:
        fh.write('/usr/local/lib')
        fh.flush()
        assert {'libpath': '/usr/local/lib'} == Config('libpath: !read {}'.
            format(fh.name))
Пример #9
0
def test_extra_config_and_config_file(f):
    with tempfile() as fh:
        fh.write(f.conf)
        fh.flush()
        c = Config(args=[f.prog, f.host, '-C="{}"'.format(fh.name),
            '-E="system_path: /tmp/systest"'])
    assert '/tmp/systest' == c.system_path
Пример #10
0
def test_include_config(f):
    '''Test yaml !include tag loads config'''
    conf = 'field: magnetic'
    with tempfile() as fh:
        fh.write(conf)
        fh.flush()
        c = Config('photon: !include {}'.format(fh.name))
    assert Odict('field: magnetic') == c.photon
Пример #11
0
def test_arguments(f):
    c = Config('data_path: /data', types=[basename],
            args=[f.prog, '-e', '/data/conf/sasha.conf', f.host,
            '-E="{}"'.format(f.conf)])
    assert f.host == c.host
    assert '/data/data.txt' == c.data_file
    assert '/data/salt/system' == c.system_path
    assert 'sasha.conf' == c.extra_config
Пример #12
0
def test_include_tag(f):
    '''Test include with a key'''
    conf = 'field: [magnetic, electric]'
    with tempfile() as fh:
        fh.write(conf)
        fh.flush()
        c = Config('photon: !include {}:field'.format(fh.name))
        assert Odict('photon: [magnetic, electric]') == c
Пример #13
0
def test_include_unknown_key(f):
    '''Test include with a key'''
    conf = 'field: [magnetic, electric]'
    with tempfile() as fh:
        fh.write(conf)
        fh.flush()
        c = Config('photon: !include {}:properties'.format(fh.name))
        assert Odict("photon: ''") == c
Пример #14
0
def main():
    conf = '!include config.yml'
    c = Config(conf)

    print(c.forecast_key)
    res = fetch_forecast_by_hour(c.API_KEYS.forecast_key, c.VARS.lat,
                                 c.VARS.lng)
    calc_fog_from_dewpoint_diff(res)
Пример #15
0
def test_convenient_config_file_from_directory(f):
    conf = '{color: [red, green, blue]}'
    with tempdir() as tmpdir:
        conf_file = '{}/config.conf'.format(tmpdir)
        with open(conf_file, 'w') as fh:
            fh.write(conf)
        c = Config(args=['-C={}'.format(tmpdir)])
    assert conf == repr(c)
Пример #16
0
def test_unexistent_include_config():
    '''Test yaml !include tag ignore unxexistent file'''
    with tempfile() as fh:
        tmpfile = fh.name
    c = Config('''\
            _: !include {}
            test: hi
            '''.format(tmpfile))
    assert '{test: hi}' == repr(c)
Пример #17
0
def test_include_config_wont_empty_subkey_lists():
    '''Ensure including a file, even an empty one, will parse lists properly'''
    with tempfile() as fh:  # Create an empty file
        c = Config('''\
                _: !include {}
                test:
                    list: [1, 2]
                '''.format(fh.name))
    assert '{test: {list: [1, 2]}}' == repr(c)
Пример #18
0
def test_verprog_from_options(f):
    with tempfile() as fh, exc(SystemExit) as e:
        fh.write(f.conf)
        fh.flush()
        Config(types={basename}, args=[f.prog, '-v',
            '-E="prog: {}"'.format(f.prog),
            '-C="{}"'.format(fh.name),
            '-E="version: {}"'.format(f.version)])
    assert '{} {}'.format(f.prog, f.version) == e().args[0]
Пример #19
0
def test_unexistent_include__config_wont_empty_subkey_lists():
    '''Ensure including a nonexistent file will parse lists properly'''
    with tempfile() as fh:  # Create a non-existent reference filename
        pass
    c = Config('''\
            _: !include {}
            test:
                list: [1, 2]
            '''.format(fh.name))
    assert '{test: {list: [1, 2]}}' == repr(c)
Пример #20
0
def test_expand_tag(f):
    conf = '''\
        properties:
          field:
            - magnetic
            - electric'''
    with tempfile() as fh:
        fh.write(conf)
        fh.flush()
        c = Config('''\
            _: !include {}:&
            photon: !expand properties:field'''.format(fh.name))
        assert Odict('photon: [magnetic, electric]') == c
Пример #21
0
def test_expand_unknown_key(f):
    conf = '''\
        properties:
          field:
            - magnetic
            - electric'''
    with tempfile() as fh:
        fh.write(conf)
        fh.flush()
        c = Config('''\
            _: !include {}:&
            photon: !expand properties:viscosity'''.format(fh.name))
        assert Odict("photon: ''") == c
Пример #22
0
def c(request):
    '''Config fixture. Return a config object for easy attribute access'''

    c = Config('''\
        prog: test_loadconfig.py
        test_version: 0.1.7

        conf: |
            version: $test_version
            clg:
                description: Build a full system
                options:
                    version:
                        short: v
                        action: version
                        version: $prog $test_version
                args:
                    host:
                        help: Host to build
                    args:
                        nargs: '*'
                        help: extra arguments
            checkconfig: |
                import re
                if re.search('[^\d\w]', '$host'):
                    raise Exception()

            system_path:       /data/salt/system
            docker__image:     reg.gdl/debian
        ''')

    # prog and version configs are hardcoded as this test runs in testsuite
    # Declare PYTHONPATH to use loadconfig package from this project
    c.project_path = dirname(ppath(__file__))
    c.loadconfig_cmd = 'PYTHONPATH={0} {0}/scripts/loadconfig'.format(
        c.project_path)
    return c
Пример #23
0
def c(request):
    '''Config fixture. Return a config object for easy attribute access'''

    c = Config('''\
        prog: test_loadconfig.py
        test_version: 0.1.7

        conf: |
            version: $test_version
            clg:
                description: Build a full system
                options:
                    version:
                        short: v
                        action: version
                        version: $prog $test_version
                args:
                    host:
                        help: Host to build
                    args:
                        nargs: '*'
                        help: extra arguments
            checkconfig: |
                import re
                if re.search('[^\d\w]', '$host'):
                    raise Exception()

            system_path:       /data/salt/system
            docker__image:     reg.gdl/debian
        ''')

    # prog and version configs are hardcoded as this test runs in testsuite
    # Declare PYTHONPATH to use loadconfig package from this project
    c.project_path = dirname(ppath(__file__))
    c.loadconfig_cmd = 'PYTHONPATH={0} {0}/scripts/loadconfig'.format(
        c.project_path)
    return c
Пример #24
0
def test_help(f):
    '''Test version and program show properly.'''
    with exc(SystemExit) as e:
        Config(args=[f.prog, '-h', '-E="{}"'.format(f.conf)],
            version=f.version, types={basename})
    exp = dedent('''\
        usage: dbuild [-h] [-v] [-e EXTRA_CONFIG] host [args [args ...]]

        Build a full system

        positional arguments:
          host                  Host to build
          args                  extra arguments

        optional arguments:
          -h, --help            show this help message and exit
          -v, --version         show program's version number and exit
          -e EXTRA_CONFIG, --extra-config EXTRA_CONFIG''')
    assert exp == e().args[0]
Пример #25
0
def get_config(opt_vars):
    """ Merge configuration from 'config.py' of ListenBrainz, commandline arguments and \
        the default values with order of preference

        ConfigFile > CommandlineArguments > DefaultConfig
    """

    config = {
        "SQLALCHEMY_DATABASE_URI": "postgresql://listenbrainz@/listenbrainz"
    }
    config.update(opt_vars)

    try:
        opt_vars['CONFIG'] = path.abspath(opt_vars['CONFIG'])
        custom_config = Config(path.dirname(__file__)).from_pyfile(
            opt_vars['CONFIG'])
        config.update(custom_config)
    except Exception, e:
        log = logging.getLogger(__name__)
        log.error(e)
Пример #26
0
def test_string_argument_value_integer_is_preserved(f):
    c = Config('year: 2015')
    assert 2015 == c.year
Пример #27
0
def test_env_unexistent():
    c = Config('!env city')
    assert '' == c.city
Пример #28
0
def test_env():
    '''Test yaml !env tag'''
    environ['CITY'] = 'San Francisco'
    c = Config('!env city')
    assert 'San Francisco' == c.city
    del environ['CITY']
Пример #29
0
def test_export_list(f):
    c = Config('Outdoor activity: [hike, bike, scuba dive, run]')
    exp = '{Outdoor activity: [hike, bike, scuba dive, run]}'
    assert exp == repr(c)
    exp = 'export OUTDOOR_ACTIVITY="hike bike \'scuba dive\' run"'
    assert exp == c.export()
Пример #30
0
def test_export_dict(f):
    c = Config('Outdoor activity: {mountain: bike, ocean: scuba dive}')
    exp = '{Outdoor activity: {mountain: bike, ocean: scuba dive}}'
    assert exp == repr(c)
    exp = 'export OUTDOOR_ACTIVITY="{mountain: bike, ocean: scuba dive}"'
    assert exp == c.export()
Пример #31
0
def test_empty_include_config():
    '''Test yaml !include tag ignore empty file'''
    with tempfile() as fh:
        c = Config('_: !include {}'.format(fh.name))
    assert '{}' == repr(c)
Пример #32
0
def test_update_with_string_argument(f):
    c = Config('hi: there')
    c.update('hi: Liss')
    assert 'Liss' == c.hi
Пример #33
0
from loadconfig import Config
import redis

config = Config()


class Cache:
    def __init__(self):
        cache = config.get_cache_config()
        self.con = redis.Redis(
               host=cache['host'],
               port=cache['port'],
               password=cache['password']
            )

    # get Redis connection
    def get_conn(self):
        return self.con

Пример #34
0
def test_clg_key_not_present(f):
    '''Config stores all args including program name'''
    c = Config(args=[f.prog, f.host, '-E="{}"'.format(f.conf)])
    assert 'clg' not in c
Пример #35
0
 def __init__(self, path):
     conf = "!include {}".format(path)
     config = Config(conf)
     self.configuration = Config(conf)
Пример #36
0
def main(args):
    c = Config(conf, args, version=version)
    assert c.version == '0.1.5'