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.
This commit is contained in:
Stefano Marinelli 2023-10-22 21:25:37 +02:00
parent 5f9c0636e5
commit 9f23eb8c92
3 changed files with 38 additions and 8 deletions

View file

@ -1,6 +1,6 @@
""" """
NotiMail NotiMail
Version: 0.10 Version: 0.11 - Alpha
Author: Stefano Marinelli <stefano@dragas.it> Author: Stefano Marinelli <stefano@dragas.it>
License: BSD 3-Clause License License: BSD 3-Clause License
@ -71,18 +71,30 @@ import datetime
import signal import signal
import sys import sys
import logging import logging
import argparse
import threading import threading
from email import policy from email import policy
from email.parser import BytesParser from email.parser import BytesParser
# Setup logging # Argument parsing to get the config file
logging.basicConfig(filename='notimail.log', 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, level=logging.INFO,
format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s') format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')
class DatabaseHandler: 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.connection = sqlite3.connect(db_name)
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
self.create_table() self.create_table()
@ -366,9 +378,6 @@ def shutdown_handler(signum, frame):
sys.exit(0) sys.exit(0)
def multi_account_main(): def multi_account_main():
config = configparser.ConfigParser()
config.read('config.ini')
accounts = [] accounts = []
# Check for the old format [EMAIL] section # Check for the old format [EMAIL] section

View file

@ -136,6 +136,23 @@ With that, you should have NotiMail up and running on your system! Enjoy a more
### Changelog: ### 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:** - **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. - 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.

View file

@ -1,3 +1,7 @@
[GENERAL]
LogFileLocation = notimail.log
DataBaseLocation = processed_emails.db
[EMAIL:account1] [EMAIL:account1]
EmailUser = your@address.com EmailUser = your@address.com
EmailPass = YourPassword EmailPass = YourPassword