gpt4 book ai didi

python - 我们如何在 Cheetah 中预编译基本模板,以便 #include、#extends 和 #import 在 Weby 中正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:58 27 4
gpt4 key购买 nike

您如何在生产中为Cheetah服务?

伙计们,你能分享一下如何在生产中预编译和服务 cheetah 的设置吗

由于我们不在 webpy 中编译模板,因此出现上游超时错误。如果您可以分享一个好的最佳实践,将会有所帮助

*

Jeremy wrote: For a production site, I use Cheetah with pre-compiled templates - it's very fast (the templates import especially quickly when python compiled and optimised). A bit of magic with the imp module takes a template name and a base directory (configured in a site-specific config) and loads up that template, taking care of #extends and

import directives as appropriate. I don't use the built-in support for

Cheetah, however. The new template library is also only imported to display the debugerror page

*

最佳答案

也许根据需要自动编译:

import sys
import os
from os import path
import logging
from Cheetah.Template import Template
from Cheetah.Compiler import Compiler

log = logging.getLogger(__name__)

_import_save = __import__
def cheetah_import(name, *args, **kw):
"""Import function which search for Cheetah templates.

When template ``*.tmpl`` is found in ``sys.path`` matching module
name (and corresponding generated Python module is outdated or
not existent) it will be compiled prior to actual import.
"""
name_parts = name.split('.')
for p in sys.path:
basename = path.join(p, *name_parts)
tmpl_path = basename+'.tmpl'
py_path = basename+'.py'
if path.exists(tmpl_path):
log.debug("%s found in %r", name, tmpl_path)
if not path.exists(py_path) or newer(tmpl_path, py_path):
log.info("cheetah compile %r -> %r", tmpl_path, py_path)
output = Compiler(
file=tmpl_path,
moduleName=name,
mainClassName=name_parts[-1],
)
open(py_path, 'wb').write(str(output))
break
return _import_save(name, *args, **kw)

def newer(new, old):
"""Whether file with path ``new`` is newer then at ``old``."""
return os.stat(new).st_mtime > os.stat(old).st_mtime

import __builtin__
__builtin__.__import__ = cheetah_import

关于python - 我们如何在 Cheetah 中预编译基本模板,以便 #include、#extends 和 #import 在 Weby 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/919539/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com