1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 03:14:15 +00:00

refactor configs to settings for clarity

This commit is contained in:
deadc0de6
2018-06-02 14:56:15 +02:00
parent 0052571d2e
commit ea3cc6663b
4 changed files with 44 additions and 44 deletions

View File

@@ -17,22 +17,22 @@ from dotdrop.action import Action, Transform
class Cfg: class Cfg:
key_all = 'ALL' key_all = 'ALL'
# key configs # settings keys
key_config = 'config' key_settings = 'config'
key_dotpath = 'dotpath' key_dotpath = 'dotpath'
key_backup = 'backup' key_backup = 'backup'
key_create = 'create' key_create = 'create'
key_banner = 'banner' key_banner = 'banner'
# key actions # actions keys
key_actions = 'actions' key_actions = 'actions'
key_actions_pre = 'pre' key_actions_pre = 'pre'
key_actions_post = 'post' key_actions_post = 'post'
# key transformations # transformations keys
key_trans = 'trans' key_trans = 'trans'
# key dotfiles # dotfiles keys
key_dotfiles = 'dotfiles' key_dotfiles = 'dotfiles'
key_dotfiles_src = 'src' key_dotfiles_src = 'src'
key_dotfiles_dst = 'dst' key_dotfiles_dst = 'dst'
@@ -40,12 +40,12 @@ class Cfg:
key_dotfiles_actions = 'actions' key_dotfiles_actions = 'actions'
key_dotfiles_trans = 'trans' key_dotfiles_trans = 'trans'
# key profiles # profiles keys
key_profiles = 'profiles' key_profiles = 'profiles'
key_profiles_dots = 'dotfiles' key_profiles_dots = 'dotfiles'
key_profiles_incl = 'include' key_profiles_incl = 'include'
# config defaults # settings defaults
default_backup = True default_backup = True
default_create = True default_create = True
default_banner = True default_banner = True
@@ -57,7 +57,7 @@ class Cfg:
self.cfgpath = cfgpath self.cfgpath = cfgpath
self.log = Logger() self.log = Logger()
# link inside content # link inside content
self.configs = {} self.settings = {}
# link inside content # link inside content
self.profiles = {} self.profiles = {}
# not linked to content # not linked to content
@@ -84,8 +84,8 @@ class Cfg:
if self.key_profiles not in self.content: if self.key_profiles not in self.content:
self.log.err('missing \"{}\" in config'.format(self.key_profiles)) self.log.err('missing \"{}\" in config'.format(self.key_profiles))
return False return False
if self.key_config not in self.content: if self.key_settings not in self.content:
self.log.err('missing \"{}\" in config'.format(self.key_config)) self.log.err('missing \"{}\" in config'.format(self.key_settings))
return False return False
if self.key_dotfiles not in self.content: if self.key_dotfiles not in self.content:
self.log.err('missing \"{}\" in config'.format(self.key_dotfiles)) self.log.err('missing \"{}\" in config'.format(self.key_dotfiles))
@@ -144,14 +144,14 @@ class Cfg:
res.append(trans[entry]) res.append(trans[entry])
return res return res
def _complete_configs(self): def _complete_settings(self):
"""set config defaults if not present""" """set settings defaults if not present"""
if self.key_backup not in self.configs: if self.key_backup not in self.settings:
self.configs[self.key_backup] = self.default_backup self.settings[self.key_backup] = self.default_backup
if self.key_create not in self.configs: if self.key_create not in self.settings:
self.configs[self.key_create] = self.default_create self.settings[self.key_create] = self.default_create
if self.key_banner not in self.configs: if self.key_banner not in self.settings:
self.configs[self.key_banner] = self.default_banner self.settings[self.key_banner] = self.default_banner
def _parse(self): def _parse(self):
"""parse config file""" """parse config file"""
@@ -184,9 +184,9 @@ class Cfg:
v[self.key_profiles_dots] is None: v[self.key_profiles_dots] is None:
v[self.key_profiles_dots] = [] v[self.key_profiles_dots] = []
# parse the configs # parse the settings
self.configs = self.content[self.key_config] self.settings = self.content[self.key_settings]
self._complete_configs() self._complete_settings()
# parse the dotfiles # parse the dotfiles
if not self.content[self.key_dotfiles]: if not self.content[self.key_dotfiles]:
@@ -232,8 +232,8 @@ class Cfg:
self.prodots[k] = list(set(self.prodots[k])) self.prodots[k] = list(set(self.prodots[k]))
# make sure we have an absolute dotpath # make sure we have an absolute dotpath
self.curdotpath = self.configs[self.key_dotpath] self.curdotpath = self.settings[self.key_dotpath]
self.configs[self.key_dotpath] = self.get_abs_dotpath(self.curdotpath) self.settings[self.key_dotpath] = self.get_abs_dotpath(self.curdotpath)
return True return True
def _get_included_dotfiles(self, profile): def _get_included_dotfiles(self, profile):
@@ -353,40 +353,40 @@ class Cfg:
"""return all defined profiles""" """return all defined profiles"""
return self.profiles.keys() return self.profiles.keys()
def get_configs(self): def get_settings(self):
"""return all defined configs""" """return all defined settings"""
return self.configs.copy() return self.settings.copy()
def dump(self): def dump(self):
"""return a dump of the config""" """return a dump of the config"""
# temporary reset dotpath # temporary reset dotpath
dotpath = self.configs[self.key_dotpath] dotpath = self.settings[self.key_dotpath]
self.configs[self.key_dotpath] = self.curdotpath self.settings[self.key_dotpath] = self.curdotpath
# reset banner # reset banner
if self.configs[self.key_banner]: if self.settings[self.key_banner]:
del self.configs[self.key_banner] del self.settings[self.key_banner]
# dump # dump
ret = yaml.dump(self.content, default_flow_style=False, indent=2) ret = yaml.dump(self.content, default_flow_style=False, indent=2)
# restore dotpath # restore dotpath
self.configs[self.key_dotpath] = dotpath self.settings[self.key_dotpath] = dotpath
# restore banner # restore banner
if self.key_banner not in self.configs: if self.key_banner not in self.settings:
self.configs[self.key_banner] = self.default_banner self.settings[self.key_banner] = self.default_banner
return ret return ret
def save(self): def save(self):
"""save the config to file""" """save the config to file"""
# temporary reset dotpath # temporary reset dotpath
dotpath = self.configs[self.key_dotpath] dotpath = self.settings[self.key_dotpath]
self.configs[self.key_dotpath] = self.curdotpath self.settings[self.key_dotpath] = self.curdotpath
# reset banner # reset banner
if self.configs[self.key_banner]: if self.settings[self.key_banner]:
del self.configs[self.key_banner] del self.settings[self.key_banner]
# save # save
ret = self._save(self.content, self.cfgpath) ret = self._save(self.content, self.cfgpath)
# restore dotpath # restore dotpath
self.configs[self.key_dotpath] = dotpath self.settings[self.key_dotpath] = dotpath
# restore banner # restore banner
if self.key_banner not in self.configs: if self.key_banner not in self.settings:
self.configs[self.key_banner] = self.default_banner self.settings[self.key_banner] = self.default_banner
return ret return ret

