1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 16:49:42 +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_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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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