1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-14 15:52:27 +00:00

propagate undefined variable exception

This commit is contained in:
deadc0de6
2020-07-14 20:32:05 +02:00
parent 7775184d06
commit c36854f250
7 changed files with 56 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import os
# local imports # local imports
from dotdrop.dictparser import DictParser from dotdrop.dictparser import DictParser
from dotdrop.exceptions import UndefinedException
class Cmd(DictParser): class Cmd(DictParser):
@@ -32,7 +33,12 @@ class Cmd(DictParser):
ret = 1 ret = 1
action = self.action action = self.action
if templater: if templater:
action = templater.generate_string(self.action) try:
action = templater.generate_string(self.action)
except UndefinedException as e:
err = 'bad {}: {}'.format(self.descr, e)
self.log.warn(err)
return False
if debug: if debug:
self.log.dbg('{}:'.format(self.descr)) self.log.dbg('{}:'.format(self.descr))
self.log.dbg(' - raw \"{}\"'.format(self.action)) self.log.dbg(' - raw \"{}\"'.format(self.action))
@@ -42,7 +48,12 @@ class Cmd(DictParser):
if self.args: if self.args:
args = self.args args = self.args
if templater: if templater:
args = [templater.generate_string(a) for a in args] try:
args = [templater.generate_string(a) for a in args]
except UndefinedException as e:
err = 'bad arguments for {}: {}'.format(self.descr, e)
self.log.warn(err)
return False
if debug and args: if debug and args:
self.log.dbg('action args:') self.log.dbg('action args:')
for cnt, arg in enumerate(args): for cnt, arg in enumerate(args):

View File

@@ -17,6 +17,7 @@ from dotdrop.profile import Profile
from dotdrop.action import Action, Transform from dotdrop.action import Action, Transform
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.utils import strip_home from dotdrop.utils import strip_home
from dotdrop.exceptions import UndefinedException
TILD = '~' TILD = '~'
@@ -281,7 +282,12 @@ class CfgAggregator:
@src: dotfile src (in dotpath) @src: dotfile src (in dotpath)
@dst: dotfile dst (on filesystem) @dst: dotfile dst (on filesystem)
""" """
src = self.cfgyaml.resolve_dotfile_src(src) try:
src = self.cfgyaml.resolve_dotfile_src(src)
except UndefinedException as e:
err = 'unable to resolve {}: {}'
self.log.err(err.format(src, e))
return None
dotfiles = self.get_dotfile_by_dst(dst) dotfiles = self.get_dotfile_by_dst(dst)
for d in dotfiles: for d in dotfiles:
if d.src == src: if d.src == src:

View File

@@ -20,7 +20,7 @@ from dotdrop.comparator import Comparator
from dotdrop.utils import get_tmpdir, remove, strip_home, \ from dotdrop.utils import get_tmpdir, remove, strip_home, \
run, uniq_list, patch_ignores, dependencies_met run, uniq_list, patch_ignores, dependencies_met
from dotdrop.linktypes import LinkTypes from dotdrop.linktypes import LinkTypes
from dotdrop.exceptions import YamlException from dotdrop.exceptions import YamlException, UndefinedException
LOG = Logger() LOG = Logger()
TRANS_SUFFIX = 'trans' TRANS_SUFFIX = 'trans'
@@ -655,7 +655,10 @@ def main():
try: try:
o = Options() o = Options()
except YamlException as e: except YamlException as e:
LOG.err('config file error: {}'.format(str(e))) LOG.err('config error: {}'.format(str(e)))
return False
except UndefinedException as e:
LOG.err('config error: {}'.format(str(e)))
return False return False
if o.debug: if o.debug:

View File

@@ -9,3 +9,8 @@ diverse exceptions
class YamlException(Exception): class YamlException(Exception):
"""exception in CfgYaml""" """exception in CfgYaml"""
pass pass
class UndefinedException(Exception):
"""exception in templating"""
pass

View File

