Quick refactor of python tools modules (#1456)

This commit is contained in:
Riccardo Pittau 2025-04-19 11:19:49 +02:00 committed by GitHub
parent 251a3974cf
commit f252f9bb46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 33 additions and 43 deletions

View File

@ -8,9 +8,9 @@ import subprocess
import hashlib import hashlib
from vars import home_dir, msg_file from vars import home_dir, msg_file
from utils import getSettings, log_message from utils import get_settings, log_message
settings = getSettings() settings = get_settings()
storage_path = os.path.expandvars(settings["storagePath"]) storage_path = os.path.expandvars(settings["storagePath"])
# Path for the JSON and target folder from command-line arguments # Path for the JSON and target folder from command-line arguments

View File

@ -1,17 +1,16 @@
import os import os
import json import json
import subprocess
import requests import requests
import zipfile import zipfile
from io import BytesIO from io import BytesIO
import sys import sys
import re from utils import get_settings, log_message
from vars import home_dir, msg_file
from utils import getSettings, log_message
settings = getSettings()
settings = get_settings()
storage_path = os.path.expandvars(settings["storagePath"]) storage_path = os.path.expandvars(settings["storagePath"])
def download_and_extract(output_dir): def download_and_extract(output_dir):
# Fixed path to the JSON file # Fixed path to the JSON file
json_file_path = os.path.join(storage_path, "retrolibrary/cache/missing_systems.json") json_file_path = os.path.join(storage_path, "retrolibrary/cache/missing_systems.json")

View File

@ -1,12 +1,11 @@
import os import os
import json import json
import sys import sys
import re from vars import excluded_systems
import subprocess from utils import get_settings, log_message, clean_name, collect_game_data, get_valid_system_dirs, parse_metadata_file
from vars import home_dir, msg_file, excluded_systems
from utils import getSettings, log_message, clean_name, collect_game_data, get_valid_system_dirs, parse_metadata_file
settings = getSettings()
settings = get_settings()
storage_path = os.path.expandvars(settings["storagePath"]) storage_path = os.path.expandvars(settings["storagePath"])
saves_path = os.path.expandvars(settings["savesPath"]) saves_path = os.path.expandvars(settings["savesPath"])

View File

@ -1,14 +1,11 @@
import os import os
import json import json
import sys import sys
import re from vars import excluded_systems
import subprocess from utils import get_settings, log_message, collect_game_data, get_valid_system_dirs, parse_metadata_file
import hashlib
from vars import home_dir, msg_file, excluded_systems
from utils import getSettings, log_message, clean_name, collect_game_data, get_valid_system_dirs, parse_metadata_file
settings = getSettings() settings = get_settings()
storage_path = os.path.expandvars(settings["storagePath"]) storage_path = os.path.expandvars(settings["storagePath"])

View File

@ -1,13 +1,11 @@
import os import os
import json import json
import sys import sys
import re from vars import excluded_systems
import subprocess from utils import get_settings, log_message, get_valid_system_dirs
from vars import home_dir, msg_file, excluded_systems
from utils import getSettings, log_message, get_valid_system_dirs
settings = getSettings() settings = get_settings()
storage_path = os.path.expandvars(settings["storagePath"]) storage_path = os.path.expandvars(settings["storagePath"])

View File

@ -1,16 +1,18 @@
import os import os
import re import re
import subprocess import subprocess
from vars import home_dir, msg_file from vars import msg_file
def getSettings():
def get_settings():
pattern = re.compile(r'([A-Za-z_][A-Za-z0-9_]*)=(.*)') pattern = re.compile(r'([A-Za-z_][A-Za-z0-9_]*)=(.*)')
user_home = os.path.expanduser("~") user_home = os.path.expanduser("~")
bash_command = "cd $HOME/.config/EmuDeck/backend/ && git rev-parse --abbrev-ref HEAD"
config_file_path = os.path.join(user_home, '.config', 'EmuDeck',
'settings.sh')
if os.name == 'nt': if os.name == 'nt':
bash_command = f"cd {os.path.join(user_home, 'AppData', 'Roaming', 'EmuDeck', 'backend')} && git rev-parse --abbrev-ref HEAD"
config_file_path = os.path.join(user_home, 'AppData', 'Roaming', 'EmuDeck', 'settings.ps1') config_file_path = os.path.join(user_home, 'AppData', 'Roaming', 'EmuDeck', 'settings.ps1')
else:
config_file_path = os.path.join(user_home, '.config' , 'EmuDeck', 'settings.sh')
configuration = {} configuration = {}
@ -23,12 +25,6 @@ def getSettings():
expanded_value = os.path.expandvars(value.replace('"', '').replace("'", "")) expanded_value = os.path.expandvars(value.replace('"', '').replace("'", ""))
configuration[variable] = expanded_value configuration[variable] = expanded_value
# Obtener rama actual del repositorio backend
if os.name == 'nt':
bash_command = f"cd {os.path.join(user_home, 'AppData', 'Roaming', 'EmuDeck', 'backend')} && git rev-parse --abbrev-ref HEAD"
else:
bash_command = "cd $HOME/.config/EmuDeck/backend/ && git rev-parse --abbrev-ref HEAD"
result = subprocess.run(bash_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) result = subprocess.run(bash_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
configuration["branch"] = result.stdout.strip() configuration["branch"] = result.stdout.strip()
@ -36,21 +32,23 @@ def getSettings():
return configuration return configuration
def log_message(message): def log_message(message):
with open(msg_file, "w") as log_file: # "a" to append messages without overwriting with open(msg_file, "w") as log_file: # "a" to append messages without overwriting
log_file.write(message + "\n") log_file.write(message + "\n")
def clean_name(name): def clean_name(name):
name_cleaned = re.sub(r'\(.*?\)', '', name) name_cleaned = re.sub(r'\(.*?\)', '', name)
name_cleaned = re.sub(r'\[.*?\]', '', name_cleaned) name_cleaned = re.sub(r'\[.*?\]', '', name_cleaned)
name_cleaned = name_cleaned.strip().replace(' ', '_').replace('-', '_') name_cleaned = name_cleaned.strip().replace(' ', '_').replace('-', '_')
name_cleaned = re.sub(r'_+', '_', name_cleaned) name_cleaned = re.sub(r'_+', '_', name_cleaned)
name_cleaned = name_cleaned.replace('+', '').replace('&', '').replace('!', '').replace("'", '').replace('.', '').replace('_decrypted','').replace('decrypted','').replace('.ps3', '') name_cleaned = name_cleaned.replace('+', '').replace('&', '').replace('!', '').replace("'", '').replace('.', '').replace('_decrypted', '').replace('decrypted', '').replace('.ps3', '')
name_cleaned_pegasus = name.replace(',_', ',')
name_cleaned = name_cleaned.lower() name_cleaned = name_cleaned.lower()
return name_cleaned return name_cleaned
def collect_game_data(system_dir, extensions, images_path = None):
def collect_game_data(system_dir, extensions, images_path=None):
game_data = [] game_data = []
#PS3 #PS3
@ -169,9 +167,6 @@ def collect_game_data(system_dir, extensions, images_path = None):
"filename": file_path "filename": file_path
} }
game_data.append(game_info) game_data.append(game_info)
else: else:
game_info = { game_info = {
"name": name_cleaned, "name": name_cleaned,
@ -212,6 +207,7 @@ def get_valid_system_dirs(roms_dir, valid_system_dirs):
log_message(f"GGL: Valid system directory found: {full_path}") log_message(f"GGL: Valid system directory found: {full_path}")
return valid_system_dirs return valid_system_dirs
def parse_metadata_file(metadata_path): def parse_metadata_file(metadata_path):
if not os.path.exists(metadata_path): if not os.path.exists(metadata_path):
raise FileNotFoundError(f"Metadata file not found: {metadata_path}") raise FileNotFoundError(f"Metadata file not found: {metadata_path}")

View File

@ -1,9 +1,10 @@
import os import os
home_dir = os.environ.get("HOME")
msg_file = os.path.join(home_dir, ".config/EmuDeck/logs/msg.log")
if os.name == 'nt': if os.name == 'nt':
home_dir = os.environ.get("USERPROFILE") home_dir = os.environ.get("USERPROFILE")
msg_file = os.path.join(home_dir, 'AppData', 'Roaming', 'EmuDeck', 'logs/msg.log') msg_file = os.path.join(home_dir, 'AppData', 'Roaming', 'EmuDeck', 'logs/msg.log')
else:
home_dir = os.environ.get("HOME")
msg_file = os.path.join(home_dir, ".config/EmuDeck/logs/msg.log")
excluded_systems = ["/model2", "/genesiswide", "/mame", "/emulators", "/desktop", "/sneswide"] excluded_systems = ["/model2", "/genesiswide", "/mame", "/emulators", "/desktop", "/sneswide"]