refactor: update config handling in ConfigManager (removed globals and add parsing of file_name ) and run script for better flexibility, enable config argument to load custom config and reinitialize config_manager.

This commit is contained in:
Alessandro Perazzetta 2025-05-23 12:24:51 +02:00
parent 02c35929ec
commit e667d970e9
2 changed files with 48 additions and 25 deletions

View File

@ -30,6 +30,9 @@ class ConfigManager:
Args: Args:
file_name (str, optional): Configuration file name. Default: 'config.json'. file_name (str, optional): Configuration file name. Default: 'config.json'.
""" """
# Check if the file name is a full path or just a file name
if os.path.basename(file_name) == file_name:
# Determine the base path - use the current working directory # Determine the base path - use the current working directory
if getattr(sys, 'frozen', False): if getattr(sys, 'frozen', False):
# If the application is frozen (e.g., PyInstaller) # If the application is frozen (e.g., PyInstaller)
@ -44,6 +47,10 @@ class ConfigManager:
# We need to go up 2 levels to reach the project root # We need to go up 2 levels to reach the project root
base_path = os.path.dirname(os.path.dirname(os.path.dirname(current_file_path))) base_path = os.path.dirname(os.path.dirname(os.path.dirname(current_file_path)))
else:
base_path = os.path.dirname(os.path.abspath(file_name))
file_name = os.path.basename(file_name)
# Initialize file paths # Initialize file paths
self.file_path = os.path.join(base_path, file_name) self.file_path = os.path.join(base_path, file_name)
self.domains_path = os.path.join(base_path, 'domains.json') self.domains_path = os.path.join(base_path, 'domains.json')

View File

@ -29,9 +29,9 @@ from StreamingCommunity.TelegramHelp.telegram_bot import get_bot_instance, Teleg
# Config # Config
SHOW_TRENDING = config_manager.get_bool('DEFAULT', 'show_trending') # SHOW_TRENDING = config_manager.get_bool('DEFAULT', 'show_trending')
NOT_CLOSE_CONSOLE = config_manager.get_bool('DEFAULT', 'not_close') # NOT_CLOSE_CONSOLE = config_manager.get_bool('DEFAULT', 'not_close')
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot') # TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
# Variable # Variable
@ -61,7 +61,7 @@ def load_search_functions():
loaded_functions = {} loaded_functions = {}
# Lista dei siti da escludere se TELEGRAM_BOT è attivo # Lista dei siti da escludere se TELEGRAM_BOT è attivo
excluded_sites = {"cb01new", "guardaserie", "ilcorsaronero", "mostraguarda"} if TELEGRAM_BOT else set() excluded_sites = {"cb01new", "guardaserie", "ilcorsaronero", "mostraguarda"} if config_manager.get_bool('DEFAULT', 'telegram_bot') else set()
# Find api home directory # Find api home directory
if getattr(sys, 'frozen', False): # Modalità PyInstaller if getattr(sys, 'frozen', False): # Modalità PyInstaller
@ -142,7 +142,8 @@ def initialize():
sys.exit(0) sys.exit(0)
# Trending tmbd # Trending tmbd
if SHOW_TRENDING: # SHOW_TRENDING
if config_manager.get_bool('DEFAULT', 'show_trending'):
print() print()
tmdb.display_trending_films() tmdb.display_trending_films()
tmdb.display_trending_tv_shows() tmdb.display_trending_tv_shows()
@ -200,10 +201,6 @@ def main(script_id = 0):
"torrent": "white" "torrent": "white"
} }
if TELEGRAM_BOT:
bot = get_bot_instance()
bot.send_message(f"Avviato script {script_id}", None)
start = time.time() start = time.time()
# Create logger # Create logger
@ -244,6 +241,11 @@ def main(script_id = 0):
parser.add_argument("script_id", nargs="?", default="unknown", help="ID dello script") parser.add_argument("script_id", nargs="?", default="unknown", help="ID dello script")
# Add arguments for the custom config file
parser.add_argument(
'--config', type=str, help='Load custom config file name (e.g., custom_config.json).'
)
# Add arguments for the main configuration parameters # Add arguments for the main configuration parameters
parser.add_argument( parser.add_argument(
'--add_siteName', type=bool, help='Enable or disable adding the site name to the file name (e.g., true/false).' '--add_siteName', type=bool, help='Enable or disable adding the site name to the file name (e.g., true/false).'
@ -279,6 +281,16 @@ def main(script_id = 0):
# Parse command-line arguments # Parse command-line arguments
args = parser.parse_args() args = parser.parse_args()
# If custom config file is specified, update config_manager
if args.config:
config_manager.__init__(args.config)
# TELEGRAM_BOT:
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
bot = get_bot_instance()
bot.send_message(f"Avviato script {script_id}", None)
search_terms = args.search search_terms = args.search
# Map command-line arguments to the config values # Map command-line arguments to the config values
config_updates = {} config_updates = {}
@ -332,7 +344,8 @@ def main(script_id = 0):
for key, label in choice_labels.items()] for key, label in choice_labels.items()]
) + "[white])" ) + "[white])"
if TELEGRAM_BOT: # TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
category_legend_str = "Categorie: \n" + " | ".join([ category_legend_str = "Categorie: \n" + " | ".join([
f"{category.capitalize()}" for category in color_map.keys() f"{category.capitalize()}" for category in color_map.keys()
]) ])
@ -357,18 +370,21 @@ def main(script_id = 0):
run_function(input_to_function[category], search_terms=search_terms) run_function(input_to_function[category], search_terms=search_terms)
else: else:
if TELEGRAM_BOT: # TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
bot.send_message(f"Categoria non valida", None) bot.send_message(f"Categoria non valida", None)
console.print("[red]Invalid category.") console.print("[red]Invalid category.")
if NOT_CLOSE_CONSOLE: # NOT_CLOSE_CONSOLE
if config_manager.get_bool('DEFAULT', 'not_close'):
restart_script() restart_script()
else: else:
force_exit() force_exit()
if TELEGRAM_BOT: # TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
bot.send_message(f"Chiusura in corso", None) bot.send_message(f"Chiusura in corso", None)
# Delete script_id # Delete script_id