1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 08:04:16 +00:00

fix dry for 237

This commit is contained in:
deadc0de6
2020-06-11 22:56:12 +02:00
parent 18a61bce60
commit 9ccf27765f
3 changed files with 33 additions and 20 deletions

View File

@@ -28,7 +28,7 @@ class CfgAggregator:
dir_prefix = 'd' dir_prefix = 'd'
key_sep = '_' key_sep = '_'
def __init__(self, path, profile_key, debug=False): def __init__(self, path, profile_key, debug=False, dry=False):
""" """
high level config parser high level config parser
@path: path to the config file @path: path to the config file
@@ -38,6 +38,7 @@ class CfgAggregator:
self.path = path self.path = path
self.profile_key = profile_key self.profile_key = profile_key
self.debug = debug self.debug = debug
self.dry = dry
self.log = Logger() self.log = Logger()
self._load() self._load()
@@ -175,8 +176,8 @@ class CfgAggregator:
msg = 'new dotfile {} to profile {}' msg = 'new dotfile {} to profile {}'
self.log.dbg(msg.format(key, self.profile_key)) self.log.dbg(msg.format(key, self.profile_key))
self.cfgyaml.save() self.save()
if ret: if ret and not self.dry:
# reload # reload
if self.debug: if self.debug:
self.log.dbg('reloading config') self.log.dbg('reloading config')
@@ -291,6 +292,8 @@ class CfgAggregator:
def save(self): def save(self):
"""save the config""" """save the config"""
if self.dry:
return True
return self.cfgyaml.save() return self.cfgyaml.save()
def dump(self): def dump(self):

View File

@@ -7,6 +7,7 @@ handle lower level of the config file
import os import os
import glob import glob
import io
from copy import deepcopy from copy import deepcopy
from itertools import chain from itertools import chain
from ruamel.yaml import YAML as yaml from ruamel.yaml import YAML as yaml
@@ -942,12 +943,8 @@ class CfgYaml:
# yaml utils # yaml utils
######################################################## ########################################################
def save(self): def _prepare_to_save(self, content):
"""save this instance and return True if saved""" content = self._clear_none(content)
if not self.dirty:
return False
content = self._clear_none(self.dump())
# make sure we have the base entries # make sure we have the base entries
if self.key_settings not in content: if self.key_settings not in content:
@@ -957,6 +954,15 @@ class CfgYaml:
if self.key_profiles not in content: if self.key_profiles not in content:
content[self.key_profiles] = None 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: if self.dirty_deprecated:
# add minversion # add minversion
settings = content[self.key_settings] settings = content[self.key_settings]
@@ -966,7 +972,8 @@ class CfgYaml:
if self.debug: if self.debug:
self.log.dbg('saving to {}'.format(self.path)) self.log.dbg('saving to {}'.format(self.path))
try: try:
self._yaml_dump(content, self.path) with open(self.path, 'w') as f:
self._yaml_dump(content, f)
except Exception as e: except Exception as e:
self.log.err(e) self.log.err(e)
raise YamlException('error saving config: {}'.format(self.path)) raise YamlException('error saving config: {}'.format(self.path))
@@ -982,7 +989,10 @@ class CfgYaml:
def dump(self): def dump(self):
"""dump the config dictionary""" """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): def _load_yaml(self, path):
"""load a yaml file to a dict""" """load a yaml file to a dict"""
@@ -1002,14 +1012,13 @@ class CfgYaml:
content = y.load(f) content = y.load(f)
return content return content
def _yaml_dump(self, content, path): def _yaml_dump(self, content, where):
"""dump to yaml""" """dump to yaml"""
with open(self.path, 'w') as f: y = yaml()
y = yaml() y.default_flow_style = False
y.default_flow_style = False y.indent = 2
y.indent = 2 y.typ = 'rt'
y.typ = 'rt' y.dump(content, where)
y.dump(content, f)
######################################################## ########################################################
# helpers # helpers

View File

@@ -114,6 +114,7 @@ class Options(AttrMonitor):
self.args = docopt(USAGE, version=VERSION) self.args = docopt(USAGE, version=VERSION)
self.log = Logger() self.log = Logger()
self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ self.debug = self.args['--verbose'] or ENV_DEBUG in os.environ
self.dry = self.args['--dry']
if ENV_NODEBUG in os.environ: if ENV_NODEBUG in os.environ:
# force disabling debugs # force disabling debugs
self.debug = False self.debug = False
@@ -180,7 +181,8 @@ class Options(AttrMonitor):
def _read_config(self): def _read_config(self):
"""read the config file""" """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 # transform the config settings to self attribute
for k, v in self.conf.get_settings().items(): for k, v in self.conf.get_settings().items():
if self.debug: if self.debug:
@@ -200,7 +202,6 @@ class Options(AttrMonitor):
self.cmd_remove = self.args['remove'] self.cmd_remove = self.args['remove']
# adapt attributes based on arguments # adapt attributes based on arguments
self.dry = self.args['--dry']
self.safe = not self.args['--force'] self.safe = not self.args['--force']
# import link default value # import link default value