From 30d476768f3c2362c9d9de1eca31ccf7f30b3eb2 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 28 Jan 2023 17:30:45 -0600 Subject: [PATCH] refactoring installer --- prefs/installer.py | 37 ++++++++++++++++++++++++------------- prefs/options.py | 25 ++++++------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/prefs/installer.py b/prefs/installer.py index 8c36659..b26ab99 100644 --- a/prefs/installer.py +++ b/prefs/installer.py @@ -1,6 +1,7 @@ +import configparser +import json import pathlib import sys -import json from functools import cached_property from . import host @@ -16,6 +17,17 @@ class Installer: def __init__(self): 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): """ runs the install process @@ -28,7 +40,7 @@ class Installer: print("You are not admin: admin is required on Windows") sys.exit(1) - config = self.options.config + config = self.config for section_name in config.sections(): try: T = self.parse_section_name(section_name) @@ -55,13 +67,12 @@ class Installer: self.map_section('map.windows') def run_x(self): - config = self.options.config - for section_name in config.sections(): + for section_name in self.config.sections(): print("sections:") print(f" {section_name}") if r := Resource.from_name(section_name): print(f" {r}") - r.parse_section(config[section_name]) + r.parse_section(self.config[section_name]) @cached_property def targets(self): @@ -94,14 +105,14 @@ class Installer: # pylint: disable=missing-function-docstring return self.options.config - @cached_property - def config(self): - """ - the contents of our configuration file - """ - with open(self.config_path, 'r', encoding='utf-8') as config_fp: - log.debug("loading config from path %s", self.config_path) - return json.load(config_fp) + # @cached_property + # def config(self): + # """ + # the contents of our configuration file + # """ + # with open(self.config_path, 'r', encoding='utf-8') as config_fp: + # log.debug("loading config from path %s", self.config_path) + # return json.load(config_fp) def map_section(self, section_name): section = self.options.config[section_name] diff --git a/prefs/options.py b/prefs/options.py index 78c7210..7ef9e70 100644 --- a/prefs/options.py +++ b/prefs/options.py @@ -3,8 +3,8 @@ cli options """ import argparse -import configparser import pathlib +from functools import cached_property from . import log from . import host @@ -61,21 +61,8 @@ class Options: def verbose(self, val): self._verbose = val - @property - def config(self): - # pylint: disable=missing-function-docstring - return 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 + @cached_property + def config_path(self): + if not self.config: + self.config = host.dotfiles_root / "config.ini" + return pathlib.Path(self.config)