You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from configparser import ConfigParser, SectionProxy
|
|
from functools import cached_property
|
|
|
|
from .options import Options
|
|
from .resource import Resource
|
|
|
|
|
|
class Installer:
|
|
"""
|
|
manages the installation of preferences files
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.options = Options.from_cli_args()
|
|
|
|
@cached_property
|
|
def config(self) -> ConfigParser:
|
|
path = self.options.config_path
|
|
with open(path, "r", encoding="utf-8") as config_file:
|
|
parser = ConfigParser()
|
|
# make keys case-sensitive
|
|
parser.optionxform = str
|
|
parser.read_file(config_file)
|
|
return parser
|
|
|
|
def run(self):
|
|
for name in self.config.sections():
|
|
section = self.config[name]
|
|
if self.when(section):
|
|
r = Resource.from_section(name, section)
|
|
print(f"RUN [{name}]")
|
|
r.run()
|
|
else:
|
|
print(f"SKIP [{name}]")
|
|
|
|
def when(self, section: SectionProxy) -> bool:
|
|
if clause := section.get("when", None):
|
|
from .host import host
|
|
|
|
given_locals = {
|
|
"host": host,
|
|
}
|
|
|
|
v = eval(clause, None, given_locals)
|
|
return bool(v)
|
|
return True
|