def make(glottolog, concepticon): ini = INI() ini.read_dict( {'paths': { 'concepticon': concepticon, 'glottolog': glottolog }}) p = str(tmpdir.join('config.ini')) ini.write(p) return p
def write_config(comment_text, overwrite): lines = comment_text.split("\n") assert lines[1] in (_config_file_str, _proggen_str) if lines[1] == _proggen_str: return "Original configuration was generated programmatically, no configuration to extract." config_text = "\n".join(lines[2:]) p = INI() p.read_string(config_text) filename = p.get("admin", "basename") \ if p.has_option("admin", "basename") else 'beastling' filename = Path(filename + '.conf') if filename.exists() and not overwrite: return "BEASTling configuration file %s already exists! Run beastling with the --overwrite option if you wish to overwrite it.\n" % filename if not filename.parent.exists(): filename.parent.mkdir() p.write(filename) return "Wrote BEASTling configuration file %s.\n" % filename
def test_INI(self): from clldutils.inifile import INI ini = INI() ini.set('äüü', 'äöü', ('ä', 'ö', 'ü')) ini.set('a', 'b', 5) self.assertEqual(ini['a'].getint('b'), 5) ini.set('a', 'c', None) self.assertNotIn('c', ini['a']) self.assertIn('ä\n', ini.write_string()) self.assertEqual(len(ini.getlist('äüü', 'äöü')), 3) tmp = self.tmp_path('test') ini.write(tmp.as_posix()) with tmp.open(encoding='utf8') as fp: res = fp.read() self.assertIn('coding: utf-8', res) ini2 = INI.from_file(tmp) self.assertEqual(ini2.write_string(), ini.write_string())
def write_config(comment_text, overwrite): lines = comment_text.split("\n") lines = [l for l in lines if l] assert lines[1] in (_config_file_str, _proggen_str) if lines[1] == _proggen_str: return "Original configuration was generated programmatically, no configuration to extract." truths = [_do_not_edit_str in line for line in lines] if any(truths): lines = lines[0:truths.index(True)] config_text = "\n".join(lines[2:]) p = INI() p.read_string(config_text) filename = p.get("admin", "basename") \ if p.has_option("admin", "basename") else 'beastling' filename = Path(filename + '.conf') if filename.exists() and not overwrite: return "BEASTling configuration file %s already exists! Run beastling with the --overwrite option if you wish to overwrite it.\n" % filename if not filename.parent.exists(): filename.parent.mkdir() p.write(filename) return "Wrote BEASTling configuration file %s.\n" % filename
def test_INI(tmppath): ini = INI() ini.set('äüü', 'äöü', ('ä', 'ö', 'ü')) ini.set('a', 'b', 5) assert ini['a'].getint('b') == 5 ini.set('a', 'c', None) assert 'c' not in ini['a'] assert 'ä\n' in ini.write_string() assert len(ini.getlist('äüü', 'äöü')) == 3 mt = '- a\n - aa\n - ab\n- b' ini.settext('text', 'multi', mt) tmp = tmppath / 'test' ini.write(tmp.as_posix()) with tmp.open(encoding='utf8') as fp: res = fp.read() assert 'coding: utf-8' in res ini2 = INI.from_file(tmp) assert ini2.gettext('text', 'multi') == mt assert ini2.write_string() == ini.write_string()
def test_existing_config(self): cfg = INI() cfg.read_dict({'section': {'option': '12'}}) cfg.write(self.tmp_path('test.ini')) cfg = Config('test', dir_=self.tmp_path()) self.assertEqual(cfg.get('section', 'option'), '12')