def load_config(file_path, overrides=[]): if not _is_valid(file_path): _fail_and_exit() with open(file_path) as file: config = ConfigDict() # Helps keep track of the "group" that we want to set settings to. current_group = None for line in file.read().splitlines(): line = _clean(line) if not line: continue # Begin parse by looking for any "group" to init config obj with. # -- If not found, proceed with the last detected "group". current_group = _is_a_group(line) or current_group if current_group and _set_group(config, current_group): continue elif not current_group: # At this point, if we don't have a "group" to assign "settings" to, we assume that the config is malformed. _fail_and_exit() override_setting = _is_override_setting(line) if override_setting and _set_override_setting( config, current_group, override_setting, overrides): continue standard_setting = _is_standard_setting(line) if standard_setting and _set_standard_setting( config, current_group, standard_setting): continue # At this point, if we can't recognize the line format, we assume that the config is malformed. _fail_and_exit() return config
def file_dict(tmpdir_factory): f = tmpdir_factory.mktemp('test').join('config.txt') filepath = str(f) content = '''one=unu\ntwo=doi\nthree=trei\nfour=patru\n''' f.write(content) file_dict = ConfigDict(filepath) return file_dict
def test_set_setting(self): config = ConfigDict() group = 'group' key = 'basic_size_limit' value = 26214400 _set_group(config, group) _set_setting(config, group, key, value) self.assertEqual(config.group[key], value)
def test_set_override_setting(self): config = ConfigDict() group = 'group' key = 'path' override = 'production' value = '/srv/var/tmp/' _set_group(config, group) _set_override_setting(config, group, [key, override, value], ['production']) self.assertEqual(config.group[key], value)
def test_set_group(self): config = ConfigDict() group = 'group' result = _set_group(config, group) self.assertTrue(result) self.assertTrue(group in config)