Skip to content

Commit

Permalink
Transmit more information via GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-l-martins committed Dec 5, 2024
1 parent e59cfca commit 148a2b2
Showing 1 changed file with 118 additions and 23 deletions.
141 changes: 118 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from tkinter import filedialog, ttk, font
import threading


# Define the stop_thread event
stop_thread = threading.Event()

Expand Down Expand Up @@ -110,12 +111,24 @@ def process_gsm_folder(input_folder_path: str, output_folder_path: str, progress
all_gsm_gsba_files = glob.glob(os.path.join(input_folder_path, '*.gsba'))
logging.info(f'Processing GSM/.gsba files: {all_gsm_gsba_files}')

progress_info_label['text'] = "Starting to process..."

processing_error_flag = False

if progress_bar:
progress_bar['maximum'] = len(all_gsm_gsba_files)

#Restart counter, if "Process Backups" button is pressed again
warning_info_label['text'] = ""
i=0
progress_bar['value'] = 0
progress_label['text'] = f"0% (0/{progress_bar['maximum']})"
previous_backup_processed = None

for i, gsm_gsba_file in enumerate(all_gsm_gsba_files):
if previous_backup_processed:
progress_info_label['text'] = f"Finished processing: {previous_backup_processed}\n Now processing: {os.path.basename(gsm_gsba_file)}"

if stop_thread and stop_thread.is_set():
break
try:
Expand Down Expand Up @@ -154,12 +167,13 @@ def process_gsm_folder(input_folder_path: str, output_folder_path: str, progress
progress_bar.step(1)
progress_bar.update_idletasks()
progress_label['text'] = f"{int(progress_bar['value'] / progress_bar['maximum'] * 100)}% ({i+1}/{progress_bar['maximum']})"
progress_info_label['text'] = f"Finished processing file: {os.path.basename(gsm_gsba_file)}"
#progress_info_label['text'] = f"Finished processing file: {os.path.basename(gsm_gsba_file)}"
previous_backup_processed = os.path.basename(gsm_gsba_file)

if progress_bar:
progress_bar['value'] = progress_bar['maximum']
progress_label['text'] = f"100% ({progress_bar['maximum']}/{progress_bar['maximum']} of .gsba files found)"
progress_info_label['text'] = "Processing complete"
progress_info_label['text'] = "Processing complete!"
if progress_bar['maximum'] == 0:
warning_info_label['text'] = "Warning: No GSM/.gsba files found in the input folder!"
elif processing_error_flag:
Expand All @@ -169,8 +183,10 @@ def process_gsm_folder(input_folder_path: str, output_folder_path: str, progress

def process_ludusavi_folder(input_folder_path: str, output_folder_path: str, progress_bar=None, stop_thread=None):

processing_error_flag = False
progress_info_label['text'] = "Starting to process..."

processing_error_flag = False

# Detect Ludusavi folders, which are folders that have a 'mapping.yaml' file in them
ludusavi_folders = []
registry_entries = []
Expand All @@ -185,7 +201,18 @@ def process_ludusavi_folder(input_folder_path: str, output_folder_path: str, pro
if progress_bar:
progress_bar['maximum'] = len(ludusavi_folders)
registry_yaml = None

#Restart counter, if "Process Backups" button is pressed again
warning_info_label['text'] = ""
i=0
progress_bar['value'] = 0
progress_label['text'] = f"0% (0/{progress_bar['maximum']})"
previous_backup_processed = None

for i, ludusavi_folder in enumerate(ludusavi_folders):
if previous_backup_processed:
progress_info_label['text'] = f"Finished processing: {previous_backup_processed}\n Now processing: {os.path.basename(ludusavi_folder)}"

if stop_thread and stop_thread.is_set():
break
#Check ludusavi_folder
Expand All @@ -205,44 +232,112 @@ def process_ludusavi_folder(input_folder_path: str, output_folder_path: str, pro
# Read as utf-16 to avoid encoding issues
with open(os.path.join(ludusavi_folder, 'mapping.yaml'), 'r', encoding="utf-8") as mapping_file:
mapping_yaml = mapping_file.read()
#yaml_contents = yaml.load(mapping_file)

with open(os.path.join(ludusavi_folder, 'mapping.yaml'), 'r') as mapping_file:
mapping_yaml_lines = mapping_file.readlines()
#logging.info(f"mapping.yaml content: {mapping_yaml}")

# Process yaml contents to string
yaml_contents = yaml.safe_load(mapping_yaml)
if yaml_contents and registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path, registry_yaml) #, ludusavi_folder)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1], gsm_info_xml[2])
elif yaml_contents and not registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1], gsm_info_xml[2])
else:
processing_error_flag = True
logging.error(f"Error parsing YAML content: {mapping_yaml}")
with open(os.path.join(output_folder_path, "Failed to process.log"), 'a') as failed_log:
failed_log.write(f"ERROR: Failed to process {os.path.basename(ludusavi_folder)} because the mapping.yaml file is missing, empty or corrupted.\n")
#Count the number of times '- name' appears in the mapping.yaml file
backups_count = mapping_yaml.count('- name')

if backups_count == 1:
# Process yaml contents to string
yaml_contents = yaml.safe_load(mapping_yaml)
if yaml_contents and registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path, registry_yaml) #, ludusavi_folder)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1],
gsm_info_xml[2], gsm_info_xml[3])
elif yaml_contents and not registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1],
gsm_info_xml[2], gsm_info_xml[3])
else:
processing_error_flag = True
logging.error(f"Error parsing YAML content: {mapping_yaml}")
with open(os.path.join(output_folder_path, "Failed to process.log"), 'a') as failed_log:
failed_log.write(f"ERROR: Failed to process {os.path.basename(ludusavi_folder)} because the mapping.yaml file is missing, empty or corrupted.\n")

