runtime.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.runtime
  4. ~~~~~~~~~~~~~~
  5. Runtime helpers.
  6. :copyright: (c) 2010 by the Jinja Team.
  7. :license: BSD.
  8. """
  9. from itertools import chain
  10. from jinja2.nodes import EvalContext, _context_function_types
  11. from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \
  12. internalcode, object_type_repr
  13. from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \
  14. TemplateNotFound
  15. from jinja2._compat import next, imap, text_type, iteritems, \
  16. implements_iterator, implements_to_string, string_types, PY2
  17. # these variables are exported to the template runtime
  18. __all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',
  19. 'TemplateRuntimeError', 'missing', 'concat', 'escape',
  20. 'markup_join', 'unicode_join', 'to_string', 'identity',
  21. 'TemplateNotFound']
  22. #: the name of the function that is used to convert something into
  23. #: a string. We can just use the text type here.
  24. to_string = text_type
  25. #: the identity function. Useful for certain things in the environment
  26. identity = lambda x: x
  27. _last_iteration = object()
  28. def markup_join(seq):
  29. """Concatenation that escapes if necessary and converts to unicode."""
  30. buf = []
  31. iterator = imap(soft_unicode, seq)
  32. for arg in iterator:
  33. buf.append(arg)
  34. if hasattr(arg, '__html__'):
  35. return Markup(u'').join(chain(buf, iterator))
  36. return concat(buf)
  37. def unicode_join(seq):
  38. """Simple args to unicode conversion and concatenation."""
  39. return concat(imap(text_type, seq))
  40. def new_context(environment, template_name, blocks, vars=None,
  41. shared=None, globals=None, locals=None):
  42. """Internal helper to for context creation."""
  43. if vars is None:
  44. vars = {}
  45. if shared:
  46. parent = vars
  47. else:
  48. parent = dict(globals or (), **vars)
  49. if locals:
  50. # if the parent is shared a copy should be created because
  51. # we don't want to modify the dict passed
  52. if shared:
  53. parent = dict(parent)
  54. for key, value in iteritems(locals):
  55. if key[:2] == 'l_' and value is not missing:
  56. parent[key[2:]] = value
  57. return Context(environment, parent, template_name, blocks)
  58. class TemplateReference(object):
  59. """The `self` in templates."""
  60. def __init__(self, context):
  61. self.__context = context
  62. def __getitem__(self, name):
  63. blocks = self.__context.blocks[name]
  64. return BlockReference(name, self.__context, blocks, 0)
  65. def __repr__(self):
  66. return '<%s %r>' % (
  67. self.__class__.__name__,
  68. self.__context.name
  69. )
  70. class Context(object):
  71. """The template context holds the variables of a template. It stores the
  72. values passed to the template and also the names the template exports.
  73. Creating instances is neither supported nor useful as it's created
  74. automatically at various stages of the template evaluation and should not
  75. be created by hand.
  76. The context is immutable. Modifications on :attr:`parent` **must not**
  77. happen and modifications on :attr:`vars` are allowed from generated
  78. template code only. Template filters and global functions marked as
  79. :func:`contextfunction`\s get the active context passed as first argument
  80. and are allowed to access the context read-only.
  81. The template context supports read only dict operations (`get`,
  82. `keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`,
  83. `__getitem__`, `__contains__`). Additionally there is a :meth:`resolve`
  84. method that doesn't fail with a `KeyError` but returns an
  85. :class:`Undefined` object for missing variables.
  86. """
  87. __slots__ = ('parent', 'vars', 'environment', 'eval_ctx', 'exported_vars',
  88. 'name', 'blocks', '__weakref__')
  89. def __init__(self, environment, parent, name, blocks):
  90. self.parent = parent
  91. self.vars = {}
  92. self.environment = environment
  93. self.eval_ctx = EvalContext(self.environment, name)
  94. self.exported_vars = set()
  95. self.name = name
  96. # create the initial mapping of blocks. Whenever template inheritance
  97. # takes place the runtime will update this mapping with the new blocks
  98. # from the template.
  99. self.blocks = dict((k, [v]) for k, v in iteritems(blocks))
  100. def super(self, name, current):
  101. """Render a parent block."""
  102. try:
  103. blocks = self.blocks[name]
  104. index = blocks.index(current) + 1
  105. blocks[index]
  106. except LookupError:
  107. return self.environment.undefined('there is no parent block '
  108. 'called %r.' % name,
  109. name='super')
  110. return BlockReference(name, self, blocks, index)
  111. def get(self, key, default=None):
  112. """Returns an item from the template context, if it doesn't exist
  113. `default` is returned.
  114. """
  115. try:
  116. return self[key]
  117. except KeyError:
  118. return default
  119. def resolve(self, key):
  120. """Looks up a variable like `__getitem__` or `get` but returns an
  121. :class:`Undefined` object with the name of the name looked up.
  122. """
  123. if key in self.vars:
  124. return self.vars[key]
  125. if key in self.parent:
  126. return self.parent[key]
  127. return self.environment.undefined(name=key)
  128. def get_exported(self):
  129. """Get a new dict with the exported variables."""
  130. return dict((k, self.vars[k]) for k in self.exported_vars)
  131. def get_all(self):
  132. """Return a copy of the complete context as dict including the
  133. exported variables.
  134. """
  135. return dict(self.parent, **self.vars)
  136. @internalcode
  137. def call(__self, __obj, *args, **kwargs):
  138. """Call the callable with the arguments and keyword arguments
  139. provided but inject the active context or environment as first
  140. argument if the callable is a :func:`contextfunction` or
  141. :func:`environmentfunction`.
  142. """
  143. if __debug__:
  144. __traceback_hide__ = True
  145. # Allow callable classes to take a context
  146. fn = __obj.__call__
  147. for fn_type in ('contextfunction',
  148. 'evalcontextfunction',
  149. 'environmentfunction'):
  150. if hasattr(fn, fn_type):
  151. __obj = fn
  152. break
  153. if isinstance(__obj, _context_function_types):
  154. if getattr(__obj, 'contextfunction', 0):
  155. args = (__self,) + args
  156. elif getattr(__obj, 'evalcontextfunction', 0):
  157. args = (__self.eval_ctx,) + args
  158. elif getattr(__obj, 'environmentfunction', 0):
  159. args = (__self.environment,) + args
  160. try:
  161. return __obj(*args, **kwargs)
  162. except StopIteration:
  163. return __self.environment.undefined('value was undefined because '
  164. 'a callable raised a '
  165. 'StopIteration exception')
  166. def derived(self, locals=None):
  167. """Internal helper function to create a derived context."""
  168. context = new_context(self.environment, self.name, {},
  169. self.parent, True, None, locals)
  170. context.vars.update(self.vars)
  171. context.eval_ctx = self.eval_ctx
  172. context.blocks.update((k, list(v)) for k, v in iteritems(self.blocks))
  173. return context
  174. def _all(meth):
  175. proxy = lambda self: getattr(self.get_all(), meth)()
  176. proxy.__doc__ = getattr(dict, meth).__doc__
  177. proxy.__name__ = meth
  178. return proxy
  179. keys = _all('keys')
  180. values = _all('values')
  181. items = _all('items')
  182. # not available on python 3
  183. if PY2:
  184. iterkeys = _all('iterkeys')
  185. itervalues = _all('itervalues')
  186. iteritems = _all('iteritems')
  187. del _all
  188. def __contains__(self, name):
  189. return name in self.vars or name in self.parent
  190. def __getitem__(self, key):
  191. """Lookup a variable or raise `KeyError` if the variable is
  192. undefined.
  193. """
  194. item = self.resolve(key)
  195. if isinstance(item, Undefined):
  196. raise KeyError(key)
  197. return item
  198. def __repr__(self):
  199. return '<%s %s of %r>' % (
  200. self.__class__.__name__,
  201. repr(self.get_all()),
  202. self.name
  203. )
  204. # register the context as mapping if possible
  205. try:
  206. from collections import Mapping
  207. Mapping.register(Context)
  208. except ImportError:
  209. pass
  210. class BlockReference(object):
  211. """One block on a template reference."""
  212. def __init__(self, name, context, stack, depth):
  213. self.name = name
  214. self._context = context
  215. self._stack = stack
  216. self._depth = depth
  217. @property
  218. def super(self):
  219. """Super the block."""
  220. if self._depth + 1 >= len(self._stack):
  221. return self._context.environment. \
  222. undefined('there is no parent block called %r.' %
  223. self.name, name='super')
  224. return BlockReference(self.name, self._context, self._stack,
  225. self._depth + 1)
  226. @internalcode
  227. def __call__(self):
  228. rv = concat(self._stack[self._depth](self._context))
  229. if self._context.eval_ctx.autoescape:
  230. rv = Markup(rv)
  231. return rv
  232. class LoopContext(object):
  233. """A loop context for dynamic iteration."""
  234. def __init__(self, iterable, recurse=None, depth0=0):
  235. self._iterator = iter(iterable)
  236. self._recurse = recurse
  237. self._after = self._safe_next()
  238. self.index0 = -1
  239. self.depth0 = depth0
  240. # try to get the length of the iterable early. This must be done
  241. # here because there are some broken iterators around where there
  242. # __len__ is the number of iterations left (i'm looking at your
  243. # listreverseiterator!).
  244. try:
  245. self._length = len(iterable)
  246. except (TypeError, AttributeError):
  247. self._length = None
  248. def cycle(self, *args):
  249. """Cycles among the arguments with the current loop index."""
  250. if not args:
  251. raise TypeError('no items for cycling given')
  252. return args[self.index0 % len(args)]
  253. first = property(lambda x: x.index0 == 0)
  254. last = property(lambda x: x._after is _last_iteration)
  255. index = property(lambda x: x.index0 + 1)
  256. revindex = property(lambda x: x.length - x.index0)
  257. revindex0 = property(lambda x: x.length - x.index)
  258. depth = property(lambda x: x.depth0 + 1)
  259. def __len__(self):
  260. return self.length
  261. def __iter__(self):
  262. return LoopContextIterator(self)
  263. def _safe_next(self):
  264. try:
  265. return next(self._iterator)
  266. except StopIteration:
  267. return _last_iteration
  268. @internalcode
  269. def loop(self, iterable):
  270. if self._recurse is None:
  271. raise TypeError('Tried to call non recursive loop. Maybe you '
  272. "forgot the 'recursive' modifier.")
  273. return self._recurse(iterable, self._recurse, self.depth0 + 1)
  274. # a nifty trick to enhance the error message if someone tried to call
  275. # the the loop without or with too many arguments.
  276. __call__ = loop
  277. del loop
  278. @property
  279. def length(self):
  280. if self._length is None:
  281. # if was not possible to get the length of the iterator when
  282. # the loop context was created (ie: iterating over a generator)
  283. # we have to convert the iterable into a sequence and use the
  284. # length of that.
  285. iterable = tuple(self._iterator)
  286. self._iterator = iter(iterable)
  287. self._length = len(iterable) + self.index0 + 1
  288. return self._length
  289. def __repr__(self):
  290. return '<%s %r/%r>' % (
  291. self.__class__.__name__,
  292. self.index,
  293. self.length
  294. )
  295. @implements_iterator
  296. class LoopContextIterator(object):
  297. """The iterator for a loop context."""
  298. __slots__ = ('context',)
  299. def __init__(self, context):
  300. self.context = context
  301. def __iter__(self):
  302. return self
  303. def __next__(self):
  304. ctx = self.context
  305. ctx.index0 += 1
  306. if ctx._after is _last_iteration:
  307. raise StopIteration()
  308. next_elem = ctx._after
  309. ctx._after = ctx._safe_next()
  310. return next_elem, ctx
  311. class Macro(object):
  312. """Wraps a macro function."""
  313. def __init__(self, environment, func, name, arguments, defaults,
  314. catch_kwargs, catch_varargs, caller):
  315. self._environment = environment
  316. self._func = func
  317. self._argument_count = len(arguments)
  318. self.name = name
  319. self.arguments = arguments
  320. self.defaults = defaults
  321. self.catch_kwargs = catch_kwargs
  322. self.catch_varargs = catch_varargs
  323. self.caller = caller
  324. @internalcode
  325. def __call__(self, *args, **kwargs):
  326. # try to consume the positional arguments
  327. arguments = list(args[:self._argument_count])
  328. off = len(arguments)
  329. # if the number of arguments consumed is not the number of
  330. # arguments expected we start filling in keyword arguments
  331. # and defaults.
  332. if off != self._argument_count:
  333. for idx, name in enumerate(self.arguments[len(arguments):]):
  334. try:
  335. value = kwargs.pop(name)
  336. except KeyError:
  337. try:
  338. value = self.defaults[idx - self._argument_count + off]
  339. except IndexError:
  340. value = self._environment.undefined(
  341. 'parameter %r was not provided' % name, name=name)
  342. arguments.append(value)
  343. # it's important that the order of these arguments does not change
  344. # if not also changed in the compiler's `function_scoping` method.
  345. # the order is caller, keyword arguments, positional arguments!
  346. if self.caller:
  347. caller = kwargs.pop('caller', None)
  348. if caller is None:
  349. caller = self._environment.undefined('No caller defined',
  350. name='caller')
  351. arguments.append(caller)
  352. if self.catch_kwargs:
  353. arguments.append(kwargs)
  354. elif kwargs:
  355. raise TypeError('macro %r takes no keyword argument %r' %
  356. (self.name, next(iter(kwargs))))
  357. if self.catch_varargs:
  358. arguments.append(args[self._argument_count:])
  359. elif len(args) > self._argument_count:
  360. raise TypeError('macro %r takes not more than %d argument(s)' %
  361. (self.name, len(self.arguments)))
  362. return self._func(*arguments)
  363. def __repr__(self):
  364. return '<%s %s>' % (
  365. self.__class__.__name__,
  366. self.name is None and 'anonymous' or repr(self.name)
  367. )
  368. @implements_to_string
  369. class Undefined(object):
  370. """The default undefined type. This undefined type can be printed and
  371. iterated over, but every other access will raise an :exc:`UndefinedError`:
  372. >>> foo = Undefined(name='foo')
  373. >>> str(foo)
  374. ''
  375. >>> not foo
  376. True
  377. >>> foo + 42
  378. Traceback (most recent call last):
  379. ...
  380. UndefinedError: 'foo' is undefined
  381. """
  382. __slots__ = ('_undefined_hint', '_undefined_obj', '_undefined_name',
  383. '_undefined_exception')
  384. def __init__(self, hint=None, obj=missing, name=None, exc=UndefinedError):
  385. self._undefined_hint = hint
  386. self._undefined_obj = obj
  387. self._undefined_name = name
  388. self._undefined_exception = exc
  389. @internalcode
  390. def _fail_with_undefined_error(self, *args, **kwargs):
  391. """Regular callback function for undefined objects that raises an
  392. `UndefinedError` on call.
  393. """
  394. if self._undefined_hint is None:
  395. if self._undefined_obj is missing:
  396. hint = '%r is undefined' % self._undefined_name
  397. elif not isinstance(self._undefined_name, string_types):
  398. hint = '%s has no element %r' % (
  399. object_type_repr(self._undefined_obj),
  400. self._undefined_name
  401. )
  402. else:
  403. hint = '%r has no attribute %r' % (
  404. object_type_repr(self._undefined_obj),
  405. self._undefined_name
  406. )
  407. else:
  408. hint = self._undefined_hint
  409. raise self._undefined_exception(hint)
  410. @internalcode
  411. def __getattr__(self, name):
  412. if name[:2] == '__':
  413. raise AttributeError(name)
  414. return self._fail_with_undefined_error()
  415. __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
  416. __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \
  417. __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
  418. __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \
  419. __float__ = __complex__ = __pow__ = __rpow__ = \
  420. _fail_with_undefined_error
  421. def __eq__(self, other):
  422. return type(self) is type(other)
  423. def __ne__(self, other):
  424. return not self.__eq__(other)
  425. def __hash__(self):
  426. return id(type(self))
  427. def __str__(self):
  428. return u''
  429. def __len__(self):
  430. return 0
  431. def __iter__(self):
  432. if 0:
  433. yield None
  434. def __nonzero__(self):
  435. return False
  436. def __repr__(self):
  437. return 'Undefined'
  438. @implements_to_string
  439. class DebugUndefined(Undefined):
  440. """An undefined that returns the debug info when printed.
  441. >>> foo = DebugUndefined(name='foo')
  442. >>> str(foo)
  443. '{{ foo }}'
  444. >>> not foo
  445. True
  446. >>> foo + 42
  447. Traceback (most recent call last):
  448. ...
  449. UndefinedError: 'foo' is undefined
  450. """
  451. __slots__ = ()
  452. def __str__(self):
  453. if self._undefined_hint is None:
  454. if self._undefined_obj is missing:
  455. return u'{{ %s }}' % self._undefined_name
  456. return '{{ no such element: %s[%r] }}' % (
  457. object_type_repr(self._undefined_obj),
  458. self._undefined_name
  459. )
  460. return u'{{ undefined value printed: %s }}' % self._undefined_hint
  461. @implements_to_string
  462. class StrictUndefined(Undefined):
  463. """An undefined that barks on print and iteration as well as boolean
  464. tests and all kinds of comparisons. In other words: you can do nothing
  465. with it except checking if it's defined using the `defined` test.
  466. >>> foo = StrictUndefined(name='foo')
  467. >>> str(foo)
  468. Traceback (most recent call last):
  469. ...
  470. UndefinedError: 'foo' is undefined
  471. >>> not foo
  472. Traceback (most recent call last):
  473. ...
  474. UndefinedError: 'foo' is undefined
  475. >>> foo + 42
  476. Traceback (most recent call last):
  477. ...
  478. UndefinedError: 'foo' is undefined
  479. """
  480. __slots__ = ()
  481. __iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \
  482. __ne__ = __bool__ = __hash__ = \
  483. Undefined._fail_with_undefined_error
  484. # remove remaining slots attributes, after the metaclass did the magic they
  485. # are unneeded and irritating as they contain wrong data for the subclasses.
  486. del Undefined.__slots__, DebugUndefined.__slots__, StrictUndefined.__slots__