From b46a58bd233cc526dfa4e45bbdbdbd3754e2fc17 Mon Sep 17 00:00:00 2001 From: Cantue35 <71815869+Cantue35@users.noreply.github.com> Date: Tue, 8 Apr 2025 20:35:09 +0300 Subject: [PATCH] Fix forced update config deletion when update check is disabled - Added a conditional check in force_update_config() to verify that enabled_update_check in the [Utils] section is read as False. - If disabled, the config file will not be deleted. - This prevents loss of custom configuration and improves usability. - Tested on macOS with our modifications. --- config.py | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/config.py b/config.py index e5839c8..98706af 100644 --- a/config.py +++ b/config.py @@ -261,7 +261,7 @@ def print_config(config, translator=None): def force_update_config(translator=None): """ - Force update configuration file with latest defaults + Force update configuration file with latest defaults if update check is enabled. Args: translator: Translator instance Returns: @@ -271,26 +271,39 @@ def force_update_config(translator=None): config_dir = os.path.join(get_user_documents_path(), ".cursor-free-vip") config_file = os.path.join(config_dir, "config.ini") current_time = datetime.datetime.now() - + + # If the config file exists, check if forced update is enabled if os.path.exists(config_file): - try: - # create backup - backup_file = f"{config_file}.bak.{current_time.strftime('%Y%m%d_%H%M%S')}" - shutil.copy2(config_file, backup_file) + # First, read the existing configuration + existing_config = configparser.ConfigParser() + existing_config.read(config_file, encoding='utf-8') + # Check if "enabled_update_check" is True + update_enabled = True # Default to True if not set + if existing_config.has_section('Utils') and existing_config.has_option('Utils', 'enabled_update_check'): + update_enabled = existing_config.get('Utils', 'enabled_update_check').strip().lower() in ('true', 'yes', '1', 'on') + + if update_enabled: + try: + # Create a backup + backup_file = f"{config_file}.bak.{current_time.strftime('%Y%m%d_%H%M%S')}" + shutil.copy2(config_file, backup_file) + if translator: + print(f"{Fore.CYAN}{EMOJI['INFO']} {translator.get('config.backup_created', path=backup_file) if translator else f'Backup created: {backup_file}'}{Style.RESET_ALL}") + + # Delete the original config file (forced update) + os.remove(config_file) + if translator: + print(f"{Fore.CYAN}{EMOJI['INFO']} {translator.get('config.config_removed') if translator else 'Config file removed for forced update'}{Style.RESET_ALL}") + except Exception as e: + if translator: + print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('config.backup_failed', error=str(e)) if translator else f'Failed to backup config: {str(e)}'}{Style.RESET_ALL}") + else: if translator: - print(f"{Fore.CYAN}{EMOJI['INFO']} {translator.get('config.backup_created', path=backup_file) if translator else f'Backup created: {backup_file}'}{Style.RESET_ALL}") - - # delete original file - os.remove(config_file) - if translator: - print(f"{Fore.CYAN}{EMOJI['INFO']} {translator.get('config.config_removed') if translator else 'Config file removed for forced update'}{Style.RESET_ALL}") - except Exception as e: - if translator: - print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('config.backup_failed', error=str(e)) if translator else f'Failed to backup config: {str(e)}'}{Style.RESET_ALL}") - - # use existing setup_config function to create new config + print(f"{Fore.CYAN}{EMOJI['INFO']} {translator.get('config.force_update_disabled', fallback='Force update disabled by configuration. Keeping existing config file.') if translator else 'Force update disabled by configuration. Keeping existing config file.'}{Style.RESET_ALL}") + + # Generate a new (or updated) configuration if needed return setup_config(translator) - + except Exception as e: if translator: print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('config.force_update_failed', error=str(e)) if translator else f'Force update config failed: {str(e)}'}{Style.RESET_ALL}")