コード例 #1
0
def test_result(fs):
    '''Test IP from settings.'''
    fs.add_real_file(settings_filepath, False)
    cfg = ConfigFile(name='tests')
    cfg.load(settings_filepath)
    result = cfg.search('/servers/**/ip')
    assert ['10.0.0.1', '10.0.0.2'] == result
コード例 #2
0
 def load_config(self,
                 filepath: str,
                 update: bool = True) -> Optional[ConfigFile]:
     '''Load settings from configuration.'''
     if os.path.exists(filepath):
         config_file = ConfigFile(filepath=filepath)
         config_file.load()
         if update:
             self.data.push(config_file)
         return config_file
     return None
コード例 #3
0
def test_toml_delete(fs):
    '''Test content deletion.'''
    fs.add_real_file(settings_filepath, False)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=settings_filepath)
    assert cfg.search('/owner/name') == ['Tom Preston-Werner']
    cfg.delete('/owner/name')
    assert cfg.search('/owner/name') == []
コード例 #4
0
ファイル: test_toml.py プロジェクト: kuwv/python-compendium
def test_cfg_save_fail(fs):
    '''Test TOML content failure.'''
    fs.add_real_file(toml_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=toml_filepath)

    with pytest.raises(CompendiumConfigFileError):
        cfg.create('/test', 'test')
        cfg.dump('./test.toml')
コード例 #5
0
ファイル: test_json.py プロジェクト: kuwv/python-compendium
def test_cfg_save_fail(fs):
    '''Test JSON failure.'''
    fs.add_real_file(json_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=json_filepath)

    with pytest.raises(CompendiumConfigFileError):
        cfg.create('/test', 'test')
        cfg.dump('./test.json')
コード例 #6
0
ファイル: test_json.py プロジェクト: kuwv/python-compendium
def test_json_filepath(fs):
    '''Test JSON filepaths.'''
    fs.add_real_file(json_filepath)
    cfg = ConfigFile(
        name='json',
        filepath=os.path.join(settings_filepath, 'test.json'),
    )
    assert "{}/test.json".format(settings_filepath) == cfg.filepath
コード例 #7
0
def test_xml_filepath(fs):
    '''Test XML path.'''
    fs.add_real_file(xml_filepath)
    cfg = ConfigFile(
        name='tests',
        filepath=os.path.join(config_filepath, 'test.xml'),
    )
    assert "{}/test.xml".format(config_filepath) == cfg.filepath
コード例 #8
0
ファイル: test_toml.py プロジェクト: kuwv/python-compendium
def test_toml_filepath(fs):
    '''Test TOML filepaths.'''
    fs.add_real_file(toml_filepath)
    cfg = ConfigFile(
        name='toml',
        filepath=os.path.join(config_filepath, 'test.toml')
    )
    assert "{}/test.toml".format(config_filepath) == cfg.filepath
コード例 #9
0
ファイル: test_json.py プロジェクト: kuwv/python-compendium
def test_cfg(fs):
    '''Test loading JSON configuration.'''
    fs.add_real_file(json_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=json_filepath)
    assert cfg.retrieve('/stooges/stooge1') == 'Larry'
    assert cfg.retrieve('/stooges/stooge2') == 'Curly'
    assert cfg.retrieve('/stooges/stooge3') == 'Moe'
    assert cfg.retrieve('/fruit') != 'banana'
    assert cfg.retrieve('/number') == 2
コード例 #10
0
ファイル: test_yaml.py プロジェクト: kuwv/python-compendium
def test_yaml_content(fs):
    '''Test read YAML content.'''
    fs.add_real_file(yaml_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=yaml_filepath)
    assert cfg.retrieve('/stooges/stooge1') == 'Larry'
    assert cfg.retrieve('/stooges/stooge2') == 'Curly'
    assert cfg.retrieve('/stooges/stooge3') == 'Moe'
    assert cfg.retrieve('/fruit') != 'banana'
    assert cfg.retrieve('/number') == 2
コード例 #11
0
def test_xml_content(fs):
    '''Test XML content read.'''
    fs.add_real_file(xml_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=xml_filepath)
    assert cfg.retrieve('/root/stooges/stooge1') == 'Larry'
    assert cfg.retrieve('/root/stooges/stooge2') == 'Curly'
    assert cfg.retrieve('/root/stooges/stooge3') == 'Moe'
    assert cfg.retrieve('/root/fruit') != 'banana'
    assert cfg.retrieve('/root/number') == '2'
コード例 #12
0
ファイル: test_ini.py プロジェクト: kuwv/python-compendium
def test_ini_content(fs):
    '''Test TOML content load.'''
    fs.add_real_file(ini_filepath)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=ini_filepath)
    assert cfg.retrieve('/stooges/stooge1') == 'Larry'
    assert cfg.retrieve('/stooges/stooge2') == 'Curly'
    assert cfg.retrieve('/stooges/stooge3') == 'Moe'
    assert cfg.retrieve('/fruit') == 'apple'
    assert int(cfg.retrieve('/number')) == 2
