Skip to content

Commit

Permalink
move_roms_worker implementation v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bazzu85 committed Sep 20, 2023
1 parent 109fc6d commit 2e87920
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Create exe.bat
build
dist
*.spec
test
22 changes: 16 additions & 6 deletions gui/move_roms_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@
import src.global_variables as global_variables
import src.configuration_manager as configuration_manager
from obj.move_tracing import Move_Tracing
import workers.move_files_worker as move_files_worker
import workers.move_roms_worker as move_roms_worker

show_move_roms_to_folder_elements = False
show_move_roms_to_subfolder_elements = False
show_preview = False
enable_ui_elements = True

def create_move_roms_section():
global show_move_roms_to_subfolder_elements
global show_move_roms_to_folder_elements
global show_preview
global enable_ui_elements
global_variables.logger.debug(inspect.currentframe().f_code.co_name)
with ui.card().classes('w-full items-center no-shadow'):
with ui.row().classes('w-full items-center'):
global_variables.ui_move_roms_choice_radio = ui.radio({1: 'Move roms to subfolders based on rom name', 2: 'Move roms to folder'}, on_change=check_move_roms_choice).props('inline')
global_variables.ui_move_roms_choice_radio = ui.radio({global_variables.MOVE_ROMS_TO_SUBFOLDER: 'Move roms to subfolders based on rom name', global_variables.MOVE_ROMS_TO_FOLDER: 'Move roms to folder'}, on_change=check_move_roms_choice).props('inline')
with ui.row().classes('w-full items-center'):
global_variables.ui_move_roms_source_path_input = ui.input(label='Rom file location',
validation={
'Insert something': lambda value: value != '' ,
'Path not valid': lambda value: Path(value).is_dir()
}
).classes('w-full')
with ui.row().classes('w-full items-center').bind_visibility(globals(), 'show_move_roms_to_subfolder_elements'):
global_variables.ui_move_roms_use_same_folder_for_multidisc_switch = ui.switch("Use the same folder for multidisc roms?")
with ui.row().classes('w-full items-center').bind_visibility(globals(), 'show_move_roms_to_folder_elements'):
global_variables.ui_move_roms_use_different_folder_switch = ui.switch("Use as destination folder a custom folder?")
global_variables.ui_move_roms_destination_path_input = ui.input(label='Destination file location',
Expand All @@ -53,7 +57,10 @@ def apply_bindings():
global_variables.ui_move_roms_source_path_input.bind_value(global_variables.user_data.move_roms, 'source_path')
global_variables.ui_move_roms_source_path_input.bind_enabled_from(globals(), 'enable_ui_elements')

global_variables.ui_move_roms_use_different_folder_switch.bind_value(global_variables.user_data.move_roms, 'use_same_folder')
global_variables.ui_move_roms_use_same_folder_for_multidisc_switch.bind_value(global_variables.user_data.move_roms, 'use_same_folder_for_multidisc')
global_variables.ui_move_roms_use_same_folder_for_multidisc_switch.bind_enabled_from(globals(), 'enable_ui_elements')

global_variables.ui_move_roms_use_different_folder_switch.bind_value(global_variables.user_data.move_roms, 'use_different_folder')
global_variables.ui_move_roms_use_different_folder_switch.bind_enabled_from(globals(), 'enable_ui_elements')

global_variables.ui_move_roms_destination_path_input.bind_value(global_variables.user_data.move_roms, 'destination_path')
Expand All @@ -66,11 +73,14 @@ def apply_bindings():
global_variables.ui_move_roms_run_button.bind_visibility_from(globals(), 'show_preview')

def check_move_roms_choice():
global show_move_roms_to_subfolder_elements
global show_move_roms_to_folder_elements
global_variables.logger.debug(inspect.currentframe().f_code.co_name)
if global_variables.ui_move_roms_choice_radio.value == 2:
if global_variables.ui_move_roms_choice_radio.value == global_variables.MOVE_ROMS_TO_FOLDER:
show_move_roms_to_subfolder_elements = False
show_move_roms_to_folder_elements = True
elif global_variables.ui_move_roms_choice_radio.value == 1:
elif global_variables.ui_move_roms_choice_radio.value == global_variables.MOVE_ROMS_TO_SUBFOLDER:
show_move_roms_to_subfolder_elements = True
show_move_roms_to_folder_elements = False


Expand Down Expand Up @@ -166,7 +176,7 @@ async def run():

# add the spinner to apply the ui enable change from enable_ui_elements and show the loading animation
spinner = ui.spinner('dots', size='xl')
moved_roms = await asyncio.to_thread(M3U_files_worker.generate_M3U)
moved_roms = await asyncio.to_thread(move_roms_worker.generate_M3U)

if moved_roms > 0:
message = 'Moved ' + str(moved_roms) + ' roms. Check log for details'
Expand Down
6 changes: 3 additions & 3 deletions obj/m3u_tracing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class M3U_tracing():
def __init__(self, M3U_path, M3U_file_list):
self.M3U_path = M3U_path
self.M3U_file_list = M3U_file_list
def __init__(self, m3u_path, m3u_file_list):
self.M3U_path = m3u_path
self.M3U_file_list = m3u_file_list

6 changes: 3 additions & 3 deletions obj/move_tracing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Move_Tracing():
def __init__(self):
self.source = ''
self.destination = ''
def __init__(self, source, destination):
self.source = source
self.destination = destination

5 changes: 4 additions & 1 deletion obj/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ def __init__(self,
class Move_Roms():
def __init__(self,
choice,
source_path,
source_path,
use_same_folder_for_multidisc,
use_different_folder,
destination_path,
):
self.choice = choice
self.source_path = source_path
self.use_same_folder_for_multidisc = use_same_folder_for_multidisc
self.use_different_folder = use_different_folder
self.destination_path = destination_path

Expand All @@ -37,6 +39,7 @@ def return_default_user_data():
move_roms= Move_Roms(
choice=1,
source_path=str(Path.cwd()),
use_same_folder_for_multidisc=False,
use_different_folder= False,
destination_path= '',
),
Expand Down
2 changes: 1 addition & 1 deletion src/configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def read_configuration():
message = 'Configuration file missing. A default file is been created at ' + global_variables.configuration_file
print(message)
global_variables.logger.info(message)
sys.exit(-1)
return

# if configuration file exist, read it from file
with open(file=global_variables.configuration_file, mode='r') as file:
Expand Down
3 changes: 3 additions & 0 deletions src/global_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
ui_config_extensions_for_M3U_dialog: ui.dialog

ui_move_roms_choice_radio: ui.radio
MOVE_ROMS_TO_SUBFOLDER = 1
MOVE_ROMS_TO_FOLDER = 2
ui_move_roms_source_path_input: ui.input
ui_move_roms_use_same_folder_for_multidisc_switch: ui.switch
ui_move_roms_use_different_folder_switch: ui.switch
ui_move_roms_destination_path_input: ui.input
ui_move_roms_preview_button: ui.button
Expand Down
18 changes: 9 additions & 9 deletions workers/m3u_files_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def generate_preview():
root = str(Path(root))
global_variables.logger.debug('Working on folder ' + root)

M3U_file_list = []
m3u_file_list = []

for file in files:
global_variables.logger.debug('Working on file ' + str(file))
Expand All @@ -40,17 +40,17 @@ def generate_preview():
# using a unique folder for the M3U files take to the full rom path in destination file
if global_variables.user_data.create_m3u.use_centralized_folder:
path_to_append = os.path.join(str(Path(root)), file)
M3U_file_list.append(path_to_append)
m3u_file_list.append(path_to_append)
else:
M3U_file_list.append(file)
m3u_file_list.append(file)
else:
global_variables.logger.debug('Item is not ok [multidisk pattern]')
continue

M3U_file_list = sorted(M3U_file_list)
m3u_file_list = sorted(m3u_file_list)
# if for the folder we found some files for the M3U playlist, trace it
if len(M3U_file_list) > 0:
if len(M3U_file_list) == 1:
if len(m3u_file_list) > 0:
if len(m3u_file_list) == 1:
global_variables.logger.debug('Only 1 disk found. Not creating m3u file')
continue

Expand All @@ -66,12 +66,12 @@ def generate_preview():
global_variables.logger.debug(str(Path(M3U_path).name) + ' already exist. Not overwriting')
continue

global_variables.logger.debug('Adding ' + str(M3U_file_list) + ' to M3U file ' + str(M3U_path))
global_variables.logger.debug('Adding ' + str(m3u_file_list) + ' to M3U file ' + str(M3U_path))

global_variables.m3u_tracing_list.append(
M3U_tracing(
M3U_path=M3U_path,
M3U_file_list=M3U_file_list
m3u_path=M3U_path,
m3u_file_list=m3u_file_list
)
)

Expand Down
140 changes: 140 additions & 0 deletions workers/move_roms_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# standard and external libraries
import inspect
import os
import re
import time
from nicegui import ui
from pathlib import Path

# project libraries
import src.global_variables as global_variables
from obj.move_tracing import Move_Tracing

def generate_preview():
global_variables.logger.debug(inspect.currentframe().f_code.co_name)

global_variables.logger.debug('Working on ' + global_variables.user_data.move_roms.source_path + ' path')

if global_variables.user_data.move_roms.choice == global_variables.MOVE_ROMS_TO_SUBFOLDER:
generate_preview_to_subfolder()
elif global_variables.user_data.move_roms.choice == global_variables.MOVE_ROMS_TO_FOLDER:
generate_preview_to_folder()

def generate_preview_to_subfolder():
global_variables.logger.debug(inspect.currentframe().f_code.co_name)

global_variables.logger.debug('Generating preview moves to subfolders')

# using the os.walk instead of rglob because return in an entry the folder and all it's files
for root, folders, files in os.walk(global_variables.user_data.move_roms.source_path):
if len(files) == 0:
continue

root = str(Path(root))
global_variables.logger.debug('Working on folder ' + root)

for file in files:
global_variables.logger.debug('Working on file ' + str(file))

# check if the current file is allowed (extension)
if not check_extesion(file):
global_variables.logger.debug('Item is not ok [extension not allowed]')
continue

if (re.search(global_variables.regex_multi_disc, file, re.IGNORECASE)) and global_variables.user_data.move_roms.use_same_folder_for_multidisc:
generate_preview_to_subfolder_multidisc(file, root)
else:
generate_preview_to_subfolder_singledisc(file, root)

def generate_preview_to_subfolder_multidisc(file, root):
global_variables.logger.debug(inspect.currentframe().f_code.co_name)

cleanedFileName = clean_file_name(file)

regexResult = re.split(global_variables.regex_multi_disc, cleanedFileName, re.IGNORECASE)
global_variables.logger.debug('Check multidisk regex result: ' + str(regexResult))

# starts composing the destination folder
global_variables.logger.debug('Folder calculated before loop is: ' + destination_folder)

calculated_folder = ''
for item in regexResult:

if re.search(global_variables.regex_multi_disc, item, re.IGNORECASE):
global_variables.logger.debug('Multidisc pattern found in: ' + item + '. Breaking loop')
break
else:
global_variables.logger.debug('Multidisc pattern not found in: ' + item + '. Appending')
calculated_folder += item

destination_folder = os.path.join(str(Path(global_variables.user_data.move_roms.source_path)), calculated_folder)
destination_folder = destination_folder.rstrip()

global_variables.logger.debug('New folder calculated after loop is: ' + destination_folder)

if (Path(root) != Path(destination_folder)):
global_variables.move_roms_tracing_list.append(
Move_Tracing(
source=os.path.join(root, file),
destination=destination_folder
)
)

def generate_M3U():
global_variables.logger.debug(inspect.currentframe().f_code.co_name)

created_m3u_files = 0
for item in global_variables.m3u_tracing_list:
item: M3U_tracing
if Path(item.M3U_path).exists() and Path(item.M3U_path).is_file() and not global_variables.user_data.create_m3u.overwrite:
global_variables.logger.info('M3U file ' + Path(item.M3U_path).name + ' already created and overwrite not selected. Skipping')
continue

with open(file=item.M3U_path, mode="w") as file:
count = 0
for element in item.M3U_file_list:
if count != 0:
file.write("\n")
file.write(element)
count = count + 1
file.close

global_variables.logger.info('Created M3U file ' + str(Path(item.M3U_path)) + ' with this discs into: ' + str(item.M3U_file_list))
created_m3u_files += 1

return created_m3u_files


def check_extesion(file):
global_variables.logger.debug(inspect.currentframe().f_code.co_name)
file_extension = Path(file).suffix
foundExtension = False

for extension in global_variables.configuration.extensions_for_file_move:
if extension.upper() == file_extension.upper():
foundExtension = True
break

if foundExtension:
return True
else:
return False

def clean_file_name(file):
global_variables.logger.debug(inspect.currentframe().f_code.co_name)

global_variables.logger.debug("Checking if there's some text to remove from filename " + file)

cleaned_file = ''

regexResult = re.split(global_variables.regex_string_to_remove_for_destination_folder, file, re.IGNORECASE)
for item in regexResult:
if re.search(global_variables.regex_string_to_remove_for_destination_folder, item, re.IGNORECASE):
pass
else:
cleaned_file += item

global_variables.logger.debug('Cleaned file name ' + cleaned_file)
return cleaned_file


0 comments on commit 2e87920

Please sign in to comment.