From 215ed9060e643d516d03839acd69a5f8b662e8ea Mon Sep 17 00:00:00 2001 From: "Fredrick W. Warren" Date: Sun, 9 Mar 2025 20:53:11 -0600 Subject: [PATCH] Add reading config file --- pywmreceived.py | 92 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/pywmreceived.py b/pywmreceived.py index 7cac304..8af139b 100755 --- a/pywmreceived.py +++ b/pywmreceived.py @@ -5,12 +5,12 @@ WindowMaker dockapp pidgin messages Copyright (C) 2025 Fredrick W. Warren Licensed under the GNU General Public License. """ - import click +import configparser import dbus import dbus.mainloop.glib import logging -import os +import os.path import threading from gi.repository import GLib from icecream import ic @@ -23,6 +23,77 @@ config_file = os.path.expanduser("~/.config") + '/pywmreceived/config.ini' logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) + +def load_config(config_path): + """ + Load configuration from the specified config file. + + Parameters: + config_path (str): Path to the configuration file + + Returns: + list: List of configured lines with name, message count, and accounts + """ + # Default configuration + 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'} + for i, line in enumerate(default_lines): + section = f'line{i+1}' + config[section] = { + 'name': line[0].strip(), + 'accounts': ','.join(line[2]) + } + + # Write default config + with open(config_path, 'w') as configfile: + config.write(configfile) + + return default_lines + + # Read config file + config.read(config_path) + + # Parse configuration + lines = [] + + # Get number of sections (excluding DEFAULT) + sections = [s for s in config.sections() if s.startswith('line')] + + for section in sorted(sections): + if section.startswith('line'): + name = config[section].get('name', 'UNKNOWN') + accounts_str = config[section].get('accounts', '') + + # Parse accounts - split by comma and strip whitespace + accounts = [account.strip() for account in accounts_str.split(',') if account.strip()] + + # Add to lines with proper formatting (two spaces before name) + lines.append([f" {name}", 0, accounts]) + + # Always ensure we have the OTHER entry as the last one + if not lines or lines[-1][0].strip() != "OTHER": + lines.append([" OTHER", 0, ['']]) + + return lines + class Application(wmoo.Application): """ Display dockapp and respond to libpurple dbus @@ -31,18 +102,18 @@ class Application(wmoo.Application): def __init__(self, *args, **kwargs): """ + Initialize the application """ + # must remove config_path before passing **kwargs to woo.Application + config_path = kwargs.pop('config_path', config_file) wmoo.Application.__init__(self, *args, **kwargs) self._count = 0 self._flasher = 0 self.backlit = 0 - self.lines = [ # name, messages received, accounts - [" CATHY", 0, ['cathy@example.com']], - [" FRANK", 0, ['frank@example.com']], - [" TIM", 0, ['tim@example.com']], - [" LEE", 0, ['lee@example.com']], - [" OTHER", 0, ['']], - ] + + # Load configuration from file + self.lines = load_config(config_path) + # Initialize D-Bus and connect to Pidgin's ReceivedIMMsg signal self.register_dbus() @@ -270,7 +341,8 @@ def main(config, debug): fg=2, palette = palette, background = background, - patterns = patterns) + patterns = patterns, + config_path = config) # app.addCallback(app.previousRadio, 'buttonrelease', area=( 6,29,15,38)) # 6x7 grey1=1 grey2=10 green1=18 green2=27 app.draw_background()