continue
elif backups_count > 1:
start_index = -1
end_index = -1
list_of_backup_slices = []

for j in range(len(mapping_yaml_lines)):
if mapping_yaml_lines[j].lstrip().startswith("- name"):
start_index = j
if mapping_yaml_lines[j].lstrip().startswith("children") and start_index != -1:
end_index = j
if start_index != -1 and end_index != -1:
list_of_backup_slices.append((start_index, end_index))
start_index = -1
end_index = -1

continue


if len(list_of_backup_slices) != backups_count:
#Throw error
processing_error_flag = True
#logging.error(f"Error parsing YAML content: {mapping_yaml}")
with open(os.path.join(output_folder_path, "Failed to process.log"), 'a') as failed_log:
failed_log.write(f"ERROR: Failed to process {os.path.basename(ludusavi_folder)} because the mapping.yaml file is missing, empty or corrupted.\n")
break

for k in range(len(list_of_backup_slices)):
partial_mapping_yaml = mapping_yaml_lines[:(list_of_backup_slices[0][0])] + mapping_yaml_lines[list_of_backup_slices[k][0]:(list_of_backup_slices[k][1]+1)]
#List to single string
#partial_mapping_yaml = '\n'.join(partial_mapping_yaml)

with open(os.path.join(ludusavi_folder, f'temp_partial_mapping_{k}.yaml'), 'w', encoding="utf-8") as partial_mapping_file:
#partial_mapping_file.write(partial_mapping_yaml)
partial_mapping_file.writelines(partial_mapping_yaml)
#yaml.dump(partial_mapping_yaml, partial_mapping_file, default_flow_style=False, explicit_start=True)
#partial_mapping_yaml_as_stream = io.StringIO(partial_mapping_yaml)
# Process yaml contents to string

with open(os.path.join(ludusavi_folder, f'temp_partial_mapping_{k}.yaml'), 'r', encoding="utf-8") as partial_mapping_file:
partial_yaml_read = partial_mapping_file.read()
yaml_contents = yaml.safe_load(partial_yaml_read)
#yaml_contents = yaml.load(partial_mapping_file)

#Delete the temporary auxiliary file
os.remove(os.path.join(ludusavi_folder, f'temp_partial_mapping_{k}.yaml'))

if yaml_contents and registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path, registry_yaml)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1],
gsm_info_xml[2], gsm_info_xml[3])
elif yaml_contents and not registry_yaml:
gsm_info_xml = create_gsm_info_xml_mapping(yaml_contents, output_folder_path)
move_files_to_gsba_based_on_gsm_xml(input_folder_path, gsm_info_xml[0], gsm_info_xml[1],
gsm_info_xml[2], gsm_info_xml[3])
else:
processing_error_flag = True
logging.error(f"Error parsing YAML content: {partial_mapping_yaml}")
with open(os.path.join(output_folder_path, "Failed to process.log"), 'a') as failed_log:
failed_log.write(f"ERROR: Failed to process {os.path.basename(ludusavi_folder)} because the mapping.yaml file is missing, empty or corrupted.\n")

continue

if progress_bar:
progress_bar.step(1)
progress_bar.update_idletasks()
progress_label['text'] = f"{int(progress_bar['value'] / progress_bar['maximum'] * 100)}% ({i+1}/{progress_bar['maximum']})"
progress_info_label['text'] = f"Finished processing folder: {os.path.basename(ludusavi_folder)}"
#progress_info_label['text'] = f"Finished processing folder: {os.path.basename(ludusavi_folder)}"
previous_backup_processed = os.path.basename(ludusavi_folder)

if progress_bar:
progress_bar['value'] = progress_bar['maximum']
progress_label['text'] = f"100% ({progress_bar['maximum']}/{progress_bar['maximum']} of Ludusavi folders found)"
progress_info_label['text'] = "Processing complete"
progress_info_label['text'] = "Processing complete!"
if progress_bar['maximum'] == 0:
warning_info_label['text'] = "Warning: No Ludusavi folders found in the input folder!"
elif processing_error_flag:
warning_info_label['text'] = "Error: Some files failed to process. Check the 'Failed to process.log' file, in the ouput folder, for more information."
logging.error("Some files failed to process. Check the 'Failed to process.log' file, in the ouput folder, for more information.")







def main():
parser = argparse.ArgumentParser(description='Process GameSave Manager backup files and generate Ludusavi mappings.')
Expand Down

0 comments on commit 148a2b2

Please sign in to comment.