Toolbox for analyzing and editing pkg application files for psp,ps3, ps4 and ps5, includes the most useful functions you might need.
| 1 | import os |
| 2 | import json |
| 3 | from pathlib import Path |
| 4 | import logging |
| 5 | |
| 6 | class SettingsManager: |
| 7 | """Gestisce il caricamento, salvataggio e accesso alle impostazioni dell'applicazione""" |
| 8 | |
| 9 | DEFAULT_SETTINGS = { |
| 10 | "appearance": { |
| 11 | "theme": "Light", |
| 12 | "night_mode": False, |
| 13 | "font_family": "Arial", |
| 14 | "font_size": 12, |
| 15 | "colors": { |
| 16 | "background": "#ffffff", |
| 17 | "text": "#000000", |
| 18 | "accent": "#3498db" |
| 19 | } |
| 20 | }, |
| 21 | "language": { |
| 22 | "current": "en", |
| 23 | "available": ["en", "it", "es", "fr", "de", "ja"] |
| 24 | }, |
| 25 | "behavior": { |
| 26 | "auto_expand": True, |
| 27 | "show_hidden": False, |
| 28 | "confirm_exit": True, |
| 29 | "auto_save": True |
| 30 | }, |
| 31 | "paths": { |
| 32 | "output": "", |
| 33 | "temp": "", |
| 34 | "last_pkg": "", |
| 35 | "recent_files": [] |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | def __init__(self): |
| 40 | """Inizializza il gestore delle impostazioni""" |
| 41 | self._settings = self.DEFAULT_SETTINGS.copy() |
| 42 | self._config_dir = os.path.join(str(Path.home()), ".pkgtoolbox") |
| 43 | self._config_file = os.path.join(self._config_dir, "settings.json") |
| 44 | |
| 45 | # Assicurati che la directory di configurazione esista |
| 46 | os.makedirs(self._config_dir, exist_ok=True) |
| 47 | |
| 48 | # Carica o crea le impostazioni |
| 49 | if os.path.exists(self._config_file): |
| 50 | self.load() |
| 51 | else: |
| 52 | # Se il file non esiste, usa Arial come font predefinito |
| 53 | self._settings["appearance"]["font_family"] = "Arial" |
| 54 | self.save() |
| 55 | logging.info("Created new settings file with defaults") |
| 56 | |
| 57 | def load(self): |
| 58 | """Carica le impostazioni dal file""" |
| 59 | try: |
| 60 | with open(self._config_file, 'r', encoding='utf-8') as f: |
| 61 | stored_settings = json.load(f) |
| 62 | self._settings = self._merge_settings(self.DEFAULT_SETTINGS, stored_settings) |
| 63 | logging.info("Settings loaded successfully") |
| 64 | except Exception as e: |
| 65 | logging.error(f"Failed to load settings: {e}") |
| 66 | self._settings = self.DEFAULT_SETTINGS.copy() |
| 67 | self.save() |
| 68 | |
| 69 | def save(self): |
| 70 | """Salva le impostazioni nel file""" |
| 71 | try: |
| 72 | os.makedirs(self._config_dir, exist_ok=True) |
| 73 | with open(self._config_file, 'w', encoding='utf-8') as f: |
| 74 | json.dump(self._settings, f, indent=4, ensure_ascii=False) |
| 75 | logging.info("Settings saved successfully") |
| 76 | except Exception as e: |
| 77 | logging.error(f"Failed to save settings: {e}") |
| 78 | |
| 79 | def get(self, key_path: str, default=None): |
| 80 | """Ottiene un valore dalle impostazioni usando un percorso con punti""" |
| 81 | try: |
| 82 | value = self._settings |
| 83 | for key in key_path.split('.'): |
| 84 | value = value[key] |
| 85 | return value |
| 86 | except (KeyError, TypeError): |
| 87 | return default |
| 88 | |
| 89 | def set(self, key_path: str, value): |
| 90 | """Imposta un valore nelle impostazioni usando un percorso con punti""" |
| 91 | try: |
| 92 | keys = key_path.split('.') |
| 93 | target = self._settings |
| 94 | |
| 95 | # Naviga attraverso il dizionario creando la struttura se necessario |
| 96 | for key in keys[:-1]: |
| 97 | if key not in target: |
| 98 | target[key] = {} |
| 99 | target = target[key] |
| 100 | |
| 101 | # Imposta il valore |
| 102 | target[keys[-1]] = value |
| 103 | |
| 104 | # Salva automaticamente se abilitato |
| 105 | if self.get('behavior.auto_save', True): |
| 106 | self.save() |
| 107 | |
| 108 | logging.info(f"Setting updated: {key_path} = {value}") |
| 109 | except Exception as e: |
| 110 | logging.error(f"Failed to set setting {key_path}: {e}") |
| 111 | |
| 112 | def _merge_settings(self, default, stored): |
| 113 | """Unisce le impostazioni memorizzate con quelle predefinite""" |
| 114 | result = default.copy() |
| 115 | for key, value in stored.items(): |
| 116 | if key in result and isinstance(result[key], dict) and isinstance(value, dict): |
| 117 | result[key] = self._merge_settings(result[key], value) |
| 118 | else: |
| 119 | result[key] = value |
| 120 | return result |
| 121 | |
| 122 | def reset(self): |
| 123 | """Resetta tutte le impostazioni ai valori predefiniti""" |
| 124 | self._settings = self.DEFAULT_SETTINGS.copy() |
| 125 | self.save() |
| 126 | logging.info("Settings reset to defaults") |
| 127 | |
| 128 | @property |
| 129 | def settings(self): |
| 130 | """Restituisce una copia delle impostazioni correnti""" |
| 131 | return self._settings.copy() |