Skip to content

Commit d9dbb60

Browse files
committed
Refactor ini_writer.py
1 parent 34ed50c commit d9dbb60

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

Python/ini_converting/ini_writer.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ def write_converted_ini_cst(parsed_portion, output_folder_path):
44
if isinstance(dict_or_list, dict): # If dict_or_list contains a dictionary of more filenames.
55
write_converted_ini_cst(dict_or_list, output_folder_path / name)
66
else: # If dict_or_list contains a list of the sections of a file.
7-
# pprint.pprint(dict_or_list)
87
with open(str(output_folder_path / name), mode="w") as f:
98
lines = []
109
for section in dict_or_list:
11-
get_lines_from_dicts_recursively(section, lines)
12-
f.write("\n".join(lines))
10+
write_recursively(section, lines)
11+
f.write("".join(lines))
1312

1413

15-
def get_lines_from_dicts_recursively(line_tokens, lines):
16-
# This function loops twice through line_tokens so the lines can be appended in the correct order.
17-
18-
line = ""
19-
for dictionary in line_tokens:
20-
if dictionary["type"] != "children":
21-
line += dictionary["content"]
22-
lines.append(line)
23-
24-
for dictionary in line_tokens:
25-
if dictionary["type"] == "children":
26-
for line_tokens in dictionary["content"]:
27-
get_lines_from_dicts_recursively(line_tokens, lines)
14+
def write_recursively(line_tokens, lines):
15+
for token in line_tokens:
16+
if token["type"] != "children":
17+
lines.append(token["content"])
18+
elif token["type"] == "children":
19+
for line_tokens in token["content"]:
20+
write_recursively(line_tokens, lines)

0 commit comments

Comments
 (0)