def write_to_file(cfg, filepath): """ Write a configuration to the given file so that it's readable by configparser. """ with open(filepath, mode='w', encoding='utf-8') as f: def printf(s): f.write(s + os.linesep) lastsection = None for prop in cfg.to_properties(): if prop.hidden: continue key, value, doc = (Key(prop.key), prop.value, prop.doc) section, subkey = str(key.head), str(key.tail) if section != lastsection: lastsection = section printf('%s[%s]' % ( os.linesep, section, )) if doc: printf('') lines = util.phrase_to_lines(doc) for line in lines: printf('; %s' % (line, )) printf('%s = %s' % (subkey, value))
def test_phrase_to_lines(): phrase = '''qwertyui9o0 sdfghjk dfghjk dfghj fghjk dfghjk fghj fghj ghjfdkj ahg jkdgf sjkdfhg skjfhg sjkfh sjkd fhgsjd hgf sdjhgf skjg fg hjkfghjk fghjk gfhjk fghj fghjk ghj fghj gfhjk fghj ghj asd''' lines = util.phrase_to_lines(phrase, length=80) assert 60 < len(lines[0]) < 80 assert 60 < len(lines[1]) < 80 assert len(lines[1]) < 80
def test_phrase_to_lines(): phrase = """qwertyui9o0 sdfghjk dfghjk dfghj fghjk dfghjk fghj fghj ghjfdkj ahg jkdgf sjkdfhg skjfhg sjkfh sjkd fhgsjd hgf sdjhgf skjg fg hjkfghjk fghjk gfhjk fghj fghjk ghj fghj gfhjk fghj ghj asd""" lines = util.phrase_to_lines(phrase, length=80) assert 60 < len(lines[0]) < 80 assert 60 < len(lines[1]) < 80 assert len(lines[1]) < 80
def write_to_file(cfg, filepath): """ Write a configuration to the given file so that it's readable by configparser. """ with open(filepath, mode='w', encoding='utf-8') as f: def printf(s): f.write(s + os.linesep) lastsection = None for prop in cfg.to_properties(): if prop.hidden: continue key, value, doc = (Key(prop.key), prop.value, prop.doc) section, subkey = str(key.head), str(key.tail) if section != lastsection: lastsection = section printf('%s[%s]' % (os.linesep, section,)) if doc: printf('') lines = util.phrase_to_lines(doc) for line in lines: printf('; %s' % (line,)) printf('%s = %s' % (subkey, value))