1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +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'
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

View File

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

View File

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

View File

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