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

keep dotpath as a relative path when importing

This commit is contained in:
deadc0de6
2017-04-18 12:32:58 +02:00
parent 2c052f8b97
commit d25ad19ec2
8 changed files with 43 additions and 24 deletions

View File

@@ -15,18 +15,16 @@ class Cfg:
key_config = 'config' key_config = 'config'
key_profiles = 'profiles' key_profiles = 'profiles'
key_dotfiles = 'dotfiles' key_dotfiles = 'dotfiles'
key_dotpath = 'dotpath'
key_dotfiles_src = 'src' key_dotfiles_src = 'src'
key_dotfiles_dst = 'dst' key_dotfiles_dst = 'dst'
def __init__(self, cfgpath, dotpath): def __init__(self, cfgpath):
if not os.path.exists(cfgpath): if not os.path.exists(cfgpath):
raise ValueError('config file does not exist') raise ValueError('config file does not exist')
self.cfgpath = cfgpath self.cfgpath = cfgpath
self.log = Logger() self.log = Logger()
relconf = dotpath self.configs = {}
if not relconf.startswith(os.sep):
relconf = os.path.join(os.path.dirname(cfgpath), dotpath)
self.configs = {'dotpath': relconf}
self.dotfiles = {} self.dotfiles = {}
self.profiles = {} self.profiles = {}
self.prodots = {} self.prodots = {}
@@ -73,13 +71,19 @@ class Cfg:
self.prodots[k] = self.dotfiles.values() self.prodots[k] = self.dotfiles.values()
else: else:
self.prodots[k].extend([self.dotfiles[dot] for dot in v]) self.prodots[k].extend([self.dotfiles[dot] for dot in v])
# make sure we have a correct dotpath # make sure we have an absolute dotpath
if not self.configs['dotpath'].startswith(os.sep): self.curdotpath = self.configs[self.key_dotpath]
relconf = os.path.join(os.path.dirname( self.configs[self.key_dotpath] = self._get_abs_dotpath(self.curdotpath)
self.cfgpath), self.configs['dotpath'])
self.configs['dotpath'] = relconf
return True 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): def new(self, dotfile, profile):
""" import new dotfile """ """ import new dotfile """
dots = self.content[self.key_dotfiles] dots = self.content[self.key_dotfiles]
@@ -124,11 +128,22 @@ class Cfg:
def dump(self): def dump(self):
""" dump config file """ """ 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): def save(self):
""" save config file to path """ """ 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: with open(self.cfgpath, 'w') as f:
ret = yaml.dump(self.content, f, ret = yaml.dump(self.content, f,
default_flow_style=False, indent=2) default_flow_style=False, indent=2)
# restore dotpath
self.configs[self.key_dotpath] = tmp
return ret return ret

View File

@@ -16,7 +16,6 @@ from dotfile import Dotfile
from config import Cfg from config import Cfg
VERSION = '0.2' VERSION = '0.2'
DEF_DOTFILES = 'dotfiles'
CUR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CUR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOG = Logger() LOG = Logger()
HOSTNAME = os.uname()[1] HOSTNAME = os.uname()[1]
@@ -146,10 +145,11 @@ def header():
LOG.log(BANNER) LOG.log(BANNER)
LOG.log("") LOG.log("")
if __name__ == '__main__': if __name__ == '__main__':
ret = True ret = True
args = docopt(USAGE, version=VERSION) args = docopt(USAGE, version=VERSION)
conf = Cfg(args['--cfg'], DEF_DOTFILES) conf = Cfg(args['--cfg'])
opts = conf.get_configs() opts = conf.get_configs()
opts['dry'] = args['--dry'] opts['dry'] = args['--dry']

View File

@@ -43,9 +43,9 @@ class Templategen:
return self._handle_text_file(src, profile) return self._handle_text_file(src, profile)
def _handle_text_file(self, src, profile): def _handle_text_file(self, src, profile):
l = len(self.base) + 1 length = len(self.base) + 1
try: try:
template = self.env.get_template(src[l:]) template = self.env.get_template(src[length:])
content = template.render(profile=profile) content = template.render(profile=profile)
except UnicodeDecodeError: except UnicodeDecodeError:
data = self._read_bad_encoded_text(src) data = self._read_bad_encoded_text(src)

View File

@@ -60,9 +60,9 @@ def create_dir(path):
return path return path
def load_config(confpath, dotpath, profile): def load_config(confpath, profile):
'''Load the config file from path''' '''Load the config file from path'''
conf = Cfg(confpath, dotpath) conf = Cfg(confpath)
opts = conf.get_configs() opts = conf.get_configs()
opts['dry'] = False opts['dry'] = False
opts['profile'] = profile opts['profile'] = profile

View File

@@ -95,12 +95,12 @@ class TestCompare(unittest.TestCase):
backup=self.CONFIG_BACKUP, backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE) create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath)) 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] dfiles = [d1, d2, d3, d4, d5]
# import the files # import the files
importer(opts, conf, dfiles) importer(opts, conf, dfiles)
conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) conf, opts = load_config(confpath, profile)
# compare the files # compare the files
expected = {d1: True, d2: True, d3: True, d4: True, d5: True} expected = {d1: True, d2: True, d3: True, d4: True, d5: True}
@@ -137,5 +137,6 @@ class TestCompare(unittest.TestCase):
def main(): def main():
unittest.main() unittest.main()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -33,7 +33,7 @@ class TestConfig(unittest.TestCase):
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, self.CONFIG_DOTPATH) conf = Cfg(confpath)
self.assertTrue(conf is not None) self.assertTrue(conf is not None)
opts = conf.get_configs() opts = conf.get_configs()
@@ -49,5 +49,6 @@ class TestConfig(unittest.TestCase):
def main(): def main():
unittest.main() unittest.main()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -70,7 +70,7 @@ class TestImport(unittest.TestCase):
backup=self.CONFIG_BACKUP, backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE) create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath)) 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 # create some random dotfiles
dotfile1, content1 = create_random_file(src) dotfile1, content1 = create_random_file(src)
@@ -100,7 +100,7 @@ class TestImport(unittest.TestCase):
importer(opts, conf, dfiles) importer(opts, conf, dfiles)
# reload the config # reload the config
conf, opts = load_config(confpath, self.CONFIG_DOTPATH, profile) conf, opts = load_config(confpath, profile)
# test dotfiles in config class # test dotfiles in config class
self.assertTrue(profile in conf.get_profiles()) self.assertTrue(profile in conf.get_profiles())
@@ -133,5 +133,6 @@ class TestImport(unittest.TestCase):
def main(): def main():
unittest.main() unittest.main()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -82,11 +82,11 @@ exec bspwm
profile = get_string(5) profile = get_string(5)
confpath = os.path.join(tmp, self.CONFIG_NAME) confpath = os.path.join(tmp, self.CONFIG_NAME)
self.fake_config(confpath, [d1, d2, d3, d4], profile, tmp) self.fake_config(confpath, [d1, d2, d3, d4], profile, tmp)
conf = Cfg(confpath, tmp) conf = Cfg(confpath)
self.assertTrue(conf is not None) self.assertTrue(conf is not None)
# install them # install them
conf, opts = load_config(confpath, tmp, profile) conf, opts = load_config(confpath, profile)
opts['safe'] = False opts['safe'] = False
install(opts, conf) install(opts, conf)
@@ -108,5 +108,6 @@ exec bspwm
def main(): def main():
unittest.main() unittest.main()
if __name__ == '__main__': if __name__ == '__main__':
main() main()