def test_unsplit_lines(): lines = list(unsplit_lines(SPLIT_LINES.splitlines())) assert len(lines) == 3 assert lines[0] == 'Line one' assert lines[ 1] == 'Line two part 1 line two part 2 line two part 3' assert lines[2] == 'Line three' lines = list(unsplit_lines(SPLIT_LINES_2.splitlines(), cont_char='^')) assert len(lines) == 3 assert lines[0] == 'Line one' assert lines[ 1] == 'Line two part 1 line two part 2 line two part 3' assert lines[2] == 'Line three' # test continuation on last line # Test keeping continuation character on line lines = list( unsplit_lines(SPLIT_LINES_3.splitlines(), cont_char=',', keep_cont_char=True)) assert len(lines) == 4 assert lines[0] == '' assert lines[ 1] == 'web.default_taskmaster_tasks = RHN::Task::SessionCleanup, RHN::Task::ErrataQueue, RHN::Task::ErrataEngine, RHN::Task::DailySummary, RHN::Task::SummaryPopulation, RHN::Task::RHNProc, RHN::Task::PackageCleanup' assert lines[2] == '' assert lines[3] == 'db_host ='
def parse_content(self, content): self.data = {} for line in unsplit_lines(get_active_lines(content), ',', keep_cont_char=True): if '=' in line: k, v = [i.strip() for i in line.split('=', 1)] self.data[k] = [i.strip() for i in v.split(',')] if ',' in v else v
def parse_systemd_ini(content): """Function to parse config format file, the result format is dictionary""" Config = cp(dict_type=MultiOrderedDict) Config.optionxform = str Config.readfp(StringIO('\n'.join(content))) dict_all = {} for section in Config.sections(): dict_section = {} for option in Config.options(section): value = Config.get(section, option).splitlines() value = list(unsplit_lines(value)) # If the len of value is 1, then set the value as string dict_section[option] = value[0] if len( value) == 1 else value if len(value) > 1 else '' dict_all[section] = dict_section return dict_all