From 9f23eb8c92eeacc3ace54ca0c3d7cb7c5f9c0a88 Mon Sep 17 00:00:00 2001 From: Stefano Marinelli Date: Sun, 22 Oct 2023 21:25:37 +0200 Subject: [PATCH] Add configurable paths for log and database via config file - Introduced argparse for custom config file support. - Added [GENERAL] section in config.ini for log and DB paths. - Updated logging and DatabaseHandler to use paths from config. --- NotiMail.py | 25 +++++++++++++++++-------- README.md | 17 +++++++++++++++++ config.ini | 4 ++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/NotiMail.py b/NotiMail.py index f5193a2..31c9b8e 100644 --- a/NotiMail.py +++ b/NotiMail.py @@ -1,6 +1,6 @@ """ NotiMail -Version: 0.10 +Version: 0.11 - Alpha Author: Stefano Marinelli License: BSD 3-Clause License @@ -71,18 +71,30 @@ import datetime import signal import sys import logging +import argparse import threading from email import policy from email.parser import BytesParser -# Setup logging -logging.basicConfig(filename='notimail.log', +# Argument parsing to get the config file +parser = argparse.ArgumentParser(description='NotiMail Notification Service.') +parser.add_argument('-c', '--config', type=str, default='config.ini', help='Path to the configuration file.') +args = parser.parse_args() + +# Configuration reading +config = configparser.ConfigParser() +config.read(args.config) + +# Logging setup using config (or default if not set) +log_file_location = config.get('GENERAL', 'LogFileLocation', fallback='notimail.log') +logging.basicConfig(filename=log_file_location, level=logging.INFO, format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s') - class DatabaseHandler: - def __init__(self, db_name="processed_emails.db"): + def __init__(self, db_name=None): + if db_name is None: + db_name = config.get('GENERAL', 'DataBaseLocation', fallback="processed_emails.db") self.connection = sqlite3.connect(db_name) self.cursor = self.connection.cursor() self.create_table() @@ -366,9 +378,6 @@ def shutdown_handler(signum, frame): sys.exit(0) def multi_account_main(): - config = configparser.ConfigParser() - config.read('config.ini') - accounts = [] # Check for the old format [EMAIL] section diff --git a/README.md b/README.md index e3455f9..8251c65 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,23 @@ With that, you should have NotiMail up and running on your system! Enjoy a more ### Changelog: +- **Version 0.11 Alpha - not yet released:** + +New Features: + +- **Configurable Paths:** Introduced the ability to configure the location of the log file and the SQLite database from the configuration file. +- **Flexible Configuration:** Added support to specify a custom configuration file when running the script. + +Changes: + +- **Codebase:** + - Introduced `argparse` to enable command-line arguments. Users can now specify the path to a configuration file using `-c` or `--config` option. + - Moved the config reading to the top of the script to make it globally accessible. + - Updated the logging setup to fetch the log file location from the config file (`LogFileLocation`). If not set, it defaults to `notimail.log`. + - Modified the `DatabaseHandler` class to fetch the database location from the config file (`DataBaseLocation`). If not set, it defaults to `processed_emails.db`. +- **Configuration (`config.ini`):** + - Introduced a new `[GENERAL]` section. Users can specify `LogFileLocation` and `DataBaseLocation` within this section to set the desired paths for the log file and database, respectively. + - **Version 0.10:** - Authentication Tokens for NTFY Notifications: Enhanced the NTFYNotificationProvider to support optional authentication tokens. If a token is provided in the config.ini file for a specific NTFY URL, the notification request will include an "Authorization" header for authentication. diff --git a/config.ini b/config.ini index 54a51f8..2aff74e 100644 --- a/config.ini +++ b/config.ini @@ -1,3 +1,7 @@ +[GENERAL] +LogFileLocation = notimail.log +DataBaseLocation = processed_emails.db + [EMAIL:account1] EmailUser = your@address.com EmailPass = YourPassword