1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-08 13:09: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_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)