From 1e5eeaca9bebaa488567c0614f19f21e860805f7 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 10 Apr 2020 14:02:16 +0200 Subject: [PATCH] refactoring --- dotdrop/cfg_aggregator.py | 49 +++++++++++++++++++-------------------- dotdrop/dotdrop.py | 6 ++--- dotdrop/options.py | 2 +- tests/test_yamlcfg.py | 5 ++-- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 5791871..9063b57 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -28,15 +28,15 @@ class CfgAggregator: dir_prefix = 'd' key_sep = '_' - def __init__(self, path, profile=None, debug=False): + def __init__(self, path, profile_key, debug=False): """ high level config parser @path: path to the config file - @profile: selected profile + @profile_key: profile key @debug: debug flag """ self.path = path - self.profile = profile + self.profile_key = profile_key self.debug = debug self.log = Logger() self._load() @@ -44,7 +44,7 @@ class CfgAggregator: def _load(self): """load lower level config""" self.cfgyaml = CfgYaml(self.path, - self.profile, + self.profile_key, debug=self.debug) # settings @@ -147,13 +147,12 @@ class CfgAggregator: """remove this dotfile from this profile""" 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 @src: path in dotpath @dst: path in FS @link: LinkType - @profile_key: to which profile """ dst = self.path_to_dotfile_dst(dst) @@ -168,15 +167,15 @@ class CfgAggregator: dotfile = Dotfile(key, dst, src) 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: - self.log.dbg('new dotfile {} to profile {}'.format(key, - profile_key)) + msg = 'new dotfile {} to profile {}' + self.log.dbg(msg.format(key, self.profile_key)) # reload self.cfgyaml.save() if self.debug: - self.log.dbg('RELOADING') + self.log.dbg('reloading config') self._load() return ret @@ -286,10 +285,10 @@ class CfgAggregator: """return profiles""" return self.profiles - def get_profile(self, key): - """return profile by key""" + def get_profile(self): + """return profile object""" 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: return None @@ -302,22 +301,22 @@ class CfgAggregator: res.append(p) return res - def get_dotfiles(self, profile=None): - """return dotfiles dict for this profile key""" + def get_dotfiles(self): + """get all dotfiles for this profile""" + dotfiles = [] + profile = self.get_profile() if not profile: - return self.dotfiles - try: - pro = self.get_profile(profile) - if not pro: - return [] - return pro.dotfiles - except StopIteration: - return [] + return dotfiles + return profile.dotfiles def get_dotfile(self, key): - """return dotfile by key""" + """ + return dotfile object by key + @key: the dotfile key to look for + """ 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: return None diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 57efdf9..d9c0712 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -71,7 +71,7 @@ def action_executor(o, actions, defactions, templater, post=False): def cmd_install(o): """install dotfiles for this profile""" 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_post_actions = prof.get_post_actions() if prof else [] @@ -356,7 +356,7 @@ def cmd_importer(o): continue 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 srcf = os.path.join(o.dotpath, src) @@ -395,7 +395,7 @@ def cmd_importer(o): LOG.err('importing \"{}\" failed!'.format(path)) ret = False continue - retconf = o.conf.new(src, dst, linktype, o.profile) + retconf = o.conf.new(src, dst, linktype) if retconf: LOG.sub('\"{}\" imported'.format(path)) cnt += 1 diff --git a/dotdrop/options.py b/dotdrop/options.py index de7babc..cfe7f0f 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -256,7 +256,7 @@ class Options(AttrMonitor): # variables self.variables = self.conf.get_variables() # the dotfiles - self.dotfiles = self.conf.get_dotfiles(self.profile) + self.dotfiles = self.conf.get_dotfiles() # the profiles self.profiles = self.conf.get_profiles() diff --git a/tests/test_yamlcfg.py b/tests/test_yamlcfg.py index ece36d3..2b53988 100644 --- a/tests/test_yamlcfg.py +++ b/tests/test_yamlcfg.py @@ -22,6 +22,7 @@ class TestConfig(SubsetTestCase): CONFIG_BACKUP = False CONFIG_CREATE = True CONFIG_DOTPATH = 'dotfiles' + PROFILE = 'p1' TMPSUFFIX = '.dotdrop' CONFIG_NAME = 'config.yaml' CONFIG_NAME_2 = 'config-2.yaml' @@ -37,7 +38,7 @@ class TestConfig(SubsetTestCase): dotpath=self.CONFIG_DOTPATH, backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE) - conf = Cfg(confpath, debug=True) + conf = Cfg(confpath, self.PROFILE, debug=True) self.assertTrue(conf is not None) opts = conf.settings @@ -90,7 +91,7 @@ profiles: mock_exists.return_value = True args = _fake_args() - args['--profile'] = 'p1' + args['--profile'] = self.PROFILE args['--cfg'] = 'mocked' args['--link'] = cliargs args['--verbose'] = True