@@ -12,6 +12,7 @@ import errno
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.templategen import Templategen from dotdrop.templategen import Templategen
import dotdrop.utils as utils import dotdrop.utils as utils
from dotdrop.exceptions import UndefinedException
class Installer: class Installer:
@@ -325,8 +326,12 @@ class Installer:
err = 'dotfile points to itself: {}'.format(dst) err = 'dotfile points to itself: {}'.format(dst)
return False, err return False, err
saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst)) saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
content = templater.generate(src) try:
templater.restore_vars(saved) content = templater.generate(src)
except UndefinedException as e:
return False, e.message
finally:
templater.restore_vars(saved)
if noempty and utils.content_empty(content): if noempty and utils.content_empty(content):
if self.debug: if self.debug:
self.log.dbg('ignoring empty template: {}'.format(src)) self.log.dbg('ignoring empty template: {}'.format(src))

View File

@@ -9,11 +9,14 @@ import os
from jinja2 import Environment, FileSystemLoader, \ from jinja2 import Environment, FileSystemLoader, \
ChoiceLoader, FunctionLoader, TemplateNotFound, \ ChoiceLoader, FunctionLoader, TemplateNotFound, \
StrictUndefined StrictUndefined
from jinja2.exceptions import UndefinedError
# local imports # local imports
import dotdrop.utils as utils import dotdrop.utils as utils
from dotdrop.logger import Logger from dotdrop.logger import Logger
import dotdrop.jhelpers as jhelpers import dotdrop.jhelpers as jhelpers
from dotdrop.exceptions import UndefinedException
BLOCK_START = '{%@@' BLOCK_START = '{%@@'
BLOCK_END = '@@%}' BLOCK_END = '@@%}'
@@ -79,22 +82,28 @@ class Templategen:
def generate(self, src): def generate(self, src):
""" """
render template from path render template from path
may raise a jinja2.exceptions.UndefinedError may raise a UndefinedException
in case a variable is undefined in case a variable is undefined
""" """
if not os.path.exists(src): if not os.path.exists(src):
return '' return ''
return self._handle_file(src) try:
return self._handle_file(src)
except UndefinedError as e:
raise UndefinedException(e.message)
def generate_string(self, string): def generate_string(self, string):
""" """
render template from string render template from string
may raise a jinja2.exceptions.UndefinedError may raise a UndefinedException
in case a variable is undefined in case a variable is undefined
""" """
if not string: if not string:
return '' return ''
return self.env.from_string(string).render(self.variables) try:
return self.env.from_string(string).render(self.variables)
except UndefinedError as e:
raise UndefinedException(e.message)
def add_tmp_vars(self, newvars={}): def add_tmp_vars(self, newvars={}):
"""add vars to the globals, make sure to call restore_vars""" """add vars to the globals, make sure to call restore_vars"""

View File

@@ -14,6 +14,7 @@ from dotdrop.logger import Logger
from dotdrop.templategen import Templategen from dotdrop.templategen import Templategen
from dotdrop.utils import patch_ignores, remove, get_unique_tmp_name, \ from dotdrop.utils import patch_ignores, remove, get_unique_tmp_name, \
write_to_tmpfile, must_ignore, mirror_file_rights write_to_tmpfile, must_ignore, mirror_file_rights
from dotdrop.exceptions import UndefinedException
TILD = '~' TILD = '~'
@@ -186,7 +187,11 @@ class Updater:
if self.debug: if self.debug:
self.log.dbg('{} is a template'.format(dtpath)) self.log.dbg('{} is a template'.format(dtpath))
if self.showpatch: if self.showpatch:
self._show_patch(path, dtpath) try:
self._show_patch(path, dtpath)
except UndefinedException as e:
msg = 'unable to show patch for {}: {}'.format(path, e)
self.log.warn(msg)
return False return False
if compare and filecmp.cmp(path, dtpath, shallow=False) and \ if compare and filecmp.cmp(path, dtpath, shallow=False) and \
self._same_rights(path, dtpath): self._same_rights(path, dtpath):