View File

@@ -314,7 +314,7 @@ def main():
LOG.err('error: {}'.format(str(e))) LOG.err('error: {}'.format(str(e)))
return False return False
opts = conf.get_configs() opts = conf.get_settings()
opts['dry'] = args['--dry'] opts['dry'] = args['--dry']
opts['profile'] = args['--profile'] opts['profile'] = args['--profile']
opts['safe'] = not args['--force'] opts['safe'] = not args['--force']

View File

@@ -65,7 +65,7 @@ def create_dir(path):
def load_config(confpath, profile): def load_config(confpath, profile):
'''Load the config file from path''' '''Load the config file from path'''
conf = Cfg(confpath) conf = Cfg(confpath)
opts = conf.get_configs() opts = conf.get_settings()
opts['dry'] = False opts['dry'] = False
opts['profile'] = profile opts['profile'] = profile
opts['safe'] = True opts['safe'] = True

View File

@@ -37,7 +37,7 @@ class TestConfig(unittest.TestCase):
conf = Cfg(confpath) conf = Cfg(confpath)
self.assertTrue(conf is not None) self.assertTrue(conf is not None)
opts = conf.get_configs() opts = conf.get_settings()
self.assertTrue(opts is not None) self.assertTrue(opts is not None)
self.assertTrue(opts != {}) self.assertTrue(opts != {})
self.assertTrue(opts['backup'] == self.CONFIG_BACKUP) self.assertTrue(opts['backup'] == self.CONFIG_BACKUP)
@@ -88,7 +88,7 @@ class TestConfig(unittest.TestCase):
self.assertTrue(conf is not None) self.assertTrue(conf is not None)
# test profile # test profile
opts = conf.get_configs() opts = conf.get_settings()
print(conf.get_profiles()) print(conf.get_profiles())
profiles = conf.get_profiles() profiles = conf.get_profiles()
self.assertTrue(pf1key in profiles) self.assertTrue(pf1key in profiles)