From d25ad19ec2da95fe49f8f23bc005b756a3f78c53 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 18 Apr 2017 12:32:58 +0200 Subject: [PATCH] keep dotpath as a relative path when importing --- dotdrop/config.py | 37 ++++++++++++++++++++++++++----------- dotdrop/dotdrop.py | 4 ++-- dotdrop/templategen.py | 4 ++-- tests/helpers.py | 4 ++-- tests/test_compare.py | 5 +++-- tests/test_config.py | 3 ++- tests/test_import.py | 5 +++-- tests/test_install.py | 5 +++-- 8 files changed, 43 insertions(+), 24 deletions(-) diff --git a/dotdrop/config.py b/dotdrop/config.py index 03b3ffa..5cd02b2 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -15,18 +15,16 @@ class Cfg: key_config = 'config' key_profiles = 'profiles' key_dotfiles = 'dotfiles' + key_dotpath = 'dotpath' key_dotfiles_src = 'src' key_dotfiles_dst = 'dst' - def __init__(self, cfgpath, dotpath): + def __init__(self, cfgpath): if not os.path.exists(cfgpath): raise ValueError('config file does not exist') self.cfgpath = cfgpath self.log = Logger() - relconf = dotpath - if not relconf.startswith(os.sep): - relconf = os.path.join(os.path.dirname(cfgpath), dotpath) - self.configs = {'dotpath': relconf} + self.configs = {} self.dotfiles = {} self.profiles = {} self.prodots = {} @@ -73,13 +71,19 @@ class Cfg: self.prodots[k] = self.dotfiles.values() else: self.prodots[k].extend([self.dotfiles[dot] for dot in v]) - # make sure we have a correct dotpath - if not self.configs['dotpath'].startswith(os.sep): - relconf = os.path.join(os.path.dirname( - self.cfgpath), self.configs['dotpath']) - self.configs['dotpath'] = relconf + # make sure we have an absolute dotpath + self.curdotpath = self.configs[self.key_dotpath] + self.configs[self.key_dotpath] = self._get_abs_dotpath(self.curdotpath) return True + def _get_abs_dotpath(self, dotpath): + """ transform dotpath to an absolute path """ + if not dotpath.startswith(os.sep): + absconf = os.path.join(os.path.dirname( + self.cfgpath), dotpath) + return absconf + return dotpath + def new(self, dotfile, profile): """ import new dotfile """ dots = self.content[self.key_dotfiles] @@ -124,11 +128,22 @@ class Cfg: def dump(self): """ dump config file """ - return yaml.dump(self.content, default_flow_style=False, indent=2) + # temporary reset dotpath + tmp = self.configs[self.key_dotpath] + self.configs[self.key_dotpath] = self.curdotpath + ret = yaml.dump(self.content, default_flow_style=False, indent=2) + # restore dotpath + self.configs[self.key_dotpath] = tmp + return ret def save(self): """ save config file to path """ + # temporary reset dotpath + tmp = self.configs[self.key_dotpath] + self.configs[self.key_dotpath] = self.curdotpath with open(self.cfgpath, 'w') as f: ret = yaml.dump(self.content, f, default_flow_style=False, indent=2) + # restore dotpath + self.configs[self.key_dotpath] = tmp return ret diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index b369a7d..dad1ca1 100755 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -16,7 +16,6 @@ from dotfile import Dotfile from config import Cfg VERSION = '0.2' -DEF_DOTFILES = 'dotfiles' CUR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) LOG = Logger() HOSTNAME = os.uname()[1] @@ -146,10 +145,11 @@ def header(): LOG.log(BANNER) LOG.log("") + if __name__ == '__main__': ret = True args = docopt(USAGE, version=VERSION) - conf = Cfg(args['--cfg'], DEF_DOTFILES) + conf = Cfg(args['--cfg']) opts = conf.get_configs() opts['dry'] = args['--dry'] diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index 97b0760..63256c1 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -43,9 +43,9 @@ class Templategen: return self._handle_text_file(src, profile) def _handle_text_file(self, src, profile): - l = len(self.base) + 1 + length = len(self.base) + 1 try: - template = self.env.get_template(src[l:]) + template = self.env.get_template(src[length:]) content = template.render(profile=profile) except UnicodeDecodeError: data = self._read_bad_encoded_text(src) diff --git a/tests/helpers.py b/tests/helpers.py index 6060d8d..35c7c1b 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -60,9 +60,9 @@ def create_dir(path): return path -def load_config(confpath, dotpath, profile): +def load_config(confpath, profile): '''Load the config file from path''' - conf = Cfg(confpath, dotpath) + conf = Cfg(confpath) opts = conf.get_configs() opts['dry'] = False opts['profile'] = profile diff --git a/tests/test_compare.py b/tests/test_compare.py index 7120893..9b22e6b 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -95,12 +95,12 @@ class TestCompare(unittest.TestCase): backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE) self.assertTrue(os.path.exists(confpath)) - conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) + conf, opts = load_config(confpath, profile) dfiles = [d1, d2, d3, d4, d5] # import the files importer(opts, conf, dfiles) - conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) + conf, opts = load_config(confpath, profile) # compare the files expected = {d1: True, d2: True, d3: True, d4: True, d5: True} @@ -137,5 +137,6 @@ class TestCompare(unittest.TestCase): def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/test_config.py b/tests/test_config.py index 952c748..1377f76 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -33,7 +33,7 @@ class TestConfig(unittest.TestCase): dotpath=self.CONFIG_DOTPATH, backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE) - conf = Cfg(confpath, self.CONFIG_DOTPATH) + conf = Cfg(confpath) self.assertTrue(conf is not None) opts = conf.get_configs() @@ -49,5 +49,6 @@ class TestConfig(unittest.TestCase): def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/test_import.py b/tests/test_import.py index 315318e..393d25e 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -70,7 +70,7 @@ class TestImport(unittest.TestCase): backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE) self.assertTrue(os.path.exists(confpath)) - conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) + conf, opts = load_config(confpath, profile) # create some random dotfiles dotfile1, content1 = create_random_file(src) @@ -100,7 +100,7 @@ class TestImport(unittest.TestCase): importer(opts, conf, dfiles) # reload the config - conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) + conf, opts = load_config(confpath, profile) # test dotfiles in config class self.assertTrue(profile in conf.get_profiles()) @@ -133,5 +133,6 @@ class TestImport(unittest.TestCase): def main(): unittest.main() + if __name__ == '__main__': main() diff --git a/tests/test_install.py b/tests/test_install.py index a498f10..5c17be0 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -82,11 +82,11 @@ exec bspwm profile = get_string(5) confpath = os.path.join(tmp, self.CONFIG_NAME) self.fake_config(confpath, [d1, d2, d3, d4], profile, tmp) - conf = Cfg(confpath, tmp) + conf = Cfg(confpath) self.assertTrue(conf is not None) # install them - conf, opts = load_config(confpath, tmp, profile) + conf, opts = load_config(confpath, profile) opts['safe'] = False install(opts, conf) @@ -108,5 +108,6 @@ exec bspwm def main(): unittest.main() + if __name__ == '__main__': main()