From 9ccf27765f23a36fc00cd3e3fa859ccd548a37f7 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Thu, 11 Jun 2020 22:56:12 +0200 Subject: [PATCH] fix dry for 237 --- dotdrop/cfg_aggregator.py | 9 ++++++--- dotdrop/cfg_yaml.py | 39 ++++++++++++++++++++++++--------------- dotdrop/options.py | 5 +++-- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 2141f58..61f877d 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -28,7 +28,7 @@ class CfgAggregator: dir_prefix = 'd' key_sep = '_' - def __init__(self, path, profile_key, debug=False): + def __init__(self, path, profile_key, debug=False, dry=False): """ high level config parser @path: path to the config file @@ -38,6 +38,7 @@ class CfgAggregator: self.path = path self.profile_key = profile_key self.debug = debug + self.dry = dry self.log = Logger() self._load() @@ -175,8 +176,8 @@ class CfgAggregator: msg = 'new dotfile {} to profile {}' self.log.dbg(msg.format(key, self.profile_key)) - self.cfgyaml.save() - if ret: + self.save() + if ret and not self.dry: # reload if self.debug: self.log.dbg('reloading config') @@ -291,6 +292,8 @@ class CfgAggregator: def save(self): """save the config""" + if self.dry: + return True return self.cfgyaml.save() def dump(self): diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 01f1a18..e66adde 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -7,6 +7,7 @@ handle lower level of the config file import os import glob +import io from copy import deepcopy from itertools import chain from ruamel.yaml import YAML as yaml @@ -942,12 +943,8 @@ class CfgYaml: # yaml utils ######################################################## - def save(self): - """save this instance and return True if saved""" - if not self.dirty: - return False - - content = self._clear_none(self.dump()) + def _prepare_to_save(self, content): + content = self._clear_none(content) # make sure we have the base entries if self.key_settings not in content: @@ -957,6 +954,15 @@ class CfgYaml: if self.key_profiles not in content: content[self.key_profiles] = None + return content + + def save(self): + """save this instance and return True if saved""" + if not self.dirty: + return False + + content = self._prepare_to_save(self.yaml_dict) + if self.dirty_deprecated: # add minversion settings = content[self.key_settings] @@ -966,7 +972,8 @@ class CfgYaml: if self.debug: self.log.dbg('saving to {}'.format(self.path)) try: - self._yaml_dump(content, self.path) + with open(self.path, 'w') as f: + self._yaml_dump(content, f) except Exception as e: self.log.err(e) raise YamlException('error saving config: {}'.format(self.path)) @@ -982,7 +989,10 @@ class CfgYaml: def dump(self): """dump the config dictionary""" - return self.yaml_dict + output = io.StringIO() + content = self._prepare_to_save(self.yaml_dict.copy()) + self._yaml_dump(content, output) + return output.getvalue() def _load_yaml(self, path): """load a yaml file to a dict""" @@ -1002,14 +1012,13 @@ class CfgYaml: content = y.load(f) return content - def _yaml_dump(self, content, path): + def _yaml_dump(self, content, where): """dump to yaml""" - with open(self.path, 'w') as f: - y = yaml() - y.default_flow_style = False - y.indent = 2 - y.typ = 'rt' - y.dump(content, f) + y = yaml() + y.default_flow_style = False + y.indent = 2 + y.typ = 'rt' + y.dump(content, where) ######################################################## # helpers diff --git a/dotdrop/options.py b/dotdrop/options.py index 274c083..3760e71 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -114,6 +114,7 @@ class Options(AttrMonitor): self.args = docopt(USAGE, version=VERSION) self.log = Logger() self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ + self.dry = self.args['--dry'] if ENV_NODEBUG in os.environ: # force disabling debugs self.debug = False @@ -180,7 +181,8 @@ class Options(AttrMonitor): def _read_config(self): """read the config file""" - self.conf = Cfg(self.confpath, self.profile, debug=self.debug) + self.conf = Cfg(self.confpath, self.profile, debug=self.debug, + dry=self.dry) # transform the config settings to self attribute for k, v in self.conf.get_settings().items(): if self.debug: @@ -200,7 +202,6 @@ class Options(AttrMonitor): self.cmd_remove = self.args['remove'] # adapt attributes based on arguments - self.dry = self.args['--dry'] self.safe = not self.args['--force'] # import link default value