reportmonster/src/reportmonster/classes/config.py
2023-07-03 11:24:31 +02:00

55 lines
1.7 KiB
Python

import configparser
from pathlib import Path
from typing import Union, List
from .vessel import Vessel
from .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:
try:
import pyadonis
self.pyadonis = Path(pyadonis.__path__[0])
except ImportError:
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: List[Vessel] = []
self.users: List[User] = []
self.pyadonis = None
if path:
self.readFile(path)