1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-05 01:34:42 +00:00

Merge pull request #238 from deadc0de6/dry-import

fix dry import for #237
This commit is contained in:
deadc0de
2020-06-13 13:41:28 +02:00
committed by GitHub
3 changed files with 33 additions and 20 deletions

View File

@@ -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):

View File

@@ -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

View File

@@ -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