template.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. # mako/template.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 the Template class, a facade for parsing, generating and executing
  7. template strings, as well as template runtime operations."""
  8. from mako.lexer import Lexer
  9. from mako import runtime, util, exceptions, codegen, cache, compat
  10. import os
  11. import re
  12. import shutil
  13. import stat
  14. import sys
  15. import tempfile
  16. import types
  17. import weakref
  18. class Template(object):
  19. """Represents a compiled template.
  20. :class:`.Template` includes a reference to the original
  21. template source (via the :attr:`.source` attribute)
  22. as well as the source code of the
  23. generated Python module (i.e. the :attr:`.code` attribute),
  24. as well as a reference to an actual Python module.
  25. :class:`.Template` is constructed using either a literal string
  26. representing the template text, or a filename representing a filesystem
  27. path to a source file.
  28. :param text: textual template source. This argument is mutually
  29. exclusive versus the ``filename`` parameter.
  30. :param filename: filename of the source template. This argument is
  31. mutually exclusive versus the ``text`` parameter.
  32. :param buffer_filters: string list of filters to be applied
  33. to the output of ``%def``\ s which are buffered, cached, or otherwise
  34. filtered, after all filters
  35. defined with the ``%def`` itself have been applied. Allows the
  36. creation of default expression filters that let the output
  37. of return-valued ``%def``\ s "opt out" of that filtering via
  38. passing special attributes or objects.
  39. :param bytestring_passthrough: When ``True``, and ``output_encoding`` is
  40. set to ``None``, and :meth:`.Template.render` is used to render,
  41. the `StringIO` or `cStringIO` buffer will be used instead of the
  42. default "fast" buffer. This allows raw bytestrings in the
  43. output stream, such as in expressions, to pass straight
  44. through to the buffer. This flag is forced
  45. to ``True`` if ``disable_unicode`` is also configured.
  46. .. versionadded:: 0.4
  47. Added to provide the same behavior as that of the previous series.
  48. :param cache_args: Dictionary of cache configuration arguments that
  49. will be passed to the :class:`.CacheImpl`. See :ref:`caching_toplevel`.
  50. :param cache_dir:
  51. .. deprecated:: 0.6
  52. Use the ``'dir'`` argument in the ``cache_args`` dictionary.
  53. See :ref:`caching_toplevel`.
  54. :param cache_enabled: Boolean flag which enables caching of this
  55. template. See :ref:`caching_toplevel`.
  56. :param cache_impl: String name of a :class:`.CacheImpl` caching
  57. implementation to use. Defaults to ``'beaker'``.
  58. :param cache_type:
  59. .. deprecated:: 0.6
  60. Use the ``'type'`` argument in the ``cache_args`` dictionary.
  61. See :ref:`caching_toplevel`.
  62. :param cache_url:
  63. .. deprecated:: 0.6
  64. Use the ``'url'`` argument in the ``cache_args`` dictionary.
  65. See :ref:`caching_toplevel`.
  66. :param default_filters: List of string filter names that will
  67. be applied to all expressions. See :ref:`filtering_default_filters`.
  68. :param disable_unicode: Disables all awareness of Python Unicode
  69. objects. See :ref:`unicode_disabled`.
  70. :param enable_loop: When ``True``, enable the ``loop`` context variable.
  71. This can be set to ``False`` to support templates that may
  72. be making usage of the name "``loop``". Individual templates can
  73. re-enable the "loop" context by placing the directive
  74. ``enable_loop="True"`` inside the ``<%page>`` tag -- see
  75. :ref:`migrating_loop`.
  76. :param encoding_errors: Error parameter passed to ``encode()`` when
  77. string encoding is performed. See :ref:`usage_unicode`.
  78. :param error_handler: Python callable which is called whenever
  79. compile or runtime exceptions occur. The callable is passed
  80. the current context as well as the exception. If the
  81. callable returns ``True``, the exception is considered to
  82. be handled, else it is re-raised after the function
  83. completes. Is used to provide custom error-rendering
  84. functions.
  85. .. seealso::
  86. :paramref:`.Template.include_error_handler` - include-specific
  87. error handler function
  88. :param format_exceptions: if ``True``, exceptions which occur during
  89. the render phase of this template will be caught and
  90. formatted into an HTML error page, which then becomes the
  91. rendered result of the :meth:`.render` call. Otherwise,
  92. runtime exceptions are propagated outwards.
  93. :param imports: String list of Python statements, typically individual
  94. "import" lines, which will be placed into the module level
  95. preamble of all generated Python modules. See the example
  96. in :ref:`filtering_default_filters`.
  97. :param future_imports: String list of names to import from `__future__`.
  98. These will be concatenated into a comma-separated string and inserted
  99. into the beginning of the template, e.g. ``futures_imports=['FOO',
  100. 'BAR']`` results in ``from __future__ import FOO, BAR``. If you're
  101. interested in using features like the new division operator, you must
  102. use future_imports to convey that to the renderer, as otherwise the
  103. import will not appear as the first executed statement in the generated
  104. code and will therefore not have the desired effect.
  105. :param include_error_handler: An error handler that runs when this template
  106. is included within another one via the ``<%include>`` tag, and raises an
  107. error. Compare to the :paramref:`.Template.error_handler` option.
  108. .. versionadded:: 1.0.6
  109. .. seealso::
  110. :paramref:`.Template.error_handler` - top-level error handler function
  111. :param input_encoding: Encoding of the template's source code. Can
  112. be used in lieu of the coding comment. See
  113. :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for
  114. details on source encoding.
  115. :param lookup: a :class:`.TemplateLookup` instance that will be used
  116. for all file lookups via the ``<%namespace>``,
  117. ``<%include>``, and ``<%inherit>`` tags. See
  118. :ref:`usage_templatelookup`.
  119. :param module_directory: Filesystem location where generated
  120. Python module files will be placed.
  121. :param module_filename: Overrides the filename of the generated
  122. Python module file. For advanced usage only.
  123. :param module_writer: A callable which overrides how the Python
  124. module is written entirely. The callable is passed the
  125. encoded source content of the module and the destination
  126. path to be written to. The default behavior of module writing
  127. uses a tempfile in conjunction with a file move in order
  128. to make the operation atomic. So a user-defined module
  129. writing function that mimics the default behavior would be:
  130. .. sourcecode:: python
  131. import tempfile
  132. import os
  133. import shutil
  134. def module_writer(source, outputpath):
  135. (dest, name) = \\
  136. tempfile.mkstemp(
  137. dir=os.path.dirname(outputpath)
  138. )
  139. os.write(dest, source)
  140. os.close(dest)
  141. shutil.move(name, outputpath)
  142. from mako.template import Template
  143. mytemplate = Template(
  144. filename="index.html",
  145. module_directory="/path/to/modules",
  146. module_writer=module_writer
  147. )
  148. The function is provided for unusual configurations where
  149. certain platform-specific permissions or other special
  150. steps are needed.
  151. :param output_encoding: The encoding to use when :meth:`.render`
  152. is called.
  153. See :ref:`usage_unicode` as well as :ref:`unicode_toplevel`.
  154. :param preprocessor: Python callable which will be passed
  155. the full template source before it is parsed. The return
  156. result of the callable will be used as the template source
  157. code.
  158. :param lexer_cls: A :class:`.Lexer` class used to parse
  159. the template. The :class:`.Lexer` class is used by
  160. default.
  161. .. versionadded:: 0.7.4
  162. :param strict_undefined: Replaces the automatic usage of
  163. ``UNDEFINED`` for any undeclared variables not located in
  164. the :class:`.Context` with an immediate raise of
  165. ``NameError``. The advantage is immediate reporting of
  166. missing variables which include the name.
  167. .. versionadded:: 0.3.6
  168. :param uri: string URI or other identifier for this template.
  169. If not provided, the ``uri`` is generated from the filesystem
  170. path, or from the in-memory identity of a non-file-based
  171. template. The primary usage of the ``uri`` is to provide a key
  172. within :class:`.TemplateLookup`, as well as to generate the
  173. file path of the generated Python module file, if
  174. ``module_directory`` is specified.
  175. """
  176. lexer_cls = Lexer
  177. def __init__(self,
  178. text=None,
  179. filename=None,
  180. uri=None,
  181. format_exceptions=False,
  182. error_handler=None,
  183. lookup=None,
  184. output_encoding=None,
  185. encoding_errors='strict',
  186. module_directory=None,
  187. cache_args=None,
  188. cache_impl='beaker',
  189. cache_enabled=True,
  190. cache_type=None,
  191. cache_dir=None,
  192. cache_url=None,
  193. module_filename=None,
  194. input_encoding=None,
  195. disable_unicode=False,
  196. module_writer=None,
  197. bytestring_passthrough=False,
  198. default_filters=None,
  199. buffer_filters=(),
  200. strict_undefined=False,
  201. imports=None,
  202. future_imports=None,
  203. enable_loop=True,
  204. preprocessor=None,
  205. lexer_cls=None,
  206. include_error_handler=None):
  207. if uri:
  208. self.module_id = re.sub(r'\W', "_", uri)
  209. self.uri = uri
  210. elif filename:
  211. self.module_id = re.sub(r'\W', "_", filename)
  212. drive, path = os.path.splitdrive(filename)
  213. path = os.path.normpath(path).replace(os.path.sep, "/")
  214. self.uri = path
  215. else:
  216. self.module_id = "memory:" + hex(id(self))
  217. self.uri = self.module_id
  218. u_norm = self.uri
  219. if u_norm.startswith("/"):
  220. u_norm = u_norm[1:]
  221. u_norm = os.path.normpath(u_norm)
  222. if u_norm.startswith(".."):
  223. raise exceptions.TemplateLookupException(
  224. "Template uri \"%s\" is invalid - "
  225. "it cannot be relative outside "
  226. "of the root path." % self.uri)
  227. self.input_encoding = input_encoding
  228. self.output_encoding = output_encoding
  229. self.encoding_errors = encoding_errors
  230. self.disable_unicode = disable_unicode
  231. self.bytestring_passthrough = bytestring_passthrough or disable_unicode
  232. self.enable_loop = enable_loop
  233. self.strict_undefined = strict_undefined
  234. self.module_writer = module_writer
  235. if compat.py3k and disable_unicode:
  236. raise exceptions.UnsupportedError(
  237. "Mako for Python 3 does not "
  238. "support disabling Unicode")
  239. elif output_encoding and disable_unicode:
  240. raise exceptions.UnsupportedError(
  241. "output_encoding must be set to "
  242. "None when disable_unicode is used.")
  243. if default_filters is None:
  244. if compat.py3k or self.disable_unicode:
  245. self.default_filters = ['str']
  246. else:
  247. self.default_filters = ['unicode']
  248. else:
  249. self.default_filters = default_filters
  250. self.buffer_filters = buffer_filters
  251. self.imports = imports
  252. self.future_imports = future_imports
  253. self.preprocessor = preprocessor
  254. if lexer_cls is not None:
  255. self.lexer_cls = lexer_cls
  256. # if plain text, compile code in memory only
  257. if text is not None:
  258. (code, module) = _compile_text(self, text, filename)
  259. self._code = code
  260. self._source = text
  261. ModuleInfo(module, None, self, filename, code, text)
  262. elif filename is not None:
  263. # if template filename and a module directory, load
  264. # a filesystem-based module file, generating if needed
  265. if module_filename is not None:
  266. path = module_filename
  267. elif module_directory is not None:
  268. path = os.path.abspath(
  269. os.path.join(
  270. os.path.normpath(module_directory),
  271. u_norm + ".py"
  272. )
  273. )
  274. else:
  275. path = None
  276. module = self._compile_from_file(path, filename)
  277. else:
  278. raise exceptions.RuntimeException(
  279. "Template requires text or filename")
  280. self.module = module
  281. self.filename = filename
  282. self.callable_ = self.module.render_body
  283. self.format_exceptions = format_exceptions
  284. self.error_handler = error_handler
  285. self.include_error_handler = include_error_handler
  286. self.lookup = lookup
  287. self.module_directory = module_directory
  288. self._setup_cache_args(
  289. cache_impl, cache_enabled, cache_args,
  290. cache_type, cache_dir, cache_url
  291. )
  292. @util.memoized_property
  293. def reserved_names(self):
  294. if self.enable_loop:
  295. return codegen.RESERVED_NAMES
  296. else:
  297. return codegen.RESERVED_NAMES.difference(['loop'])
  298. def _setup_cache_args(self,
  299. cache_impl, cache_enabled, cache_args,
  300. cache_type, cache_dir, cache_url):
  301. self.cache_impl = cache_impl
  302. self.cache_enabled = cache_enabled
  303. if cache_args:
  304. self.cache_args = cache_args
  305. else:
  306. self.cache_args = {}
  307. # transfer deprecated cache_* args
  308. if cache_type:
  309. self.cache_args['type'] = cache_type
  310. if cache_dir:
  311. self.cache_args['dir'] = cache_dir
  312. if cache_url:
  313. self.cache_args['url'] = cache_url
  314. def _compile_from_file(self, path, filename):
  315. if path is not None:
  316. util.verify_directory(os.path.dirname(path))
  317. filemtime = os.stat(filename)[stat.ST_MTIME]
  318. if not os.path.exists(path) or \
  319. os.stat(path)[stat.ST_MTIME] < filemtime:
  320. data = util.read_file(filename)
  321. _compile_module_file(
  322. self,
  323. data,
  324. filename,
  325. path,
  326. self.module_writer)
  327. module = compat.load_module(self.module_id, path)
  328. del sys.modules[self.module_id]
  329. if module._magic_number != codegen.MAGIC_NUMBER:
  330. data = util.read_file(filename)
  331. _compile_module_file(
  332. self,
  333. data,
  334. filename,
  335. path,
  336. self.module_writer)
  337. module = compat.load_module(self.module_id, path)
  338. del sys.modules[self.module_id]
  339. ModuleInfo(module, path, self, filename, None, None)
  340. else:
  341. # template filename and no module directory, compile code
  342. # in memory
  343. data = util.read_file(filename)
  344. code, module = _compile_text(
  345. self,
  346. data,
  347. filename)
  348. self._source = None
  349. self._code = code
  350. ModuleInfo(module, None, self, filename, code, None)
  351. return module
  352. @property
  353. def source(self):
  354. """Return the template source code for this :class:`.Template`."""
  355. return _get_module_info_from_callable(self.callable_).source
  356. @property
  357. def code(self):
  358. """Return the module source code for this :class:`.Template`."""
  359. return _get_module_info_from_callable(self.callable_).code
  360. @util.memoized_property
  361. def cache(self):
  362. return cache.Cache(self)
  363. @property
  364. def cache_dir(self):
  365. return self.cache_args['dir']
  366. @property
  367. def cache_url(self):
  368. return self.cache_args['url']
  369. @property
  370. def cache_type(self):
  371. return self.cache_args['type']
  372. def render(self, *args, **data):
  373. """Render the output of this template as a string.
  374. If the template specifies an output encoding, the string
  375. will be encoded accordingly, else the output is raw (raw
  376. output uses `cStringIO` and can't handle multibyte
  377. characters). A :class:`.Context` object is created corresponding
  378. to the given data. Arguments that are explicitly declared
  379. by this template's internal rendering method are also
  380. pulled from the given ``*args``, ``**data`` members.
  381. """
  382. return runtime._render(self, self.callable_, args, data)
  383. def render_unicode(self, *args, **data):
  384. """Render the output of this template as a unicode object."""
  385. return runtime._render(self,
  386. self.callable_,
  387. args,
  388. data,
  389. as_unicode=True)
  390. def render_context(self, context, *args, **kwargs):
  391. """Render this :class:`.Template` with the given context.
  392. The data is written to the context's buffer.
  393. """
  394. if getattr(context, '_with_template', None) is None:
  395. context._set_with_template(self)
  396. runtime._render_context(self,
  397. self.callable_,
  398. context,
  399. *args,
  400. **kwargs)
  401. def has_def(self, name):
  402. return hasattr(self.module, "render_%s" % name)
  403. def get_def(self, name):
  404. """Return a def of this template as a :class:`.DefTemplate`."""
  405. return DefTemplate(self, getattr(self.module, "render_%s" % name))
  406. def list_defs(self):
  407. """return a list of defs in the template.
  408. .. versionadded:: 1.0.4
  409. """
  410. return [i[7:] for i in dir(self.module) if i[:7] == 'render_']
  411. def _get_def_callable(self, name):
  412. return getattr(self.module, "render_%s" % name)
  413. @property
  414. def last_modified(self):
  415. return self.module._modified_time
  416. class ModuleTemplate(Template):
  417. """A Template which is constructed given an existing Python module.
  418. e.g.::
  419. t = Template("this is a template")
  420. f = file("mymodule.py", "w")
  421. f.write(t.code)
  422. f.close()
  423. import mymodule
  424. t = ModuleTemplate(mymodule)
  425. print t.render()
  426. """
  427. def __init__(self, module,
  428. module_filename=None,
  429. template=None,
  430. template_filename=None,
  431. module_source=None,
  432. template_source=None,
  433. output_encoding=None,
  434. encoding_errors='strict',
  435. disable_unicode=False,
  436. bytestring_passthrough=False,
  437. format_exceptions=False,
  438. error_handler=None,
  439. lookup=None,
  440. cache_args=None,
  441. cache_impl='beaker',
  442. cache_enabled=True,
  443. cache_type=None,
  444. cache_dir=None,
  445. cache_url=None,
  446. include_error_handler=None,
  447. ):
  448. self.module_id = re.sub(r'\W', "_", module._template_uri)
  449. self.uri = module._template_uri
  450. self.input_encoding = module._source_encoding
  451. self.output_encoding = output_encoding
  452. self.encoding_errors = encoding_errors
  453. self.disable_unicode = disable_unicode
  454. self.bytestring_passthrough = bytestring_passthrough or disable_unicode
  455. self.enable_loop = module._enable_loop
  456. if compat.py3k and disable_unicode:
  457. raise exceptions.UnsupportedError(
  458. "Mako for Python 3 does not "
  459. "support disabling Unicode")
  460. elif output_encoding and disable_unicode:
  461. raise exceptions.UnsupportedError(
  462. "output_encoding must be set to "
  463. "None when disable_unicode is used.")
  464. self.module = module
  465. self.filename = template_filename
  466. ModuleInfo(module,
  467. module_filename,
  468. self,
  469. template_filename,
  470. module_source,
  471. template_source)
  472. self.callable_ = self.module.render_body
  473. self.format_exceptions = format_exceptions
  474. self.error_handler = error_handler
  475. self.include_error_handler = include_error_handler
  476. self.lookup = lookup
  477. self._setup_cache_args(
  478. cache_impl, cache_enabled, cache_args,
  479. cache_type, cache_dir, cache_url
  480. )
  481. class DefTemplate(Template):
  482. """A :class:`.Template` which represents a callable def in a parent
  483. template."""
  484. def __init__(self, parent, callable_):
  485. self.parent = parent
  486. self.callable_ = callable_
  487. self.output_encoding = parent.output_encoding
  488. self.module = parent.module
  489. self.encoding_errors = parent.encoding_errors
  490. self.format_exceptions = parent.format_exceptions
  491. self.error_handler = parent.error_handler
  492. self.include_error_handler = parent.include_error_handler
  493. self.enable_loop = parent.enable_loop
  494. self.lookup = parent.lookup
  495. self.bytestring_passthrough = parent.bytestring_passthrough
  496. def get_def(self, name):
  497. return self.parent.get_def(name)
  498. class ModuleInfo(object):
  499. """Stores information about a module currently loaded into
  500. memory, provides reverse lookups of template source, module
  501. source code based on a module's identifier.
  502. """
  503. _modules = weakref.WeakValueDictionary()
  504. def __init__(self,
  505. module,
  506. module_filename,
  507. template,
  508. template_filename,
  509. module_source,
  510. template_source):
  511. self.module = module
  512. self.module_filename = module_filename
  513. self.template_filename = template_filename
  514. self.module_source = module_source
  515. self.template_source = template_source
  516. self._modules[module.__name__] = template._mmarker = self
  517. if module_filename:
  518. self._modules[module_filename] = self
  519. @classmethod
  520. def get_module_source_metadata(cls, module_source, full_line_map=False):
  521. source_map = re.search(
  522. r"__M_BEGIN_METADATA(.+?)__M_END_METADATA",
  523. module_source, re.S).group(1)
  524. source_map = compat.json.loads(source_map)
  525. source_map['line_map'] = dict(
  526. (int(k), int(v))
  527. for k, v in source_map['line_map'].items())
  528. if full_line_map:
  529. f_line_map = source_map['full_line_map'] = []
  530. line_map = source_map['line_map']
  531. curr_templ_line = 1
  532. for mod_line in range(1, max(line_map)):
  533. if mod_line in line_map:
  534. curr_templ_line = line_map[mod_line]
  535. f_line_map.append(curr_templ_line)
  536. return source_map
  537. @property
  538. def code(self):
  539. if self.module_source is not None:
  540. return self.module_source
  541. else:
  542. return util.read_python_file(self.module_filename)
  543. @property
  544. def source(self):
  545. if self.template_source is not None:
  546. if self.module._source_encoding and \
  547. not isinstance(self.template_source, compat.text_type):
  548. return self.template_source.decode(
  549. self.module._source_encoding)
  550. else:
  551. return self.template_source
  552. else:
  553. data = util.read_file(self.template_filename)
  554. if self.module._source_encoding:
  555. return data.decode(self.module._source_encoding)
  556. else:
  557. return data
  558. def _compile(template, text, filename, generate_magic_comment):
  559. lexer = template.lexer_cls(text,
  560. filename,
  561. disable_unicode=template.disable_unicode,
  562. input_encoding=template.input_encoding,
  563. preprocessor=template.preprocessor)
  564. node = lexer.parse()
  565. source = codegen.compile(node,
  566. template.uri,
  567. filename,
  568. default_filters=template.default_filters,
  569. buffer_filters=template.buffer_filters,
  570. imports=template.imports,
  571. future_imports=template.future_imports,
  572. source_encoding=lexer.encoding,
  573. generate_magic_comment=generate_magic_comment,
  574. disable_unicode=template.disable_unicode,
  575. strict_undefined=template.strict_undefined,
  576. enable_loop=template.enable_loop,
  577. reserved_names=template.reserved_names)
  578. return source, lexer
  579. def _compile_text(template, text, filename):
  580. identifier = template.module_id
  581. source, lexer = _compile(template, text, filename,
  582. generate_magic_comment=template.disable_unicode)
  583. cid = identifier
  584. if not compat.py3k and isinstance(cid, compat.text_type):
  585. cid = cid.encode()
  586. module = types.ModuleType(cid)
  587. code = compile(source, cid, 'exec')
  588. # this exec() works for 2.4->3.3.
  589. exec(code, module.__dict__, module.__dict__)
  590. return (source, module)
  591. def _compile_module_file(template, text, filename, outputpath, module_writer):
  592. source, lexer = _compile(template, text, filename,
  593. generate_magic_comment=True)
  594. if isinstance(source, compat.text_type):
  595. source = source.encode(lexer.encoding or 'ascii')
  596. if module_writer:
  597. module_writer(source, outputpath)
  598. else:
  599. # make tempfiles in the same location as the ultimate
  600. # location. this ensures they're on the same filesystem,
  601. # avoiding synchronization issues.
  602. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath))
  603. os.write(dest, source)
  604. os.close(dest)
  605. shutil.move(name, outputpath)
  606. def _get_module_info_from_callable(callable_):
  607. if compat.py3k:
  608. return _get_module_info(callable_.__globals__['__name__'])
  609. else:
  610. return _get_module_info(callable_.func_globals['__name__'])
  611. def _get_module_info(filename):
  612. return ModuleInfo._modules[filename]