Browse Source

Gestion de l'erreur : impossible de trouver le template

pull/22/head
Louis 11 years ago
parent
commit
42fdcf97d8
  1. 14
      songbook_core/errors.py
  2. 15
      songbook_core/templates.py

14
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."""

15
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]

Loading…
Cancel
Save