refactoring installer

master
Jordan Orelli 2 years ago
parent 850120dc57
commit 30d476768f

@ -1,6 +1,7 @@
import configparser
import json
import pathlib import pathlib
import sys import sys
import json
from functools import cached_property from functools import cached_property
from . import host from . import host
@ -16,6 +17,17 @@ class Installer:
def __init__(self): def __init__(self):
self.options = Options.from_cli_args() self.options = Options.from_cli_args()
@cached_property
def config(self):
path = self.options.config_path
with open(path, 'r', encoding='utf-8') as config_file:
parser = configparser.ConfigParser()
# make keys case-sensitive
parser.optionxform = str
parser.read_file(config_file)
return parser
self._config = parser
def run(self): def run(self):
""" """
runs the install process runs the install process
@ -28,7 +40,7 @@ class Installer:
print("You are not admin: admin is required on Windows") print("You are not admin: admin is required on Windows")
sys.exit(1) sys.exit(1)
config = self.options.config config = self.config
for section_name in config.sections(): for section_name in config.sections():
try: try:
T = self.parse_section_name(section_name) T = self.parse_section_name(section_name)
@ -55,13 +67,12 @@ class Installer:
self.map_section('map.windows') self.map_section('map.windows')
def run_x(self): def run_x(self):
config = self.options.config for section_name in self.config.sections():
for section_name in config.sections():
print("sections:") print("sections:")
print(f" {section_name}") print(f" {section_name}")
if r := Resource.from_name(section_name): if r := Resource.from_name(section_name):
print(f" {r}") print(f" {r}")
r.parse_section(config[section_name]) r.parse_section(self.config[section_name])
@cached_property @cached_property
def targets(self): def targets(self):
@ -94,14 +105,14 @@ class Installer:
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
return self.options.config return self.options.config
@cached_property # @cached_property
def config(self): # def config(self):
""" # """
the contents of our configuration file # the contents of our configuration file
""" # """
with open(self.config_path, 'r', encoding='utf-8') as config_fp: # with open(self.config_path, 'r', encoding='utf-8') as config_fp:
log.debug("loading config from path %s", self.config_path) # log.debug("loading config from path %s", self.config_path)
return json.load(config_fp) # return json.load(config_fp)
def map_section(self, section_name): def map_section(self, section_name):
section = self.options.config[section_name] section = self.options.config[section_name]

@ -3,8 +3,8 @@ cli options
""" """
import argparse import argparse
import configparser
import pathlib import pathlib
from functools import cached_property
from . import log from . import log
from . import host from . import host
@ -61,21 +61,8 @@ class Options:
def verbose(self, val): def verbose(self, val):
self._verbose = val self._verbose = val
@property @cached_property
def config(self): def config_path(self):
# pylint: disable=missing-function-docstring if not self.config:
return self._config self.config = host.dotfiles_root / "config.ini"
return pathlib.Path(self.config)
@config.setter
def config(self, val):
if val is None:
path = host.dotfiles_root / "config.ini"
else:
path = pathlib.Path(val)
with open(path, 'r', encoding='utf-8') as config_file:
parser = configparser.ConfigParser()
# make keys case-sensitive
parser.optionxform = str
parser.read_file(config_file)
self._config = parser

Loading…
Cancel
Save