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,20 +30,27 @@ class ConfigManager:
Args:
file_name (str, optional): Configuration file name. Default: 'config.json'.
"""
# Determine the base path - use the current working directory
if getattr(sys, 'frozen', False):
# If the application is frozen (e.g., PyInstaller)
base_path = os.path.dirname(sys.executable)
else:
# Get the actual path of the module file
current_file_path = os.path.abspath(__file__)
# Navigate upwards to find the project root
# Assuming this file is in a package structure like StreamingCommunity/Util/config_json.py
# 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)))
# 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
if getattr(sys, 'frozen', False):
# If the application is frozen (e.g., PyInstaller)
base_path = os.path.dirname(sys.executable)
else:
# Get the actual path of the module file
current_file_path = os.path.abspath(__file__)
# Navigate upwards to find the project root
# Assuming this file is in a package structure like StreamingCommunity/Util/config_json.py
# 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)))
else:
base_path = os.path.dirname(os.path.abspath(file_name))
file_name = os.path.basename(file_name)
# Initialize file paths
self.file_path = os.path.join(base_path, file_name)
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
SHOW_TRENDING = config_manager.get_bool('DEFAULT', 'show_trending')
NOT_CLOSE_CONSOLE = config_manager.get_bool('DEFAULT', 'not_close')
TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
# SHOW_TRENDING = config_manager.get_bool('DEFAULT', 'show_trending')
# NOT_CLOSE_CONSOLE = config_manager.get_bool('DEFAULT', 'not_close')
# TELEGRAM_BOT = config_manager.get_bool('DEFAULT', 'telegram_bot')
# Variable
@ -61,7 +61,7 @@ def load_search_functions():
loaded_functions = {}
# 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
if getattr(sys, 'frozen', False): # Modalità PyInstaller
@ -142,7 +142,8 @@ def initialize():
sys.exit(0)
# Trending tmbd
if SHOW_TRENDING:
# SHOW_TRENDING
if config_manager.get_bool('DEFAULT', 'show_trending'):
print()
tmdb.display_trending_films()
tmdb.display_trending_tv_shows()
@ -200,10 +201,6 @@ def main(script_id = 0):
"torrent": "white"
}
if TELEGRAM_BOT:
bot = get_bot_instance()
bot.send_message(f"Avviato script {script_id}", None)
start = time.time()
# Create logger
@ -244,6 +241,11 @@ def main(script_id = 0):
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
parser.add_argument(
'--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
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
# Map command-line arguments to the config values
config_updates = {}
@ -332,7 +344,8 @@ def main(script_id = 0):
for key, label in choice_labels.items()]
) + "[white])"
if TELEGRAM_BOT:
# TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
category_legend_str = "Categorie: \n" + " | ".join([
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)
else:
if TELEGRAM_BOT:
# TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
bot.send_message(f"Categoria non valida", None)
console.print("[red]Invalid category.")
if NOT_CLOSE_CONSOLE:
# NOT_CLOSE_CONSOLE
if config_manager.get_bool('DEFAULT', 'not_close'):
restart_script()
else:
force_exit()
if TELEGRAM_BOT:
# TELEGRAM_BOT
if config_manager.get_bool('DEFAULT', 'telegram_bot'):
bot.send_message(f"Chiusura in corso", None)
# Delete script_id