Exemplo n.º 1
0
def test_dir_ops():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    config_dir = f'/tmp/config_dir_{file_id}'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        f.write(f'''
data:
    abc: ahe
    float: 6.1
    float_str: '6.1'
    int_str: '6'
config_dir: {config_dir}
''')

    config = load_config(config_file)
    ok_(config.config_dir == config_dir)
    ok_(not os.path.exists(config_dir), 'Dir must not exist')

    config.config_dir.ensure_path_existence()
    ok_(os.path.exists(config_dir), 'Dir must exist')

    ok_(not os.path.exists(config_dir + '-1'), 'Dir must not exist')
    config.config_dir.backup_path()
    ok_(os.path.exists(config_dir + '-1'), 'Dir must exist')
Exemplo n.º 2
0
def test_io():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    # test load config from file
    with open(config_file, 'w') as f:
        content = f'''data:
    abc: ahe
    float: 6.1
    float_str: '6.1'
    int_str: '6'
dir:
    __workdir__: /home/ubuntu
    download: Downloads
'''
        f.write(content)

    config = load_config(config_file)
    ok_(config.data.abc == 'ahe')
    ok_(config.data.abc.as_path() == '/tmp/ahe')

    os.remove(config_file)

    # test save config to file
    ok_(not os.path.exists(config_file + '.backup'))
    write_config(config, config_file + '.backup')
    ok_(os.path.exists(config_file + '.backup'))
    with open(config_file + '.backup', 'r') as f:
        eq_(content, f.read())
Exemplo n.º 3
0
def test_delete_and_contain_ops():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        f.write(f'''
logs:
    __workdir__: /home/peter
    expense: expense_sheets.csv
data:
    __workdir__: '/data'
    gold:
        monthly_expense: '@logs.expense'
        monthly_expense_path: '@@logs.expense'
''')

    config = load_config(config_file)
    ok_('expense' in config.logs)
    ok_('__workdir__' not in config.logs)
    ok_('abc' not in config.logs)
    ok_('gold' in config.data)

    del config.logs.expense
    del config.data['gold']
    ok_('expense' not in config.logs)
    ok_('gold' not in config.data)
Exemplo n.º 4
0
def test_to_dict_ops():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        f.write(f'''
logs:
    __workdir__: /home/peter
    expense: expense_sheets.csv
data:
    __workdir__: '/data'
    gold:
        monthly_expense: '@logs.expense'
        monthly_expense_path: '@@logs.expense'
''')

    config = load_config(config_file)
    eq_(
        config.data.to_dict(), {
            'gold': {
                'monthly_expense': 'expense_sheets.csv',
                'monthly_expense_path': '/home/peter/expense_sheets.csv',
            }
        })
Exemplo n.º 5
0
def test_parsing_structure():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        content = f'''data:
    abc: ahe
    float: 6.1
    float_str: '6.1'
    int_str: '6'
dir:
    __workdir__: /home/ubuntu
    download: Downloads
    documents: '@#Documents'
people:
  - name: peter
    mailbox: [email protected]
'''
        f.write(content)

    config = load_config(config_file)
    eq_(config.data.abc, 'ahe')
    eq_(config.data.abc.as_path(), '/tmp/ahe')
    eq_(config.data.float, 6.1)
    eq_(config.data.float_str, '6.1')
    eq_(config.data.float_str.as_float(), 6.1)
    eq_(config.data.int_str.as_int(), 6)
    eq_(config.data['abc'], 'ahe')
    eq_(config.get_conf('data.abc'), 'ahe')

    eq_(config.dir.download, 'Downloads')
    eq_(config.dir.download.as_path(), '/home/ubuntu/Downloads')
    eq_(config.dir.documents, '/home/ubuntu/Documents')

    ok_(isinstance(config.people, ListConf))
    ok_(isinstance(config.people[0], Configuration))
    eq_(config.people[0].mailbox, '*****@*****.**')
    eq_(config.people[0].mailbox.as_path(), '/tmp/[email protected]')

    config.set_conf('data.test.abc', 5)
    eq_(config.data.test.abc, 5)
Exemplo n.º 6
0
def test_iter():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        f.write(f'''
logs:
    __workdir__: /home/peter
    expense: expense_sheets.csv
data:
    __workdir__: '/data'
    gold:
        monthly_expense: '@logs.expense'
        monthly_expense_path: '@@logs.expense'
''')

    config = load_config(config_file)
    ok_(list(config.data.gold.items()),
        [('monthly_expense', 'expense_sheets.csv'),
         ('monthly_expense_path', '/home/peter/expense_sheets.csv')])
    ok_(list(config.data.gold), ['monthly_expense', 'monthly_expense_path'])
Exemplo n.º 7
0
def test_ref_ops():
    file_id = str(uuid.uuid4())
    config_file = f'/tmp/config_file_{file_id}.txt'
    ok_(not os.path.exists(config_file))

    with open(config_file, 'w') as f:
        f.write(f'''
logs:
    __workdir__: /home/peter
    expense: expense_sheets.csv
data:
    __workdir__: '/data'
    gold:
        monthly_expense: '@logs.expense'
        monthly_expense_path: '@@logs.expense'
        monthly_expense_copied: '@#expense_sheets.csv'
    checklists:
        - '@logs.expense'
        - '@@logs.expense'
        - '@#expense_sheets.csv'
''')

    config = load_config(config_file)
    eq_(config.logs.expense, 'expense_sheets.csv')
    eq_(config.logs.expense.as_path(), '/home/peter/expense_sheets.csv')
    eq_(config.data.gold.monthly_expense, 'expense_sheets.csv')
    ok_(isinstance(config.data.gold.monthly_expense, StringConf))
    eq_(config.data.gold.monthly_expense.as_path(),
        '/home/peter/expense_sheets.csv')
    eq_(config.data.gold.monthly_expense_path,
        '/home/peter/expense_sheets.csv')
    eq_(config.data.gold.monthly_expense_copied, '/data/expense_sheets.csv')
    ok_(isinstance(config.data.gold.monthly_expense_path, StringConf))
    eq_(config.data.checklists.to_list(), [
        'expense_sheets.csv', '/home/peter/expense_sheets.csv',
        '/data/expense_sheets.csv'
    ])
    ok_(isinstance(config.data.checklists[0], StringConf))
Exemplo n.º 8
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import os
from logging import Logger

import pkg_resources
import pyutils
from pyutils.config_utils import Configuration, load_config

current_file_path = pkg_resources.resource_filename("semantic_modeling",
                                                    "config.py")
PACKAGE_DIR = os.path.abspath(os.path.join(current_file_path, "../../"))
config: Configuration = load_config(os.path.join(PACKAGE_DIR, '../config.yml'))
logger_config: Configuration = load_config(
    os.path.join(PACKAGE_DIR, 'logging.yml'))


def get_logger(name) -> Logger:
    return pyutils.logging.Logger.get_instance(
        logger_config, init_logging=False).get_logger(name)
Exemplo n.º 9
0
 def import_from_yaml(self, filename, **kwargs):
     config = load_config(filename, as_dict=True)
     self.import_from_dict(config, **kwargs)