contentmonster/src/contentmonster/classes/config.py
Kumi e82ccb2701
feat: Enhance stability and configurability
- Extended copyright to reflect the current year.
- Incremented project version to indicate new features and fixes.
- Added a new script entry for easier execution, increasing utility and accessibility.
- Updated project URLs for better alignment with current infrastructure.
- Refactored settings path for simplicity and consistency across deployments.
- Improved code readability and maintenance across several modules by cleaning up redundant code, adding missing type annotations, and ensuring consistent code formatting.
- Enhanced logging capabilities and error handling to improve diagnostics and troubleshooting, supporting more robust error recovery mechanisms.
- Implemented more graceful handling of termination signals to ensure clean shutdown and resource cleanup, enhancing the robustness of the application in production environments.
- Introduced command-line argument parsing for configuration file path customization, improving flexibility in different runtime environments.

These changes collectively improve the project's maintainability, reliability, and user experience, laying a stronger foundation for future development.
2024-04-22 16:39:33 +02:00

56 lines
1.9 KiB
Python

import configparser
from pathlib import Path
from typing import Union
from .vessel import Vessel
from .directory import Directory
from .database import Database
class MonsterConfig:
def readFile(self, path: Union[str, Path], dbclass: type = Database) -> None:
"""Read .ini file into MonsterConfig object
Args:
path (str, pathlib.Path): Location of the .ini file to read
(absolute or relative to the working directory)
dbclass (type): Class to use for database connections. Defaults to
built-in Database using sqlite3.
Raises:
ValueError: Raised if the passed file is not a ContentMonster .ini
IOError: Raised if the file cannot be read from the provided path
"""
parser = configparser.ConfigParser()
parser.read(str(path))
if not "MONSTER" in parser.sections():
raise ValueError("Config file does not contain a MONSTER section!")
try:
self.database = parser["MONSTER"]["Database"]
except KeyError:
pass
try:
self.chunksize = int(parser["MONSTER"]["ChunkSize"])
except KeyError:
pass
for section in parser.sections():
# Read Directories from the config file
if section.startswith("Directory"):
self.directories.append(Directory.fromConfig(parser[section]))
# Read Vessels from the config file
elif section.startswith("Vessel"):
self.vessels.append(Vessel.fromConfig(parser[section], dbclass))
def __init__(self) -> None:
"""Initialize a new (empty) MonsterConfig object"""
self.directories = []
self.vessels = []
self.chunksize = 10485760 # Default: 10 MiB
self.database = None # Default: "database.sqlite3" in base directory