コード例 #13
0
def test_toml_content_update(fs):
    '''Test content update.'''
    fs.add_real_file(settings_filepath, False)
    cfg = ConfigFile(name='tests', writable=True)
    cfg.load(filepath=settings_filepath)
    cfg.set('/owner/name', 'Tom Waits')
    assert cfg.retrieve('/owner/name') == 'Tom Waits'
コード例 #14
0
ファイル: test_json.py プロジェクト: kuwv/python-compendium
def test_cfg_dump(fs):
    '''Test saving JSON content.'''
    fs.add_real_file(json_filepath, False)
    cfg = ConfigFile(name='tests', writable=True)
    cfg.load(filepath=json_filepath)
    cfg.create('/test', 'test')
    assert cfg.retrieve('test') == 'test'
コード例 #15
0
def test_toml_content_append(fs):
    '''Test appending settings to list.'''
    fs.add_real_file(settings_filepath, False)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=settings_filepath)
    cfg.append('/database/ports', 2345)
    assert 2345 in cfg.retrieve('/database/ports')
コード例 #16
0
def test_xml_content_dump(fs):
    '''Test XML content save.'''
    fs.add_real_file(xml_filepath, False)
    cfg = ConfigFile(name='tests', writable=True)
    cfg.load(filepath=xml_filepath)
    cfg.create('/root/test', 'test')
    assert cfg.retrieve('/root/test') == 'test'
コード例 #17
0
def test_toml_content_create(fs):
    '''Test content creation settings.'''
    fs.add_real_file(settings_filepath, False)
    cfg = ConfigFile(name='tests')
    cfg.load(filepath=settings_filepath)
    cfg.create('/test', 'test')
    assert cfg.retrieve('test') == 'test'
コード例 #18
0
ファイル: test_toml.py プロジェクト: kuwv/python-compendium
def test_toml_content_dump(fs):
    '''Test TOML content save.'''
    fs.add_real_file(toml_filepath, False)
    cfg = ConfigFile(name='tests', writable=True)
    cfg.load(filepath=toml_filepath)
    cfg.create('/test', 'test')
    # TODO where is save happening :/
    assert cfg.retrieve('test') == 'test'
コード例 #19
0
ファイル: test_ini.py プロジェクト: kuwv/python-compendium
def test_ini_filepath(fs):
    '''Test TOML filepaths.'''
    fs.add_real_file(ini_filepath)
    cfg = ConfigFile(name='ini',
                     filepath=os.path.join(config_filepath, 'test.ini'))
    assert "{}/test.ini".format(config_filepath) == cfg.filepath
コード例 #20
0
ファイル: config_file.py プロジェクト: kuwv/python-compendium
            f"{type(str(k))}:{str(k)}": show_types(v)
            for k, v in obj.items()
        }

    # convert list
    elif isinstance(obj, list):
        obj = [show_types(x) for x in obj]

    return f"{type(obj)}:{obj}"


basepath = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(basepath, 'example.yaml')
outpath = os.path.join(basepath, 'example-out.yaml')

cfg = ConfigFile(writable=True)
cfg.load(filepath=filepath)

print('settings', cfg)
# print('allowed_roles', cfg.allowed_roles[0])
# print('default_args', cfg.dag.default_args)
# assert 'sre' in cfg.get('/allowed_roles')
# assert 'devops' in cfg.get('/allowed_roles')
# assert 'cloudops' in cfg.get('/allowed_roles')

print('post settings', cfg.data)
cfg.dump(filepath=outpath)

# JSON
json_in = os.path.join(basepath, 'in.json')
json_out = os.path.join(basepath, 'out.json')
コード例 #21
0
'''Provide example for loading dot config file.'''
import os
from compendium.loader import ConfigFile
from compendium.filetypes.ini import IniConfig
from pprint import pprint

basepath = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(basepath, '.example')

assert os.path.exists(filepath) is True
assert os.path.isfile(filepath) is True

cfg = ConfigFile(filepath=filepath, filetype='yaml')
cfg.load()
print('settings', cfg)

assert 'sre' in cfg.retrieve('/allowed_roles')
assert 'devops' in cfg.retrieve('/allowed_roles')
assert 'cloudops' in cfg.retrieve('/allowed_roles')

pypirc_filepath = os.path.join(os.path.expanduser('~'), '.pypirc')

assert os.path.exists(pypirc_filepath) is True
assert os.path.isfile(pypirc_filepath) is True

# pypirc = ConfigFile(pypirc_filepath, filetype='ini')
# pypirc.load()
# print('pypi', type(pypirc))

pypirc = IniConfig()
p = pypirc.load_config(pypirc_filepath)
コード例 #22
0
ファイル: test_yaml.py プロジェクト: kuwv/python-compendium
def test_yaml_filepath(fs):
    '''Test YAML paths.'''
    fs.add_real_file(yaml_filepath)
    cfg = ConfigFile(name='yaml',
                     filepath=os.path.join(config_filepath, 'test.yaml'))
    assert "{}/test.yaml".format(config_filepath) == cfg.filepath