Finds all the templates the loader can find, compiles them and stores them in `target`. If `zip` is `None`, instead of in a zipfile, the templates will be stored in a directory. By default a deflate zip algorithm is used. To switch to the stored algorithm, `zip` can
(self, target, extensions=None, filter_func=None,
zip='deflated', log_function=None,
ignore_errors=True, py_compile=False)
| 636 | return TemplateExpression(template, undefined_to_none) |
| 637 | |
| 638 | def compile_templates(self, target, extensions=None, filter_func=None, |
| 639 | zip='deflated', log_function=None, |
| 640 | ignore_errors=True, py_compile=False): |
| 641 | """Finds all the templates the loader can find, compiles them |
| 642 | and stores them in `target`. If `zip` is `None`, instead of in a |
| 643 | zipfile, the templates will be stored in a directory. |
| 644 | By default a deflate zip algorithm is used. To switch to |
| 645 | the stored algorithm, `zip` can be set to ``'stored'``. |
| 646 | |
| 647 | `extensions` and `filter_func` are passed to :meth:`list_templates`. |
| 648 | Each template returned will be compiled to the target folder or |
| 649 | zipfile. |
| 650 | |
| 651 | By default template compilation errors are ignored. In case a |
| 652 | log function is provided, errors are logged. If you want template |
| 653 | syntax errors to abort the compilation you can set `ignore_errors` |
| 654 | to `False` and you will get an exception on syntax errors. |
| 655 | |
| 656 | If `py_compile` is set to `True` .pyc files will be written to the |
| 657 | target instead of standard .py files. This flag does not do anything |
| 658 | on pypy and Python 3 where pyc files are not picked up by itself and |
| 659 | don't give much benefit. |
| 660 | |
| 661 | .. versionadded:: 2.4 |
| 662 | """ |
| 663 | from jinja2.loaders import ModuleLoader |
| 664 | |
| 665 | if log_function is None: |
| 666 | log_function = lambda x: None |
| 667 | |
| 668 | if py_compile: |
| 669 | if not PY2 or PYPY: |
| 670 | from warnings import warn |
| 671 | warn(Warning('py_compile has no effect on pypy or Python 3')) |
| 672 | py_compile = False |
| 673 | else: |
| 674 | import imp |
| 675 | import marshal |
| 676 | py_header = imp.get_magic() + \ |
| 677 | u'\xff\xff\xff\xff'.encode('iso-8859-15') |
| 678 | |
| 679 | # Python 3.3 added a source filesize to the header |
| 680 | if sys.version_info >= (3, 3): |
| 681 | py_header += u'\x00\x00\x00\x00'.encode('iso-8859-15') |
| 682 | |
| 683 | def write_file(filename, data, mode): |
| 684 | if zip: |
| 685 | info = ZipInfo(filename) |
| 686 | info.external_attr = 0o755 << 16 |
| 687 | zip_file.writestr(info, data) |
| 688 | else: |
| 689 | f = open(os.path.join(target, filename), mode) |
| 690 | try: |
| 691 | f.write(data) |
| 692 | finally: |
| 693 | f.close() |
| 694 | |
| 695 | if zip is not None: |
nothing calls this directly
no test coverage detected