contentmonster/classes/config.py

55 lines
1.7 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
2021-11-20 14:40:07 +00:00
from classes.vessel import Vessel
from classes.directory import Directory
2021-11-25 12:42:00 +00:00
2021-11-20 14:40:07 +00:00
class MonsterConfig:
2021-11-25 12:42:00 +00:00
def readFile(self, path: Union[str, Path]) -> 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)
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"):
2021-11-25 12:42:00 +00:00
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]))
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