Skip to content

Commit 5042ccd

Browse files
committed
Now incrementing the progress for every single file that has been converted, instead of only for every mod. Still need to add code for incrementing zips, but I don't know how yet. Closes #38.
1 parent 977818f commit 5042ccd

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

Python/convert.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import os, sys, time, shutil, math, zipfile, json, pathlib, webbrowser
1+
import os, sys, time, shutil, math, json, pathlib, webbrowser
22
from pathlib import Path
33
from jsoncomment import JsonComment
44
from playsound import playsound
55

66
from Python import shared_globals as cfg
77
from Python import regex_rules
8-
from Python.zips import create_zips
8+
from Python import zips as zips_py
99
from Python import update_progress
1010

1111

@@ -59,61 +59,53 @@ def check_github_button_clicked_and_exit(clicked_github_button):
5959
def convert():
6060
global output_folder, warnings
6161

62+
print("") # Only prints a newline.
63+
6264
time_start = time.time()
6365

6466
input_folder_path = cfg.sg.user_settings_get_entry("input_folder")
65-
true_input_folder_path = os.path.join(input_folder_path, os.pardir) # TODO: Better variable name.
6667

67-
unzip(input_folder_path)
68+
zips_py.unzip(input_folder_path)
6869

6970
update_progress.set_max_progress(input_folder_path)
7071

7172
for input_subfolder_path, input_subfolders, input_subfiles in os.walk(input_folder_path):
72-
mod_subfolder = get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder_path)
73+
mod_subfolder = get_mod_subfolder(input_folder_path, input_subfolder_path)
7374

7475
mod_subfolder_parts = pathlib.Path(mod_subfolder).parts
76+
7577
if len(mod_subfolder_parts) > 0 and mod_subfolder_parts[0].endswith(".rte"):
76-
output_subfolder = os.path.join(output_folder, mod_subfolder)
7778
try_print_mod_name(mod_subfolder_parts, mod_subfolder)
79+
output_subfolder = os.path.join(output_folder, mod_subfolder)
7880
create_folder(input_subfolder_path, output_subfolder)
7981
process_files(input_subfiles, input_subfolder_path, output_subfolder, input_folder_path)
8082

8183
if cfg.sg.user_settings_get_entry("output_zips"):
82-
create_zips(input_folder_path, output_folder)
84+
zips_py.create_zips(input_folder_path, output_folder)
8385

8486
if len(warnings) > 0:
8587
warnings_popup()
8688

87-
update_progress.progress = 0
88-
update_progress.total_progress = 0
8989
warnings = []
9090

91+
update_progress.increment_progress() # TODO: This is a temporary solution for zipping not being accounted in the progress.
92+
9193
elapsed = math.floor(time.time() - time_start)
9294
if cfg.sg.user_settings_get_entry("play_finish_sound"):
9395
playsound(resource_path("Media/finish.wav"), block=False)
9496
print("Finished in {} {}".format(elapsed, pluralize("second", elapsed)))
9597

9698

97-
def unzip(input_folder_path):
98-
for f in os.listdir(input_folder_path):
99-
zip_path = os.path.join(input_folder_path, f)
100-
if zipfile.is_zipfile(zip_path):
101-
with zipfile.ZipFile(zip_path) as item:
102-
item.extractall(input_folder_path)
103-
os.remove(zip_path)
104-
105-
106-
def get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder_path):
99+
def get_mod_subfolder(input_folder_path, input_subfolder_path):
107100
if input_folder_path.endswith(".rte"):
108-
return os.path.relpath(input_subfolder_path, true_input_folder_path)
101+
return os.path.relpath(input_subfolder_path, os.path.join(input_folder_path, os.pardir))
109102
else:
110103
return os.path.relpath(input_subfolder_path, input_folder_path)
111104

112105

113106
def try_print_mod_name(mod_subfolder_parts, mod_subfolder):
114107
if len(mod_subfolder_parts) == 1:
115108
print("Converting '{}'".format(mod_subfolder))
116-
update_progress.increment_progress()
117109

118110

119111
def create_folder(input_subfolder_path, output_subfolder):
@@ -138,6 +130,7 @@ def process_files(input_subfiles, input_subfolder_path, output_subfolder, input_
138130
else:
139131
shutil.copyfile(input_file_path, output_file_path)
140132

133+
update_progress.increment_progress()
141134

142135
def create_converted_file(input_file_path, output_file_path, input_folder_path):
143136
# try: # TODO: Figure out why this try/except is necessary and why it doesn't check for an error type.
@@ -155,7 +148,6 @@ def create_converted_file(input_file_path, output_file_path, input_folder_path):
155148

156149
for old_str, new_str in warning_rules.items():
157150
if old_str in line:
158-
print(old_str, new_str)
159151
warnings.append("'{}' line {}: {} -> {}".format(file_path, line_number, old_str, new_str))
160152

161153
all_lines_list.append(line)

Python/update_progress.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ def increment_progress():
1414

1515

1616
def set_max_progress(input_folder_path):
17-
global total_progress # TODO: See what happens when this line is removed.
18-
if input_folder_path.endswith(".rte"):
19-
mod_count = 1
20-
else:
21-
mod_count = 0
22-
for mod_name in os.listdir(input_folder_path):
23-
if os.path.isdir(os.path.join(input_folder_path, mod_name)) and mod_name.endswith(".rte"):
24-
mod_count += 1
25-
total_progress = mod_count * 2 if cfg.sg.user_settings_get_entry("output_zips") else mod_count
17+
global progress, total_progress # TODO: See what happens when this line is removed.
18+
19+
progress = 0
20+
total_progress = 0
21+
22+
for input_subfolder_path, input_subfolders, input_subfiles in os.walk(input_folder_path):
23+
for filename in input_subfiles:
24+
total_progress += 1
25+
26+
# TODO: Find a way to see track how much progress the zipping is making.
27+
# if cfg.sg.user_settings_get_entry("output_zips"):
28+
# total_progress = total_progress * 2

Python/zips.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import os, shutil
1+
import os, shutil, zipfile
22
from pathlib import Path
33

44
from Python.update_progress import increment_progress
55

66

7+
def unzip(input_folder_path):
8+
for f in os.listdir(input_folder_path):
9+
zip_path = os.path.join(input_folder_path, f)
10+
if zipfile.is_zipfile(zip_path):
11+
with zipfile.ZipFile(zip_path) as item:
12+
item.extractall(input_folder_path)
13+
os.remove(zip_path)
14+
15+
716
def create_zips(input_folder_path, output_folder):
817
if input_folder_path.endswith(".rte"):
918
create_single_zip(Path(input_folder_path).name, output_folder)
@@ -19,5 +28,4 @@ def create_single_zip(mod_name, output_folder):
1928
print("Zipping '{}'".format(mod_name))
2029
mod_path = os.path.join(output_folder, mod_name)
2130
shutil.make_archive(mod_path.replace(".rte", "-v1.0.rte"), "zip", root_dir=output_folder, base_dir=mod_name)
22-
shutil.rmtree(mod_path)
23-
increment_progress()
31+
shutil.rmtree(mod_path)

0 commit comments

Comments
 (0)