Add reading config file

This commit is contained in:
Fredrick W. Warren 2025-03-09 20:53:11 -06:00
parent 35ad2db939
commit 215ed9060e

View File

@ -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()