From d8c9c26545d5381800865cf49be8d321dea6c4d7 Mon Sep 17 00:00:00 2001 From: "Fredrick W. Warren" Date: Sun, 9 Mar 2025 21:01:54 -0600 Subject: [PATCH] always add OTHER entry --- pywmreceived.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/pywmreceived.py b/pywmreceived.py index 8af139b..47e4a24 100755 --- a/pywmreceived.py +++ b/pywmreceived.py @@ -34,27 +34,22 @@ def load_config(config_path): Returns: list: List of configured lines with name, message count, and accounts """ - # Default configuration + # Default configuration (used if file doesn't exist) default_lines = [ [" ONE", 0, ['one@example.com']], [" TWO", 0, ['two@example.com']], [" THREE", 0, ['three@example.com']], [" FOUR", 0, ['four@example.com']], [" FIVE", 0, ['five@exmaple.com']], - [" OTHER", 0, ['']], ] - # Create config parser config = configparser.ConfigParser() - # Check if config file exists if not os.path.exists(config_path): logger.warning(f"Config file not found at {config_path}. Using default configuration.") - # Create directory if it doesn't exist os.makedirs(os.path.dirname(config_path), exist_ok=True) - # Create default config file - config['DEFAULT'] = {'max_lines': '6'} + config['DEFAULT'] = {'max_lines': '5'} #limit to 5 from config for i, line in enumerate(default_lines): section = f'line{i+1}' config[section] = { @@ -62,11 +57,35 @@ def load_config(config_path): 'accounts': ','.join(line[2]) } - # Write default config with open(config_path, 'w') as configfile: config.write(configfile) - return default_lines + result = [ [line[0], line[1], line[2]] for line in default_lines] + result.append([" OTHER", 0, ['']]) + return result + + try: + config.read(config_path) + max_lines = int(config['DEFAULT'].get('max_lines', '5')) #get max lines from config, default to 5. + + configured_lines = [] + for i in range(1, max_lines + 1): + section = f'line{i}' + if section in config: + name = ' ' + config[section].get('name', '').strip() + accounts_str = config[section].get('accounts', '') + accounts = [acc.strip() for acc in accounts_str.split(',') if acc.strip()] + configured_lines.append([name, 0, accounts]) + else: + break #stop if no more config sections. + + configured_lines.append([" OTHER", 0, ['']]) #always add OTHER + return configured_lines + except (configparser.Error, ValueError) as e: + logger.error(f"Error reading config file: {e}. Using default configuration and adding OTHER.") + result = [ [line[0], line[1], line[2]] for line in default_lines] + result.append([" OTHER", 0, ['']]) + return result # Read config file config.read(config_path)