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

refactoring

This commit is contained in:
deadc0de6
2020-04-10 14:02:16 +02:00
parent 79431e0976
commit 1e5eeaca9b
4 changed files with 31 additions and 31 deletions

View File

@@ -28,15 +28,15 @@ class CfgAggregator:
dir_prefix = 'd' dir_prefix = 'd'
key_sep = '_' key_sep = '_'
def __init__(self, path, profile=None, debug=False): def __init__(self, path, profile_key, debug=False):
""" """
high level config parser high level config parser
@path: path to the config file @path: path to the config file
@profile: selected profile @profile_key: profile key
@debug: debug flag @debug: debug flag
""" """
self.path = path self.path = path
self.profile = profile self.profile_key = profile_key
self.debug = debug self.debug = debug
self.log = Logger() self.log = Logger()
self._load() self._load()
@@ -44,7 +44,7 @@ class CfgAggregator:
def _load(self): def _load(self):
"""load lower level config""" """load lower level config"""
self.cfgyaml = CfgYaml(self.path, self.cfgyaml = CfgYaml(self.path,
self.profile, self.profile_key,
debug=self.debug) debug=self.debug)
# settings # settings
@@ -147,13 +147,12 @@ class CfgAggregator:
"""remove this dotfile from this profile""" """remove this dotfile from this profile"""
return self.cfgyaml.del_dotfile_from_profile(dotfile.key, profile.key) return self.cfgyaml.del_dotfile_from_profile(dotfile.key, profile.key)
def new(self, src, dst, link, profile_key): def new(self, src, dst, link):
""" """
import a new dotfile import a new dotfile
@src: path in dotpath @src: path in dotpath
@dst: path in FS @dst: path in FS
@link: LinkType @link: LinkType
@profile_key: to which profile
""" """
dst = self.path_to_dotfile_dst(dst) dst = self.path_to_dotfile_dst(dst)
@@ -168,15 +167,15 @@ class CfgAggregator:
dotfile = Dotfile(key, dst, src) dotfile = Dotfile(key, dst, src)
key = dotfile.key key = dotfile.key
ret = self.cfgyaml.add_dotfile_to_profile(key, profile_key) ret = self.cfgyaml.add_dotfile_to_profile(key, self.profile_key)
if self.debug: if self.debug:
self.log.dbg('new dotfile {} to profile {}'.format(key, msg = 'new dotfile {} to profile {}'
profile_key)) self.log.dbg(msg.format(key, self.profile_key))
# reload # reload
self.cfgyaml.save() self.cfgyaml.save()
if self.debug: if self.debug:
self.log.dbg('RELOADING') self.log.dbg('reloading config')
self._load() self._load()
return ret return ret
@@ -286,10 +285,10 @@ class CfgAggregator:
"""return profiles""" """return profiles"""
return self.profiles return self.profiles
def get_profile(self, key): def get_profile(self):
"""return profile by key""" """return profile object"""
try: try:
return next(x for x in self.profiles if x.key == key) return next(x for x in self.profiles if x.key == self.profile_key)
except StopIteration: except StopIteration:
return None return None
@@ -302,22 +301,22 @@ class CfgAggregator:
res.append(p) res.append(p)
return res return res
def get_dotfiles(self, profile=None): def get_dotfiles(self):
"""return dotfiles dict for this profile key""" """get all dotfiles for this profile"""
dotfiles = []
profile = self.get_profile()
if not profile: if not profile:
return self.dotfiles return dotfiles
try: return profile.dotfiles
pro = self.get_profile(profile)
if not pro:
return []
return pro.dotfiles
except StopIteration:
return []
def get_dotfile(self, key): def get_dotfile(self, key):
"""return dotfile by key""" """
return dotfile object by key
@key: the dotfile key to look for
"""
try: try:
return next(x for x in self.dotfiles if x.key == key) return next(x for x in self.dotfiles
if x.key == key)
except StopIteration: except StopIteration:
return None return None

View File

@@ -71,7 +71,7 @@ def action_executor(o, actions, defactions, templater, post=False):
def cmd_install(o): def cmd_install(o):
"""install dotfiles for this profile""" """install dotfiles for this profile"""
dotfiles = o.dotfiles dotfiles = o.dotfiles
prof = o.conf.get_profile(o.profile) prof = o.conf.get_profile()
pro_pre_actions = prof.get_pre_actions() if prof else [] pro_pre_actions = prof.get_pre_actions() if prof else []
pro_post_actions = prof.get_post_actions() if prof else [] pro_post_actions = prof.get_post_actions() if prof else []
@@ -356,7 +356,7 @@ def cmd_importer(o):
continue continue
if o.debug: if o.debug:
LOG.dbg('new dotfile: src:{} dst:{}'.format(src, dst)) LOG.dbg('import dotfile: src:{} dst:{}'.format(src, dst))
# prepare hierarchy for dotfile # prepare hierarchy for dotfile
srcf = os.path.join(o.dotpath, src) srcf = os.path.join(o.dotpath, src)
@@ -395,7 +395,7 @@ def cmd_importer(o):
LOG.err('importing \"{}\" failed!'.format(path)) LOG.err('importing \"{}\" failed!'.format(path))
ret = False ret = False
continue continue
retconf = o.conf.new(src, dst, linktype, o.profile) retconf = o.conf.new(src, dst, linktype)
if retconf: if retconf:
LOG.sub('\"{}\" imported'.format(path)) LOG.sub('\"{}\" imported'.format(path))
cnt += 1 cnt += 1

View File

@@ -256,7 +256,7 @@ class Options(AttrMonitor):
# variables # variables
self.variables = self.conf.get_variables() self.variables = self.conf.get_variables()
# the dotfiles # the dotfiles
self.dotfiles = self.conf.get_dotfiles(self.profile) self.dotfiles = self.conf.get_dotfiles()
# the profiles # the profiles
self.profiles = self.conf.get_profiles() self.profiles = self.conf.get_profiles()

View File

@@ -22,6 +22,7 @@ class TestConfig(SubsetTestCase):
CONFIG_BACKUP = False CONFIG_BACKUP = False
CONFIG_CREATE = True CONFIG_CREATE = True
CONFIG_DOTPATH = 'dotfiles' CONFIG_DOTPATH = 'dotfiles'
PROFILE = 'p1'
TMPSUFFIX = '.dotdrop' TMPSUFFIX = '.dotdrop'
CONFIG_NAME = 'config.yaml' CONFIG_NAME = 'config.yaml'
CONFIG_NAME_2 = 'config-2.yaml' CONFIG_NAME_2 = 'config-2.yaml'
@@ -37,7 +38,7 @@ class TestConfig(SubsetTestCase):
dotpath=self.CONFIG_DOTPATH, dotpath=self.CONFIG_DOTPATH,
backup=self.CONFIG_BACKUP, backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE) create=self.CONFIG_CREATE)
conf = Cfg(confpath, debug=True) conf = Cfg(confpath, self.PROFILE, debug=True)
self.assertTrue(conf is not None) self.assertTrue(conf is not None)
opts = conf.settings opts = conf.settings
@@ -90,7 +91,7 @@ profiles:
mock_exists.return_value = True mock_exists.return_value = True
args = _fake_args() args = _fake_args()
args['--profile'] = 'p1' args['--profile'] = self.PROFILE
args['--cfg'] = 'mocked' args['--cfg'] = 'mocked'
args['--link'] = cliargs args['--link'] = cliargs
args['--verbose'] = True args['--verbose'] = True