summaryrefslogtreecommitdiff
path: root/lib/spack/external/jinja2/runtime.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/spack/external/jinja2/runtime.py')
-rw-r--r--lib/spack/external/jinja2/runtime.py54
1 files changed, 40 insertions, 14 deletions
diff --git a/lib/spack/external/jinja2/runtime.py b/lib/spack/external/jinja2/runtime.py
index 00d5f03ca6..f9d7a6806c 100644
--- a/lib/spack/external/jinja2/runtime.py
+++ b/lib/spack/external/jinja2/runtime.py
@@ -15,7 +15,7 @@ from types import MethodType
from jinja2.nodes import EvalContext, _context_function_types
from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \
- internalcode, object_type_repr, evalcontextfunction
+ internalcode, object_type_repr, evalcontextfunction, Namespace
from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \
TemplateNotFound
from jinja2._compat import imap, text_type, iteritems, \
@@ -27,7 +27,7 @@ from jinja2._compat import imap, text_type, iteritems, \
__all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',
'TemplateRuntimeError', 'missing', 'concat', 'escape',
'markup_join', 'unicode_join', 'to_string', 'identity',
- 'TemplateNotFound']
+ 'TemplateNotFound', 'Namespace']
#: the name of the function that is used to convert something into
#: a string. We can just use the text type here.
@@ -36,6 +36,7 @@ to_string = text_type
#: the identity function. Useful for certain things in the environment
identity = lambda x: x
+_first_iteration = object()
_last_iteration = object()
@@ -241,13 +242,14 @@ class Context(with_metaclass(ContextMeta)):
__traceback_hide__ = True # noqa
# Allow callable classes to take a context
- fn = __obj.__call__
- for fn_type in ('contextfunction',
- 'evalcontextfunction',
- 'environmentfunction'):
- if hasattr(fn, fn_type):
- __obj = fn
- break
+ if hasattr(__obj, '__call__'):
+ fn = __obj.__call__
+ for fn_type in ('contextfunction',
+ 'evalcontextfunction',
+ 'environmentfunction'):
+ if hasattr(fn, fn_type):
+ __obj = fn
+ break
if isinstance(__obj, _context_function_types):
if getattr(__obj, 'contextfunction', 0):
@@ -349,13 +351,17 @@ class BlockReference(object):
class LoopContextBase(object):
"""A loop context for dynamic iteration."""
+ _before = _first_iteration
+ _current = _first_iteration
_after = _last_iteration
_length = None
- def __init__(self, recurse=None, depth0=0):
+ def __init__(self, undefined, recurse=None, depth0=0):
+ self._undefined = undefined
self._recurse = recurse
self.index0 = -1
self.depth0 = depth0
+ self._last_checked_value = missing
def cycle(self, *args):
"""Cycles among the arguments with the current loop index."""
@@ -363,6 +369,13 @@ class LoopContextBase(object):
raise TypeError('no items for cycling given')
return args[self.index0 % len(args)]
+ def changed(self, *value):
+ """Checks whether the value has changed since the last call."""
+ if self._last_checked_value != value:
+ self._last_checked_value = value
+ return True
+ return False
+
first = property(lambda x: x.index0 == 0)
last = property(lambda x: x._after is _last_iteration)
index = property(lambda x: x.index0 + 1)
@@ -370,6 +383,18 @@ class LoopContextBase(object):
revindex0 = property(lambda x: x.length - x.index)
depth = property(lambda x: x.depth0 + 1)
+ @property
+ def previtem(self):
+ if self._before is _first_iteration:
+ return self._undefined('there is no previous item')
+ return self._before
+
+ @property
+ def nextitem(self):
+ if self._after is _last_iteration:
+ return self._undefined('there is no next item')
+ return self._after
+
def __len__(self):
return self.length
@@ -395,8 +420,8 @@ class LoopContextBase(object):
class LoopContext(LoopContextBase):
- def __init__(self, iterable, recurse=None, depth0=0):
- LoopContextBase.__init__(self, recurse, depth0)
+ def __init__(self, iterable, undefined, recurse=None, depth0=0):
+ LoopContextBase.__init__(self, undefined, recurse, depth0)
self._iterator = iter(iterable)
# try to get the length of the iterable early. This must be done
@@ -448,9 +473,10 @@ class LoopContextIterator(object):
ctx.index0 += 1
if ctx._after is _last_iteration:
raise StopIteration()
- next_elem = ctx._after
+ ctx._before = ctx._current
+ ctx._current = ctx._after
ctx._after = ctx._safe_next()
- return next_elem, ctx
+ return ctx._current, ctx
class Macro(object):