Files
cpu_governor_auto_adjust/config.py
T
2024-12-06 16:41:59 +01:00

52 lines
1.6 KiB
Python

import logging
from pathlib import Path
from lxml import etree
from functools import cached_property
from arguments import ArgumentsParser
from typing import NamedTuple
class TriggerTuple(NamedTuple):
name: str
class Config:
def __init__(self, basepath: Path) -> None:
self.basepath = basepath
self.config = ArgumentsParser().parser.config
@cached_property
def root(self) -> etree.ElementTree:
return etree.parse(self.basepath / self.config)
@cached_property
def app_root(self) -> etree.Element:
return self.root.xpath('/cpuGovernorAutoAdjust').pop()
@cached_property
def loglevel(self) -> str:
value = self.app_root.xpath('logLevel').pop().text.upper()
_loglevels = ", ".join(logging.getLevelNamesMapping().keys())
if value not in logging.getLevelNamesMapping().keys():
raise ValueError(
"logLevel can only contain one of these values: "
f"{_loglevels}"
)
return value
@cached_property
def testmode(self) -> bool:
value = self.app_root.xpath('testMode').pop().text.lower()
if value not in ['true', 'false']:
raise ValueError("testMode can only be 'true' or 'false")
return True if value == 'true' else False
@cached_property
def triggertuples(self) -> list[TriggerTuple]:
ret_val = []
_triggers = self.app_root.xpath('triggers/trigger')
for trigger in _triggers:
name = trigger.xpath('name').pop().text
ret_val.append(TriggerTuple(name=name))
return ret_val