Skip to content

Commit

Permalink
Improve error message if using RenderLaTeX without jinja2 installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynikitenko committed May 8, 2021
1 parent 6892c26 commit b42e360
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
1 change: 1 addition & 0 deletions lena/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
intersection, iterate_update, make_context,
str_to_dict, str_to_list, update_nested, update_recursively,
)
# will import, but can't be used if jinja2 is missing
from .update_context import UpdateContext


Expand Down
6 changes: 3 additions & 3 deletions lena/context/update_context.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import print_function

# requires jinja2
import copy
import re

import jinja2
import lena.core
import lena.context
import lena.flow
Expand Down Expand Up @@ -88,6 +86,7 @@ def __init__(self, subcontext, update, value=False, default=_sentinel,
and the last two symbols of *update*,
otherwise :exc:`.LenaValueError` is raised.
"""
import jinja2
# The "Context" in the class name means any general context
# (not only :class:`.Context`).

Expand Down Expand Up @@ -173,6 +172,7 @@ def __call__(self, value):
*raise_on_missing* is ``True`` and
the update argument is missing in *value*'s context.
"""
import jinja2
data, context = lena.flow.get_data_context(value)
if isinstance(self._update, (str, jinja2.Template)):
if self._context_value:
Expand Down
25 changes: 16 additions & 9 deletions lena/output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
from .write import Writer, Write
from .to_csv import ToCSV, hist1d_to_csv, hist2d_to_csv

imported = []

def raise_on_usage(clsname, modname):
def stub(*args, **kwargs):
raise ImportError("{} can't be used because {} is not found".
format(clsname, modname))
return stub


# we do it here, because jinja2 is used at top level in render_latex.py
# (in classes' parents)
try:
# won't work if jinja2 is not installed
from lena.output.render_latex import RenderLaTeX # , Template, Environment
# will raise if jinja2 is not installed
from lena.output.render_latex import RenderLaTeX #, Template, Environment
except ImportError:
pass
else:
imported.extend(['RenderLaTeX'])
# imported.extend(['RenderLaTeX', 'Template', 'Environment'])
RenderLaTeX = raise_on_usage("RenderLaTeX", "jinja2")


__all__ = [
Expand All @@ -22,5 +28,6 @@
'RenderLaTeX',
'Write',
'Writer',
'ToCSV', 'hist1d_to_csv', 'hist2d_to_csv'
] + imported
'ToCSV', 'hist1d_to_csv', 'hist2d_to_csv',
'RenderLaTeX'
]
24 changes: 24 additions & 0 deletions tests/output/test_missing_jinja2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys

import pytest


# this function works when used solely,
# but not with other modules tests.
@pytest.mark.skip(reason="can only test this separately from other modules")
def test_missing_jinja2():
# we check that a proper and clear exception is raised
# if jinja2 is missing when using RenderLaTeX
# from https://stackoverflow.com/a/1350574/952234
import jinja2
# doesn't help.
# jinja2_tmp = jinja2
sys.modules["jinja2"] = None
from lena.output import RenderLaTeX
with pytest.raises(ImportError) as err:
r = RenderLaTeX("hello")
assert str(err.value) == "RenderLaTeX can't be used because jinja2 is not found"
# seems tests are run non-atomically, so need to return this
# del sys.modules["jinja2"]
# doesn't work:
# sys.modules["jinja2"] = jinja2_tmp
6 changes: 2 additions & 4 deletions tests/output/test_render_latex.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import print_function

import copy
from copy import deepcopy
import inspect
import jinja2
import os
import pytest
import sys
import copy
from copy import deepcopy

import lena
from lena.output.render_latex import (
Expand Down

0 comments on commit b42e360

Please sign in to comment.