reportmonster/src/reportmonster/classes/config.py

50 lines
1.6 KiB
Python

import configparser
from pathlib import Path
from typing import Union
from classes.vessel import Vessel
from classes.user import User
class MonsterConfig:
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
"""
parser = configparser.ConfigParser()
parser.read(str(path))
if not "MONSTER" in parser.sections():
raise ValueError("Config file does not contain a MONSTER section!")
for section in parser.sections():
# Read Vessels from the config file
if section.startswith("Vessel"):
self.vessels.append(Vessel.fromConfig(parser[section]))
if section.startswith("User"):
self.users.append(User.fromConfig(parser[section]))
try:
self.pyadonis = Path(parser["MONSTER"]["PyAdonis"])
except KeyError:
print(f"PyAdonis is not defined in the MONSTER section of {path}, some features may be missing.")
def __init__(self, path: Union[str, Path]) -> None:
"""Initialize a new (empty) MonsterConfig object
"""
self.vessels = []
self.users = []
self.pyadonis = None
if path:
self.readFile(path)