codegen.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. # mako/codegen.py
  2. # Copyright (C) 2006-2016 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """provides functionality for rendering a parsetree constructing into module
  7. source code."""
  8. import time
  9. import re
  10. from mako.pygen import PythonPrinter
  11. from mako import util, ast, parsetree, filters, exceptions
  12. from mako import compat
  13. MAGIC_NUMBER = 10
  14. # names which are hardwired into the
  15. # template and are not accessed via the
  16. # context itself
  17. TOPLEVEL_DECLARED = set(["UNDEFINED", "STOP_RENDERING"])
  18. RESERVED_NAMES = set(['context', 'loop']).union(TOPLEVEL_DECLARED)
  19. def compile(node,
  20. uri,
  21. filename=None,
  22. default_filters=None,
  23. buffer_filters=None,
  24. imports=None,
  25. future_imports=None,
  26. source_encoding=None,
  27. generate_magic_comment=True,
  28. disable_unicode=False,
  29. strict_undefined=False,
  30. enable_loop=True,
  31. reserved_names=frozenset()):
  32. """Generate module source code given a parsetree node,
  33. uri, and optional source filename"""
  34. # if on Py2K, push the "source_encoding" string to be
  35. # a bytestring itself, as we will be embedding it into
  36. # the generated source and we don't want to coerce the
  37. # result into a unicode object, in "disable_unicode" mode
  38. if not compat.py3k and isinstance(source_encoding, compat.text_type):
  39. source_encoding = source_encoding.encode(source_encoding)
  40. buf = util.FastEncodingBuffer()
  41. printer = PythonPrinter(buf)
  42. _GenerateRenderMethod(printer,
  43. _CompileContext(uri,
  44. filename,
  45. default_filters,
  46. buffer_filters,
  47. imports,
  48. future_imports,
  49. source_encoding,
  50. generate_magic_comment,
  51. disable_unicode,
  52. strict_undefined,
  53. enable_loop,
  54. reserved_names),
  55. node)
  56. return buf.getvalue()
  57. class _CompileContext(object):
  58. def __init__(self,
  59. uri,
  60. filename,
  61. default_filters,
  62. buffer_filters,
  63. imports,
  64. future_imports,
  65. source_encoding,
  66. generate_magic_comment,
  67. disable_unicode,
  68. strict_undefined,
  69. enable_loop,
  70. reserved_names):
  71. self.uri = uri
  72. self.filename = filename
  73. self.default_filters = default_filters
  74. self.buffer_filters = buffer_filters
  75. self.imports = imports
  76. self.future_imports = future_imports
  77. self.source_encoding = source_encoding
  78. self.generate_magic_comment = generate_magic_comment
  79. self.disable_unicode = disable_unicode
  80. self.strict_undefined = strict_undefined
  81. self.enable_loop = enable_loop
  82. self.reserved_names = reserved_names
  83. class _GenerateRenderMethod(object):
  84. """A template visitor object which generates the
  85. full module source for a template.
  86. """
  87. def __init__(self, printer, compiler, node):
  88. self.printer = printer
  89. self.compiler = compiler
  90. self.node = node
  91. self.identifier_stack = [None]
  92. self.in_def = isinstance(node, (parsetree.DefTag, parsetree.BlockTag))
  93. if self.in_def:
  94. name = "render_%s" % node.funcname
  95. args = node.get_argument_expressions()
  96. filtered = len(node.filter_args.args) > 0
  97. buffered = eval(node.attributes.get('buffered', 'False'))
  98. cached = eval(node.attributes.get('cached', 'False'))
  99. defs = None
  100. pagetag = None
  101. if node.is_block and not node.is_anonymous:
  102. args += ['**pageargs']
  103. else:
  104. defs = self.write_toplevel()
  105. pagetag = self.compiler.pagetag
  106. name = "render_body"
  107. if pagetag is not None:
  108. args = pagetag.body_decl.get_argument_expressions()
  109. if not pagetag.body_decl.kwargs:
  110. args += ['**pageargs']
  111. cached = eval(pagetag.attributes.get('cached', 'False'))
  112. self.compiler.enable_loop = self.compiler.enable_loop or eval(
  113. pagetag.attributes.get(
  114. 'enable_loop', 'False')
  115. )
  116. else:
  117. args = ['**pageargs']
  118. cached = False
  119. buffered = filtered = False
  120. if args is None:
  121. args = ['context']
  122. else:
  123. args = [a for a in ['context'] + args]
  124. self.write_render_callable(
  125. pagetag or node,
  126. name, args,
  127. buffered, filtered, cached)
  128. if defs is not None:
  129. for node in defs:
  130. _GenerateRenderMethod(printer, compiler, node)
  131. if not self.in_def:
  132. self.write_metadata_struct()
  133. def write_metadata_struct(self):
  134. self.printer.source_map[self.printer.lineno] = \
  135. max(self.printer.source_map)
  136. struct = {
  137. "filename": self.compiler.filename,
  138. "uri": self.compiler.uri,
  139. "source_encoding": self.compiler.source_encoding,
  140. "line_map": self.printer.source_map,
  141. }
  142. self.printer.writelines(
  143. '"""',
  144. '__M_BEGIN_METADATA',
  145. compat.json.dumps(struct),
  146. '__M_END_METADATA\n'
  147. '"""'
  148. )
  149. @property
  150. def identifiers(self):
  151. return self.identifier_stack[-1]
  152. def write_toplevel(self):
  153. """Traverse a template structure for module-level directives and
  154. generate the start of module-level code.
  155. """
  156. inherit = []
  157. namespaces = {}
  158. module_code = []
  159. self.compiler.pagetag = None
  160. class FindTopLevel(object):
  161. def visitInheritTag(s, node):
  162. inherit.append(node)
  163. def visitNamespaceTag(s, node):
  164. namespaces[node.name] = node
  165. def visitPageTag(s, node):
  166. self.compiler.pagetag = node
  167. def visitCode(s, node):
  168. if node.ismodule:
  169. module_code.append(node)
  170. f = FindTopLevel()
  171. for n in self.node.nodes:
  172. n.accept_visitor(f)
  173. self.compiler.namespaces = namespaces
  174. module_ident = set()
  175. for n in module_code:
  176. module_ident = module_ident.union(n.declared_identifiers())
  177. module_identifiers = _Identifiers(self.compiler)
  178. module_identifiers.declared = module_ident
  179. # module-level names, python code
  180. if self.compiler.generate_magic_comment and \
  181. self.compiler.source_encoding:
  182. self.printer.writeline("# -*- coding:%s -*-" %
  183. self.compiler.source_encoding)
  184. if self.compiler.future_imports:
  185. self.printer.writeline("from __future__ import %s" %
  186. (", ".join(self.compiler.future_imports),))
  187. self.printer.writeline("from mako import runtime, filters, cache")
  188. self.printer.writeline("UNDEFINED = runtime.UNDEFINED")
  189. self.printer.writeline("STOP_RENDERING = runtime.STOP_RENDERING")
  190. self.printer.writeline("__M_dict_builtin = dict")
  191. self.printer.writeline("__M_locals_builtin = locals")
  192. self.printer.writeline("_magic_number = %r" % MAGIC_NUMBER)
  193. self.printer.writeline("_modified_time = %r" % time.time())
  194. self.printer.writeline("_enable_loop = %r" % self.compiler.enable_loop)
  195. self.printer.writeline(
  196. "_template_filename = %r" % self.compiler.filename)
  197. self.printer.writeline("_template_uri = %r" % self.compiler.uri)
  198. self.printer.writeline(
  199. "_source_encoding = %r" % self.compiler.source_encoding)
  200. if self.compiler.imports:
  201. buf = ''
  202. for imp in self.compiler.imports:
  203. buf += imp + "\n"
  204. self.printer.writeline(imp)
  205. impcode = ast.PythonCode(
  206. buf,
  207. source='', lineno=0,
  208. pos=0,
  209. filename='template defined imports')
  210. else:
  211. impcode = None
  212. main_identifiers = module_identifiers.branch(self.node)
  213. module_identifiers.topleveldefs = \
  214. module_identifiers.topleveldefs.\
  215. union(main_identifiers.topleveldefs)
  216. module_identifiers.declared.update(TOPLEVEL_DECLARED)
  217. if impcode:
  218. module_identifiers.declared.update(impcode.declared_identifiers)
  219. self.compiler.identifiers = module_identifiers
  220. self.printer.writeline("_exports = %r" %
  221. [n.name for n in
  222. main_identifiers.topleveldefs.values()]
  223. )
  224. self.printer.write_blanks(2)
  225. if len(module_code):
  226. self.write_module_code(module_code)
  227. if len(inherit):
  228. self.write_namespaces(namespaces)
  229. self.write_inherit(inherit[-1])
  230. elif len(namespaces):
  231. self.write_namespaces(namespaces)
  232. return list(main_identifiers.topleveldefs.values())
  233. def write_render_callable(self, node, name, args, buffered, filtered,
  234. cached):
  235. """write a top-level render callable.
  236. this could be the main render() method or that of a top-level def."""
  237. if self.in_def:
  238. decorator = node.decorator
  239. if decorator:
  240. self.printer.writeline(
  241. "@runtime._decorate_toplevel(%s)" % decorator)
  242. self.printer.start_source(node.lineno)
  243. self.printer.writelines(
  244. "def %s(%s):" % (name, ','.join(args)),
  245. # push new frame, assign current frame to __M_caller
  246. "__M_caller = context.caller_stack._push_frame()",
  247. "try:"
  248. )
  249. if buffered or filtered or cached:
  250. self.printer.writeline("context._push_buffer()")
  251. self.identifier_stack.append(
  252. self.compiler.identifiers.branch(self.node))
  253. if (not self.in_def or self.node.is_block) and '**pageargs' in args:
  254. self.identifier_stack[-1].argument_declared.add('pageargs')
  255. if not self.in_def and (
  256. len(self.identifiers.locally_assigned) > 0 or
  257. len(self.identifiers.argument_declared) > 0
  258. ):
  259. self.printer.writeline("__M_locals = __M_dict_builtin(%s)" %
  260. ','.join([
  261. "%s=%s" % (x, x) for x in
  262. self.identifiers.argument_declared
  263. ]))
  264. self.write_variable_declares(self.identifiers, toplevel=True)
  265. for n in self.node.nodes:
  266. n.accept_visitor(self)
  267. self.write_def_finish(self.node, buffered, filtered, cached)
  268. self.printer.writeline(None)
  269. self.printer.write_blanks(2)
  270. if cached:
  271. self.write_cache_decorator(
  272. node, name,
  273. args, buffered,
  274. self.identifiers, toplevel=True)
  275. def write_module_code(self, module_code):
  276. """write module-level template code, i.e. that which
  277. is enclosed in <%! %> tags in the template."""
  278. for n in module_code:
  279. self.printer.start_source(n.lineno)
  280. self.printer.write_indented_block(n.text)
  281. def write_inherit(self, node):
  282. """write the module-level inheritance-determination callable."""
  283. self.printer.writelines(
  284. "def _mako_inherit(template, context):",
  285. "_mako_generate_namespaces(context)",
  286. "return runtime._inherit_from(context, %s, _template_uri)" %
  287. (node.parsed_attributes['file']),
  288. None
  289. )
  290. def write_namespaces(self, namespaces):
  291. """write the module-level namespace-generating callable."""
  292. self.printer.writelines(
  293. "def _mako_get_namespace(context, name):",
  294. "try:",
  295. "return context.namespaces[(__name__, name)]",
  296. "except KeyError:",
  297. "_mako_generate_namespaces(context)",
  298. "return context.namespaces[(__name__, name)]",
  299. None, None
  300. )
  301. self.printer.writeline("def _mako_generate_namespaces(context):")
  302. for node in namespaces.values():
  303. if 'import' in node.attributes:
  304. self.compiler.has_ns_imports = True
  305. self.printer.start_source(node.lineno)
  306. if len(node.nodes):
  307. self.printer.writeline("def make_namespace():")
  308. export = []
  309. identifiers = self.compiler.identifiers.branch(node)
  310. self.in_def = True
  311. class NSDefVisitor(object):
  312. def visitDefTag(s, node):
  313. s.visitDefOrBase(node)
  314. def visitBlockTag(s, node):
  315. s.visitDefOrBase(node)
  316. def visitDefOrBase(s, node):
  317. if node.is_anonymous:
  318. raise exceptions.CompileException(
  319. "Can't put anonymous blocks inside "
  320. "<%namespace>",
  321. **node.exception_kwargs
  322. )
  323. self.write_inline_def(node, identifiers, nested=False)
  324. export.append(node.funcname)
  325. vis = NSDefVisitor()
  326. for n in node.nodes:
  327. n.accept_visitor(vis)
  328. self.printer.writeline("return [%s]" % (','.join(export)))
  329. self.printer.writeline(None)
  330. self.in_def = False
  331. callable_name = "make_namespace()"
  332. else:
  333. callable_name = "None"
  334. if 'file' in node.parsed_attributes:
  335. self.printer.writeline(
  336. "ns = runtime.TemplateNamespace(%r,"
  337. " context._clean_inheritance_tokens(),"
  338. " templateuri=%s, callables=%s, "
  339. " calling_uri=_template_uri)" %
  340. (
  341. node.name,
  342. node.parsed_attributes.get('file', 'None'),
  343. callable_name,
  344. )
  345. )
  346. elif 'module' in node.parsed_attributes:
  347. self.printer.writeline(
  348. "ns = runtime.ModuleNamespace(%r,"
  349. " context._clean_inheritance_tokens(),"
  350. " callables=%s, calling_uri=_template_uri,"
  351. " module=%s)" %
  352. (
  353. node.name,
  354. callable_name,
  355. node.parsed_attributes.get(
  356. 'module', 'None')
  357. )
  358. )
  359. else:
  360. self.printer.writeline(
  361. "ns = runtime.Namespace(%r,"
  362. " context._clean_inheritance_tokens(),"
  363. " callables=%s, calling_uri=_template_uri)" %
  364. (
  365. node.name,
  366. callable_name,
  367. )
  368. )
  369. if eval(node.attributes.get('inheritable', "False")):
  370. self.printer.writeline("context['self'].%s = ns" % (node.name))
  371. self.printer.writeline(
  372. "context.namespaces[(__name__, %s)] = ns" % repr(node.name))
  373. self.printer.write_blanks(1)
  374. if not len(namespaces):
  375. self.printer.writeline("pass")
  376. self.printer.writeline(None)
  377. def write_variable_declares(self, identifiers, toplevel=False, limit=None):
  378. """write variable declarations at the top of a function.
  379. the variable declarations are in the form of callable
  380. definitions for defs and/or name lookup within the
  381. function's context argument. the names declared are based
  382. on the names that are referenced in the function body,
  383. which don't otherwise have any explicit assignment
  384. operation. names that are assigned within the body are
  385. assumed to be locally-scoped variables and are not
  386. separately declared.
  387. for def callable definitions, if the def is a top-level
  388. callable then a 'stub' callable is generated which wraps
  389. the current Context into a closure. if the def is not
  390. top-level, it is fully rendered as a local closure.
  391. """
  392. # collection of all defs available to us in this scope
  393. comp_idents = dict([(c.funcname, c) for c in identifiers.defs])
  394. to_write = set()
  395. # write "context.get()" for all variables we are going to
  396. # need that arent in the namespace yet
  397. to_write = to_write.union(identifiers.undeclared)
  398. # write closure functions for closures that we define
  399. # right here
  400. to_write = to_write.union(
  401. [c.funcname for c in identifiers.closuredefs.values()])
  402. # remove identifiers that are declared in the argument
  403. # signature of the callable
  404. to_write = to_write.difference(identifiers.argument_declared)
  405. # remove identifiers that we are going to assign to.
  406. # in this way we mimic Python's behavior,
  407. # i.e. assignment to a variable within a block
  408. # means that variable is now a "locally declared" var,
  409. # which cannot be referenced beforehand.
  410. to_write = to_write.difference(identifiers.locally_declared)
  411. if self.compiler.enable_loop:
  412. has_loop = "loop" in to_write
  413. to_write.discard("loop")
  414. else:
  415. has_loop = False
  416. # if a limiting set was sent, constraint to those items in that list
  417. # (this is used for the caching decorator)
  418. if limit is not None:
  419. to_write = to_write.intersection(limit)
  420. if toplevel and getattr(self.compiler, 'has_ns_imports', False):
  421. self.printer.writeline("_import_ns = {}")
  422. self.compiler.has_imports = True
  423. for ident, ns in self.compiler.namespaces.items():
  424. if 'import' in ns.attributes:
  425. self.printer.writeline(
  426. "_mako_get_namespace(context, %r)."
  427. "_populate(_import_ns, %r)" %
  428. (
  429. ident,
  430. re.split(r'\s*,\s*', ns.attributes['import'])
  431. ))
  432. if has_loop:
  433. self.printer.writeline(
  434. 'loop = __M_loop = runtime.LoopStack()'
  435. )
  436. for ident in to_write:
  437. if ident in comp_idents:
  438. comp = comp_idents[ident]
  439. if comp.is_block:
  440. if not comp.is_anonymous:
  441. self.write_def_decl(comp, identifiers)
  442. else:
  443. self.write_inline_def(comp, identifiers, nested=True)
  444. else:
  445. if comp.is_root():
  446. self.write_def_decl(comp, identifiers)
  447. else:
  448. self.write_inline_def(comp, identifiers, nested=True)
  449. elif ident in self.compiler.namespaces:
  450. self.printer.writeline(
  451. "%s = _mako_get_namespace(context, %r)" %
  452. (ident, ident)
  453. )
  454. else:
  455. if getattr(self.compiler, 'has_ns_imports', False):
  456. if self.compiler.strict_undefined:
  457. self.printer.writelines(
  458. "%s = _import_ns.get(%r, UNDEFINED)" %
  459. (ident, ident),
  460. "if %s is UNDEFINED:" % ident,
  461. "try:",
  462. "%s = context[%r]" % (ident, ident),
  463. "except KeyError:",
  464. "raise NameError(\"'%s' is not defined\")" %
  465. ident,
  466. None, None
  467. )
  468. else:
  469. self.printer.writeline(
  470. "%s = _import_ns.get"
  471. "(%r, context.get(%r, UNDEFINED))" %
  472. (ident, ident, ident))
  473. else:
  474. if self.compiler.strict_undefined:
  475. self.printer.writelines(
  476. "try:",
  477. "%s = context[%r]" % (ident, ident),
  478. "except KeyError:",
  479. "raise NameError(\"'%s' is not defined\")" %
  480. ident,
  481. None
  482. )
  483. else:
  484. self.printer.writeline(
  485. "%s = context.get(%r, UNDEFINED)" % (ident, ident)
  486. )
  487. self.printer.writeline("__M_writer = context.writer()")
  488. def write_def_decl(self, node, identifiers):
  489. """write a locally-available callable referencing a top-level def"""
  490. funcname = node.funcname
  491. namedecls = node.get_argument_expressions()
  492. nameargs = node.get_argument_expressions(as_call=True)
  493. if not self.in_def and (
  494. len(self.identifiers.locally_assigned) > 0 or
  495. len(self.identifiers.argument_declared) > 0):
  496. nameargs.insert(0, 'context._locals(__M_locals)')
  497. else:
  498. nameargs.insert(0, 'context')
  499. self.printer.writeline("def %s(%s):" % (funcname, ",".join(namedecls)))
  500. self.printer.writeline(
  501. "return render_%s(%s)" % (funcname, ",".join(nameargs)))
  502. self.printer.writeline(None)
  503. def write_inline_def(self, node, identifiers, nested):
  504. """write a locally-available def callable inside an enclosing def."""
  505. namedecls = node.get_argument_expressions()
  506. decorator = node.decorator
  507. if decorator:
  508. self.printer.writeline(
  509. "@runtime._decorate_inline(context, %s)" % decorator)
  510. self.printer.writeline(
  511. "def %s(%s):" % (node.funcname, ",".join(namedecls)))
  512. filtered = len(node.filter_args.args) > 0
  513. buffered = eval(node.attributes.get('buffered', 'False'))
  514. cached = eval(node.attributes.get('cached', 'False'))
  515. self.printer.writelines(
  516. # push new frame, assign current frame to __M_caller
  517. "__M_caller = context.caller_stack._push_frame()",
  518. "try:"
  519. )
  520. if buffered or filtered or cached:
  521. self.printer.writelines(
  522. "context._push_buffer()",
  523. )
  524. identifiers = identifiers.branch(node, nested=nested)
  525. self.write_variable_declares(identifiers)
  526. self.identifier_stack.append(identifiers)
  527. for n in node.nodes:
  528. n.accept_visitor(self)
  529. self.identifier_stack.pop()
  530. self.write_def_finish(node, buffered, filtered, cached)
  531. self.printer.writeline(None)
  532. if cached:
  533. self.write_cache_decorator(node, node.funcname,
  534. namedecls, False, identifiers,
  535. inline=True, toplevel=False)
  536. def write_def_finish(self, node, buffered, filtered, cached,
  537. callstack=True):
  538. """write the end section of a rendering function, either outermost or
  539. inline.
  540. this takes into account if the rendering function was filtered,
  541. buffered, etc. and closes the corresponding try: block if any, and
  542. writes code to retrieve captured content, apply filters, send proper
  543. return value."""
  544. if not buffered and not cached and not filtered:
  545. self.printer.writeline("return ''")
  546. if callstack:
  547. self.printer.writelines(
  548. "finally:",
  549. "context.caller_stack._pop_frame()",
  550. None
  551. )
  552. if buffered or filtered or cached:
  553. if buffered or cached:
  554. # in a caching scenario, don't try to get a writer
  555. # from the context after popping; assume the caching
  556. # implemenation might be using a context with no
  557. # extra buffers
  558. self.printer.writelines(
  559. "finally:",
  560. "__M_buf = context._pop_buffer()"
  561. )
  562. else:
  563. self.printer.writelines(
  564. "finally:",
  565. "__M_buf, __M_writer = context._pop_buffer_and_writer()"
  566. )
  567. if callstack:
  568. self.printer.writeline("context.caller_stack._pop_frame()")
  569. s = "__M_buf.getvalue()"
  570. if filtered:
  571. s = self.create_filter_callable(node.filter_args.args, s,
  572. False)
  573. self.printer.writeline(None)
  574. if buffered and not cached:
  575. s = self.create_filter_callable(self.compiler.buffer_filters,
  576. s, False)
  577. if buffered or cached:
  578. self.printer.writeline("return %s" % s)
  579. else:
  580. self.printer.writelines(
  581. "__M_writer(%s)" % s,
  582. "return ''"
  583. )
  584. def write_cache_decorator(self, node_or_pagetag, name,
  585. args, buffered, identifiers,
  586. inline=False, toplevel=False):
  587. """write a post-function decorator to replace a rendering
  588. callable with a cached version of itself."""
  589. self.printer.writeline("__M_%s = %s" % (name, name))
  590. cachekey = node_or_pagetag.parsed_attributes.get('cache_key',
  591. repr(name))
  592. cache_args = {}
  593. if self.compiler.pagetag is not None:
  594. cache_args.update(
  595. (
  596. pa[6:],
  597. self.compiler.pagetag.parsed_attributes[pa]
  598. )
  599. for pa in self.compiler.pagetag.parsed_attributes
  600. if pa.startswith('cache_') and pa != 'cache_key'
  601. )
  602. cache_args.update(
  603. (
  604. pa[6:],
  605. node_or_pagetag.parsed_attributes[pa]
  606. ) for pa in node_or_pagetag.parsed_attributes
  607. if pa.startswith('cache_') and pa != 'cache_key'
  608. )
  609. if 'timeout' in cache_args:
  610. cache_args['timeout'] = int(eval(cache_args['timeout']))
  611. self.printer.writeline("def %s(%s):" % (name, ','.join(args)))
  612. # form "arg1, arg2, arg3=arg3, arg4=arg4", etc.
  613. pass_args = [
  614. "%s=%s" % ((a.split('=')[0],) * 2) if '=' in a else a
  615. for a in args
  616. ]
  617. self.write_variable_declares(
  618. identifiers,
  619. toplevel=toplevel,
  620. limit=node_or_pagetag.undeclared_identifiers()
  621. )
  622. if buffered:
  623. s = "context.get('local')."\
  624. "cache._ctx_get_or_create("\
  625. "%s, lambda:__M_%s(%s), context, %s__M_defname=%r)" % (
  626. cachekey, name, ','.join(pass_args),
  627. ''.join(["%s=%s, " % (k, v)
  628. for k, v in cache_args.items()]),
  629. name
  630. )
  631. # apply buffer_filters
  632. s = self.create_filter_callable(self.compiler.buffer_filters, s,
  633. False)
  634. self.printer.writelines("return " + s, None)
  635. else:
  636. self.printer.writelines(
  637. "__M_writer(context.get('local')."
  638. "cache._ctx_get_or_create("
  639. "%s, lambda:__M_%s(%s), context, %s__M_defname=%r))" %
  640. (
  641. cachekey, name, ','.join(pass_args),
  642. ''.join(["%s=%s, " % (k, v)
  643. for k, v in cache_args.items()]),
  644. name,
  645. ),
  646. "return ''",
  647. None
  648. )
  649. def create_filter_callable(self, args, target, is_expression):
  650. """write a filter-applying expression based on the filters
  651. present in the given filter names, adjusting for the global
  652. 'default' filter aliases as needed."""
  653. def locate_encode(name):
  654. if re.match(r'decode\..+', name):
  655. return "filters." + name
  656. elif self.compiler.disable_unicode:
  657. return filters.NON_UNICODE_ESCAPES.get(name, name)
  658. else:
  659. return filters.DEFAULT_ESCAPES.get(name, name)
  660. if 'n' not in args:
  661. if is_expression:
  662. if self.compiler.pagetag:
  663. args = self.compiler.pagetag.filter_args.args + args
  664. if self.compiler.default_filters:
  665. args = self.compiler.default_filters + args
  666. for e in args:
  667. # if filter given as a function, get just the identifier portion
  668. if e == 'n':
  669. continue
  670. m = re.match(r'(.+?)(\(.*\))', e)
  671. if m:
  672. ident, fargs = m.group(1, 2)
  673. f = locate_encode(ident)
  674. e = f + fargs
  675. else:
  676. e = locate_encode(e)
  677. assert e is not None
  678. target = "%s(%s)" % (e, target)
  679. return target
  680. def visitExpression(self, node):
  681. self.printer.start_source(node.lineno)
  682. if len(node.escapes) or \
  683. (
  684. self.compiler.pagetag is not None and
  685. len(self.compiler.pagetag.filter_args.args)
  686. ) or \
  687. len(self.compiler.default_filters):
  688. s = self.create_filter_callable(node.escapes_code.args,
  689. "%s" % node.text, True)
  690. self.printer.writeline("__M_writer(%s)" % s)
  691. else:
  692. self.printer.writeline("__M_writer(%s)" % node.text)
  693. def visitControlLine(self, node):
  694. if node.isend:
  695. self.printer.writeline(None)
  696. if node.has_loop_context:
  697. self.printer.writeline('finally:')
  698. self.printer.writeline("loop = __M_loop._exit()")
  699. self.printer.writeline(None)
  700. else:
  701. self.printer.start_source(node.lineno)
  702. if self.compiler.enable_loop and node.keyword == 'for':
  703. text = mangle_mako_loop(node, self.printer)
  704. else:
  705. text = node.text
  706. self.printer.writeline(text)
  707. children = node.get_children()
  708. # this covers the three situations where we want to insert a pass:
  709. # 1) a ternary control line with no children,
  710. # 2) a primary control line with nothing but its own ternary
  711. # and end control lines, and
  712. # 3) any control line with no content other than comments
  713. if not children or (
  714. compat.all(isinstance(c, (parsetree.Comment,
  715. parsetree.ControlLine))
  716. for c in children) and
  717. compat.all((node.is_ternary(c.keyword) or c.isend)
  718. for c in children
  719. if isinstance(c, parsetree.ControlLine))):
  720. self.printer.writeline("pass")
  721. def visitText(self, node):
  722. self.printer.start_source(node.lineno)
  723. self.printer.writeline("__M_writer(%s)" % repr(node.content))
  724. def visitTextTag(self, node):
  725. filtered = len(node.filter_args.args) > 0
  726. if filtered:
  727. self.printer.writelines(
  728. "__M_writer = context._push_writer()",
  729. "try:",
  730. )
  731. for n in node.nodes:
  732. n.accept_visitor(self)
  733. if filtered:
  734. self.printer.writelines(
  735. "finally:",
  736. "__M_buf, __M_writer = context._pop_buffer_and_writer()",
  737. "__M_writer(%s)" %
  738. self.create_filter_callable(
  739. node.filter_args.args,
  740. "__M_buf.getvalue()",
  741. False),
  742. None
  743. )
  744. def visitCode(self, node):
  745. if not node.ismodule:
  746. self.printer.start_source(node.lineno)
  747. self.printer.write_indented_block(node.text)
  748. if not self.in_def and len(self.identifiers.locally_assigned) > 0:
  749. # if we are the "template" def, fudge locally
  750. # declared/modified variables into the "__M_locals" dictionary,
  751. # which is used for def calls within the same template,
  752. # to simulate "enclosing scope"
  753. self.printer.writeline(
  754. '__M_locals_builtin_stored = __M_locals_builtin()')
  755. self.printer.writeline(
  756. '__M_locals.update(__M_dict_builtin([(__M_key,'
  757. ' __M_locals_builtin_stored[__M_key]) for __M_key in'
  758. ' [%s] if __M_key in __M_locals_builtin_stored]))' %
  759. ','.join([repr(x) for x in node.declared_identifiers()]))
  760. def visitIncludeTag(self, node):
  761. self.printer.start_source(node.lineno)
  762. args = node.attributes.get('args')
  763. if args:
  764. self.printer.writeline(
  765. "runtime._include_file(context, %s, _template_uri, %s)" %
  766. (node.parsed_attributes['file'], args))
  767. else:
  768. self.printer.writeline(
  769. "runtime._include_file(context, %s, _template_uri)" %
  770. (node.parsed_attributes['file']))
  771. def visitNamespaceTag(self, node):
  772. pass
  773. def visitDefTag(self, node):
  774. pass
  775. def visitBlockTag(self, node):
  776. if node.is_anonymous:
  777. self.printer.writeline("%s()" % node.funcname)
  778. else:
  779. nameargs = node.get_argument_expressions(as_call=True)
  780. nameargs += ['**pageargs']
  781. self.printer.writeline(
  782. "if 'parent' not in context._data or "
  783. "not hasattr(context._data['parent'], '%s'):"
  784. % node.funcname)
  785. self.printer.writeline(
  786. "context['self'].%s(%s)" % (node.funcname, ",".join(nameargs)))
  787. self.printer.writeline("\n")
  788. def visitCallNamespaceTag(self, node):
  789. # TODO: we can put namespace-specific checks here, such
  790. # as ensure the given namespace will be imported,
  791. # pre-import the namespace, etc.
  792. self.visitCallTag(node)
  793. def visitCallTag(self, node):
  794. self.printer.writeline("def ccall(caller):")
  795. export = ['body']
  796. callable_identifiers = self.identifiers.branch(node, nested=True)
  797. body_identifiers = callable_identifiers.branch(node, nested=False)
  798. # we want the 'caller' passed to ccall to be used
  799. # for the body() function, but for other non-body()
  800. # <%def>s within <%call> we want the current caller
  801. # off the call stack (if any)
  802. body_identifiers.add_declared('caller')
  803. self.identifier_stack.append(body_identifiers)
  804. class DefVisitor(object):
  805. def visitDefTag(s, node):
  806. s.visitDefOrBase(node)
  807. def visitBlockTag(s, node):
  808. s.visitDefOrBase(node)
  809. def visitDefOrBase(s, node):
  810. self.write_inline_def(node, callable_identifiers, nested=False)
  811. if not node.is_anonymous:
  812. export.append(node.funcname)
  813. # remove defs that are within the <%call> from the
  814. # "closuredefs" defined in the body, so they dont render twice
  815. if node.funcname in body_identifiers.closuredefs:
  816. del body_identifiers.closuredefs[node.funcname]
  817. vis = DefVisitor()
  818. for n in node.nodes:
  819. n.accept_visitor(vis)
  820. self.identifier_stack.pop()
  821. bodyargs = node.body_decl.get_argument_expressions()
  822. self.printer.writeline("def body(%s):" % ','.join(bodyargs))
  823. # TODO: figure out best way to specify
  824. # buffering/nonbuffering (at call time would be better)
  825. buffered = False
  826. if buffered:
  827. self.printer.writelines(
  828. "context._push_buffer()",
  829. "try:"
  830. )
  831. self.write_variable_declares(body_identifiers)
  832. self.identifier_stack.append(body_identifiers)
  833. for n in node.nodes:
  834. n.accept_visitor(self)
  835. self.identifier_stack.pop()
  836. self.write_def_finish(node, buffered, False, False, callstack=False)
  837. self.printer.writelines(
  838. None,
  839. "return [%s]" % (','.join(export)),
  840. None
  841. )
  842. self.printer.writelines(
  843. # push on caller for nested call
  844. "context.caller_stack.nextcaller = "
  845. "runtime.Namespace('caller', context, "
  846. "callables=ccall(__M_caller))",
  847. "try:")
  848. self.printer.start_source(node.lineno)
  849. self.printer.writelines(
  850. "__M_writer(%s)" % self.create_filter_callable(
  851. [], node.expression, True),
  852. "finally:",
  853. "context.caller_stack.nextcaller = None",
  854. None
  855. )
  856. class _Identifiers(object):
  857. """tracks the status of identifier names as template code is rendered."""
  858. def __init__(self, compiler, node=None, parent=None, nested=False):
  859. if parent is not None:
  860. # if we are the branch created in write_namespaces(),
  861. # we don't share any context from the main body().
  862. if isinstance(node, parsetree.NamespaceTag):
  863. self.declared = set()
  864. self.topleveldefs = util.SetLikeDict()
  865. else:
  866. # things that have already been declared
  867. # in an enclosing namespace (i.e. names we can just use)
  868. self.declared = set(parent.declared).\
  869. union([c.name for c in parent.closuredefs.values()]).\
  870. union(parent.locally_declared).\
  871. union(parent.argument_declared)
  872. # if these identifiers correspond to a "nested"
  873. # scope, it means whatever the parent identifiers
  874. # had as undeclared will have been declared by that parent,
  875. # and therefore we have them in our scope.
  876. if nested:
  877. self.declared = self.declared.union(parent.undeclared)
  878. # top level defs that are available
  879. self.topleveldefs = util.SetLikeDict(**parent.topleveldefs)
  880. else:
  881. self.declared = set()
  882. self.topleveldefs = util.SetLikeDict()
  883. self.compiler = compiler
  884. # things within this level that are referenced before they
  885. # are declared (e.g. assigned to)
  886. self.undeclared = set()
  887. # things that are declared locally. some of these things
  888. # could be in the "undeclared" list as well if they are
  889. # referenced before declared
  890. self.locally_declared = set()
  891. # assignments made in explicit python blocks.
  892. # these will be propagated to
  893. # the context of local def calls.
  894. self.locally_assigned = set()
  895. # things that are declared in the argument
  896. # signature of the def callable
  897. self.argument_declared = set()
  898. # closure defs that are defined in this level
  899. self.closuredefs = util.SetLikeDict()
  900. self.node = node
  901. if node is not None:
  902. node.accept_visitor(self)
  903. illegal_names = self.compiler.reserved_names.intersection(
  904. self.locally_declared)
  905. if illegal_names:
  906. raise exceptions.NameConflictError(
  907. "Reserved words declared in template: %s" %
  908. ", ".join(illegal_names))
  909. def branch(self, node, **kwargs):
  910. """create a new Identifiers for a new Node, with
  911. this Identifiers as the parent."""
  912. return _Identifiers(self.compiler, node, self, **kwargs)
  913. @property
  914. def defs(self):
  915. return set(self.topleveldefs.union(self.closuredefs).values())
  916. def __repr__(self):
  917. return "Identifiers(declared=%r, locally_declared=%r, "\
  918. "undeclared=%r, topleveldefs=%r, closuredefs=%r, "\
  919. "argumentdeclared=%r)" %\
  920. (
  921. list(self.declared),
  922. list(self.locally_declared),
  923. list(self.undeclared),
  924. [c.name for c in self.topleveldefs.values()],
  925. [c.name for c in self.closuredefs.values()],
  926. self.argument_declared)
  927. def check_declared(self, node):
  928. """update the state of this Identifiers with the undeclared
  929. and declared identifiers of the given node."""
  930. for ident in node.undeclared_identifiers():
  931. if ident != 'context' and\
  932. ident not in self.declared.union(self.locally_declared):
  933. self.undeclared.add(ident)
  934. for ident in node.declared_identifiers():
  935. self.locally_declared.add(ident)
  936. def add_declared(self, ident):
  937. self.declared.add(ident)
  938. if ident in self.undeclared:
  939. self.undeclared.remove(ident)
  940. def visitExpression(self, node):
  941. self.check_declared(node)
  942. def visitControlLine(self, node):
  943. self.check_declared(node)
  944. def visitCode(self, node):
  945. if not node.ismodule:
  946. self.check_declared(node)
  947. self.locally_assigned = self.locally_assigned.union(
  948. node.declared_identifiers())
  949. def visitNamespaceTag(self, node):
  950. # only traverse into the sub-elements of a
  951. # <%namespace> tag if we are the branch created in
  952. # write_namespaces()
  953. if self.node is node:
  954. for n in node.nodes:
  955. n.accept_visitor(self)
  956. def _check_name_exists(self, collection, node):
  957. existing = collection.get(node.funcname)
  958. collection[node.funcname] = node
  959. if existing is not None and \
  960. existing is not node and \
  961. (node.is_block or existing.is_block):
  962. raise exceptions.CompileException(
  963. "%%def or %%block named '%s' already "
  964. "exists in this template." %
  965. node.funcname, **node.exception_kwargs)
  966. def visitDefTag(self, node):
  967. if node.is_root() and not node.is_anonymous:
  968. self._check_name_exists(self.topleveldefs, node)
  969. elif node is not self.node:
  970. self._check_name_exists(self.closuredefs, node)
  971. for ident in node.undeclared_identifiers():
  972. if ident != 'context' and \
  973. ident not in self.declared.union(self.locally_declared):
  974. self.undeclared.add(ident)
  975. # visit defs only one level deep
  976. if node is self.node:
  977. for ident in node.declared_identifiers():
  978. self.argument_declared.add(ident)
  979. for n in node.nodes:
  980. n.accept_visitor(self)
  981. def visitBlockTag(self, node):
  982. if node is not self.node and not node.is_anonymous:
  983. if isinstance(self.node, parsetree.DefTag):
  984. raise exceptions.CompileException(
  985. "Named block '%s' not allowed inside of def '%s'"
  986. % (node.name, self.node.name), **node.exception_kwargs)
  987. elif isinstance(self.node,
  988. (parsetree.CallTag, parsetree.CallNamespaceTag)):
  989. raise exceptions.CompileException(
  990. "Named block '%s' not allowed inside of <%%call> tag"
  991. % (node.name, ), **node.exception_kwargs)
  992. for ident in node.undeclared_identifiers():
  993. if ident != 'context' and \
  994. ident not in self.declared.union(self.locally_declared):
  995. self.undeclared.add(ident)
  996. if not node.is_anonymous:
  997. self._check_name_exists(self.topleveldefs, node)
  998. self.undeclared.add(node.funcname)
  999. elif node is not self.node:
  1000. self._check_name_exists(self.closuredefs, node)
  1001. for ident in node.declared_identifiers():
  1002. self.argument_declared.add(ident)
  1003. for n in node.nodes:
  1004. n.accept_visitor(self)
  1005. def visitTextTag(self, node):
  1006. for ident in node.undeclared_identifiers():
  1007. if ident != 'context' and \
  1008. ident not in self.declared.union(self.locally_declared):
  1009. self.undeclared.add(ident)
  1010. def visitIncludeTag(self, node):
  1011. self.check_declared(node)
  1012. def visitPageTag(self, node):
  1013. for ident in node.declared_identifiers():
  1014. self.argument_declared.add(ident)
  1015. self.check_declared(node)
  1016. def visitCallNamespaceTag(self, node):
  1017. self.visitCallTag(node)
  1018. def visitCallTag(self, node):
  1019. if node is self.node:
  1020. for ident in node.undeclared_identifiers():
  1021. if ident != 'context' and \
  1022. ident not in self.declared.union(
  1023. self.locally_declared):
  1024. self.undeclared.add(ident)
  1025. for ident in node.declared_identifiers():
  1026. self.argument_declared.add(ident)
  1027. for n in node.nodes:
  1028. n.accept_visitor(self)
  1029. else:
  1030. for ident in node.undeclared_identifiers():
  1031. if ident != 'context' and \
  1032. ident not in self.declared.union(
  1033. self.locally_declared):
  1034. self.undeclared.add(ident)
  1035. _FOR_LOOP = re.compile(
  1036. r'^for\s+((?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*'
  1037. r'(?:\s*,\s*(?:[A-Za-z_][A-Za-z0-9_]*),??)*\s*(?:\)?))\s+in\s+(.*):'
  1038. )
  1039. def mangle_mako_loop(node, printer):
  1040. """converts a for loop into a context manager wrapped around a for loop
  1041. when access to the `loop` variable has been detected in the for loop body
  1042. """
  1043. loop_variable = LoopVariable()
  1044. node.accept_visitor(loop_variable)
  1045. if loop_variable.detected:
  1046. node.nodes[-1].has_loop_context = True
  1047. match = _FOR_LOOP.match(node.text)
  1048. if match:
  1049. printer.writelines(
  1050. 'loop = __M_loop._enter(%s)' % match.group(2),
  1051. 'try:'
  1052. # 'with __M_loop(%s) as loop:' % match.group(2)
  1053. )
  1054. text = 'for %s in loop:' % match.group(1)
  1055. else:
  1056. raise SyntaxError("Couldn't apply loop context: %s" % node.text)
  1057. else:
  1058. text = node.text
  1059. return text
  1060. class LoopVariable(object):
  1061. """A node visitor which looks for the name 'loop' within undeclared
  1062. identifiers."""
  1063. def __init__(self):
  1064. self.detected = False
  1065. def _loop_reference_detected(self, node):
  1066. if 'loop' in node.undeclared_identifiers():
  1067. self.detected = True
  1068. else:
  1069. for n in node.get_children():
  1070. n.accept_visitor(self)
  1071. def visitControlLine(self, node):
  1072. self._loop_reference_detected(node)
  1073. def visitCode(self, node):
  1074. self._loop_reference_detected(node)
  1075. def visitExpression(self, node):
  1076. self._loop_reference_detected(node)