From 42fdcf97d805826c77357c9ac7f2de39ed44ed9d Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 2 Apr 2014 16:56:45 +0200 Subject: [PATCH] Gestion de l'erreur : impossible de trouver le template --- songbook_core/errors.py | 14 ++++++++++++++ songbook_core/templates.py | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/songbook_core/errors.py b/songbook_core/errors.py index 7a231042..0bd5aacd 100644 --- a/songbook_core/errors.py +++ b/songbook_core/errors.py @@ -10,6 +10,20 @@ class SongbookError(Exception): """ pass +class TemplateError(SongbookError): + """Error during template generation""" + + def __init__(self, original, message = None): + super(TemplateError, self).__init__() + self.original = original + self.message = message + + def __str__(self): + if self.message is None: + return str(self.original) + else: + return self.message + class LatexCompilationError(SongbookError): """Error during LaTeX compilation.""" diff --git a/songbook_core/templates.py b/songbook_core/templates.py index dfba1deb..4b1e71db 100644 --- a/songbook_core/templates.py +++ b/songbook_core/templates.py @@ -2,13 +2,15 @@ # -*- coding: utf-8 -*- """Template for .tex generation settings and utilities""" -from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PackageLoader +from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PackageLoader, TemplateNotFound from jinja2.meta import find_referenced_templates as find_templates import os import re import json import locale +from songbook_core import errors + _LATEX_SUBS = ( (re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'([{}_#%&$])'), r'\\\1'), @@ -57,8 +59,15 @@ class TexRenderer(object): self.texenv.trim_blocks = True self.texenv.lstrip_blocks = True - # TODO: catch the TemplateNotFound - self.template = self.texenv.get_template(template) + try: + self.template = self.texenv.get_template(template) + except TemplateNotFound as exception: + raise errors.TemplateError( + exception, + """Template "{template}" not found.""".format( + template=exception.name + ), + ) # Trick to get the language code self.lang = locale.getdefaultlocale()[0].split('-')[0].split('_')[0]