contentmonster/src/contentmonster/classes/config.py

56 lines
1.9 KiB
Python
Raw Normal View History

2021-11-20 14:40:07 +00:00
import configparser
2021-11-25 12:42:00 +00:00
from pathlib import Path
from typing import Union
2022-09-19 18:05:58 +00:00
from .vessel import Vessel
from .directory import Directory
from .database import Database
2021-11-20 14:40:07 +00:00
2021-11-25 12:42:00 +00:00
2021-11-20 14:40:07 +00:00
class MonsterConfig:
def readFile(self, path: Union[str, Path], dbclass: type = Database) -> None:
2021-11-25 12:42:00 +00:00
"""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.
2021-11-25 12:42:00 +00:00
Raises:
ValueError: Raised if the passed file is not a ContentMonster .ini
IOError: Raised if the file cannot be read from the provided path
"""
2021-11-20 14:40:07 +00:00
parser = configparser.ConfigParser()
2021-11-25 12:42:00 +00:00
parser.read(str(path))
2021-11-20 14:40:07 +00:00
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
2021-11-25 18:03:58 +00:00
try:
self.chunksize = int(parser["MONSTER"]["ChunkSize"])
2021-11-25 18:03:58 +00:00
except KeyError:
pass
2021-11-20 14:40:07 +00:00
for section in parser.sections():
2021-11-25 12:42:00 +00:00
# Read Directories from the config file
2021-11-20 14:40:07 +00:00
if section.startswith("Directory"):
self.directories.append(Directory.fromConfig(parser[section]))
2021-11-22 10:14:38 +00:00
2021-11-25 12:42:00 +00:00
# Read Vessels from the config file
elif section.startswith("Vessel"):
self.vessels.append(Vessel.fromConfig(parser[section], dbclass))
2021-11-20 14:40:07 +00:00
2021-11-25 12:42:00 +00:00
def __init__(self) -> None:
"""Initialize a new (empty) MonsterConfig object"""
2021-11-22 10:14:38 +00:00
self.directories = []
self.vessels = []
self.chunksize = 10485760 # Default: 10 MiB
self.database = None # Default: "database.sqlite3" in base directory