Skip to content

Commit 2d346d7

Browse files
committed
Add and use get_output_file_path_from_input_file_path() for get_ini_cst()
1 parent d7f676b commit 2d346d7

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

Python/convert.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def convert():
4242

4343
converter_walk(input_folder_path, output_folder_path)
4444

45-
ini_cst = ini_cst_builder.get_ini_cst(input_folder_path, input_folder_path)
45+
ini_cst = ini_cst_builder.get_ini_cst(input_folder_path, output_folder_path, input_folder_path)
4646
ini_rules.apply_rules_on_ini_cst(ini_cst)
4747
ini_writer.write_converted_ini_cst(ini_cst, Path(output_folder_path))
4848

@@ -63,7 +63,7 @@ def converter_walk(input_folder_path, output_folder_path):
6363
for input_subfolder_path, input_subfolders, input_subfiles in os.walk(input_folder_path):
6464
relative_subfolder = utils.get_relative_subfolder(input_folder_path, input_subfolder_path)
6565

66-
if is_mod_folder(relative_subfolder):
66+
if utils.is_mod_folder(relative_subfolder):
6767
warnings.clear_mod_warnings()
6868

6969
if utils.is_mod_folder_or_subfolder(relative_subfolder):
@@ -72,12 +72,6 @@ def converter_walk(input_folder_path, output_folder_path):
7272
process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path, output_folder_path)
7373

7474

75-
def is_mod_folder(relative_subfolder):
76-
# If it isn't the input folder and if it's the rte folder.
77-
mod_subfolder_parts = utils.get_mod_subfolder_parts(relative_subfolder)
78-
return len(mod_subfolder_parts) == 1 and mod_subfolder_parts[0].endswith(".rte")
79-
80-
8175
def create_folder(input_subfolder_path, output_subfolder):
8276
try:
8377
os.makedirs(output_subfolder)

Python/ini_converting/ini_cst_builder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from Python.ini_converting import ini_parser
77

88

9-
def get_ini_cst(input_folder_path, subfolder_path):
9+
def get_ini_cst(input_folder_path, output_folder_path, subfolder_path):
1010
"""
1111
The "cst" in this function's name stands for Concrete Syntax Tree.
1212
"""
@@ -19,9 +19,10 @@ def get_ini_cst(input_folder_path, subfolder_path):
1919
if not utils.is_mod_folder_or_subfolder(relative_subfolder): # TODO: Remove this once CCCP has a Mods folder that can be iterated over.
2020
continue
2121
elif p.is_file() and p.suffix == ".ini" and p.stem != "desktop": # Skip the desktop.ini Windows metadata file.
22-
tokens = ini_tokenizer.get_tokens(str(p))
22+
output_file_path = utils.get_output_file_path_from_input_file_path(input_folder_path, output_folder_path, p)
23+
tokens = ini_tokenizer.get_tokens(output_file_path)
2324
parsed_portion[name] = ini_parser.get_parsed_tokens(tokens)
2425
elif p.is_dir():
25-
parsed_portion[name] = get_ini_cst(input_folder_path, str(p))
26+
parsed_portion[name] = get_ini_cst(input_folder_path, output_folder_path, str(p))
2627

2728
return parsed_portion

Python/ini_converting/ini_writer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
def write_converted_ini_cst(parsed_portion, output_folder_path):
2-
# pprint.pprint(parsed_portion)
32
for name, dict_or_list in parsed_portion.items():
43
if isinstance(dict_or_list, dict): # If dict_or_list contains a dictionary of more filenames.
54
write_converted_ini_cst(dict_or_list, output_folder_path / name)
6-
else: # If dict_or_list contains a list of the sections of a file.
5+
else: # Else dict_or_list contains a list of the sections of a file.
76
with open(str(output_folder_path / name), mode="w") as f:
87
lines = []
98
for section in dict_or_list:
@@ -17,4 +16,4 @@ def write_recursively(line_tokens, lines):
1716
lines.append(token["content"])
1817
elif token["type"] == "children":
1918
for line_tokens in token["content"]:
20-
write_recursively(line_tokens, lines)
19+
write_recursively(line_tokens, lines)

Python/utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ def get_relative_subfolder(input_folder_path, input_subfolder_path):
1717
return os.path.relpath(input_subfolder_path, os.path.join(input_folder_path, os.pardir) if input_folder_path.endswith(".rte") else input_folder_path)
1818

1919

20-
def is_mod_folder_or_subfolder(mod_subfolder):
21-
mod_subfolder_parts = get_mod_subfolder_parts(mod_subfolder)
22-
# If it isn't the input folder and if it's (in) the rte folder.
23-
return len(mod_subfolder_parts) > 0 and mod_subfolder_parts[0].endswith(".rte")
20+
def is_mod_folder(path):
21+
path_parts = Path(path).parts
22+
# If it is a folder inside of the input folder and if it is the mod folder.
23+
return len(path_parts) == 1 and path_parts[0].endswith(".rte")
2424

2525

26-
def get_mod_subfolder_parts(mod_subfolder):
27-
return Path(mod_subfolder).parts
26+
def is_mod_folder_or_subfolder(path):
27+
path_parts = Path(path).parts
28+
# If it is a folder inside of the input folder and if it is the mod folder or is inside of it.
29+
return len(path_parts) >= 1 and path_parts[0].endswith(".rte")
30+
31+
32+
def get_output_file_path_from_input_file_path(input_folder_path, output_folder_path, p):
33+
relative = p.relative_to(input_folder_path)
34+
return str(output_folder_path / relative)

0 commit comments

Comments
 (0)