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
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
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') == []
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')
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')
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
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
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
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
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
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'
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
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'
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'
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')
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'
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'
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'
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
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')
'''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)
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