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

发布于 2024-07-21 15:36:03 字数 509 浏览 5 评论 0原文

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

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

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

*

杰里米写道: 对于生产站点,我使用 Cheetah 使用预编译的模板 - 它是 非常快(模板导入 当 python 时尤其快 编译和优化)。 一点 imp 模块的魔法需要 模板名称和基本目录 (在特定于站点的配置中配置) 并加载该模板,采取 照顾#extends 和

酌情导入指令。 我不使用内置支持

然而,猎豹。 新模板 库也只导入到 显示调试错误页面

*

How do you serve Cheetah in production?

Guys can you share the setup on how to precompile and serve cheetah in production

Since we dont compile templates in webpy it is getting upstream time out errors. If you could share a good best practise it would help

*

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

*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2024-07-28 15:36:03

也许根据需要自动编译:

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

Maybe compile automagically on as needed basis:

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
那一片橙海, 2024-07-28 15:36:03

这有效

try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass

这是我对你的代码所做的

from cheetahimport import *
sys.path.append('./templates')
cheetah_import('mafbase')

包括在给定方法中不起作用。

这是我遇到的错误

    localhost pop]$ vi code.py
    [mark@localhost pop]$ ./code.py 9911
    http://0.0.0.0:9911/
    Traceback (most recent call last):
     File "/home/mark/work/common/web/application.py", line 241, in process
     return self.handle()
    File "/home/mark/work/common/web/application.py", line 232, in handle
    return self._delegate(fn, self.fvars, args)
    File "/home/mark/work/common/web/application.py", line 411, in _delegate
    return handle_class(cls)
    File "/home/mark/work/common/web/application.py", line 386, in handle_class
    return tocall(*args)
    File "user.py", line 264, in proxyfunc
    return func(self, *args, **kw)
    File "/home/mark/work/pop/code.py", line 1801, in GET
    return web.render('subclass.html')
    File "/home/mark/work/common/web/cheetah.py", line 104, in render
    return str(compiled_tmpl)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__
    def __str__(self): return getattr(self, mainMethName)()
    File "templates/mafbase.py", line 713, in respond
    self._handleCheetahInclude("widgetbox.html", trans=trans, includeFrom="file", raw=False)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude
    nestedTemplateClass = compiler.compile(source=source,file=file)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile
     fileHash = str(hash(file))+str(os.path.getmtime(file))
   File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime
    return os.stat(filename).st_mtime
   OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html'

This works

try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass

This is what i did with you code

from cheetahimport import *
sys.path.append('./templates')
cheetah_import('mafbase')

includes dont work in the given method.

This is the error i got

    localhost pop]$ vi code.py
    [mark@localhost pop]$ ./code.py 9911
    http://0.0.0.0:9911/
    Traceback (most recent call last):
     File "/home/mark/work/common/web/application.py", line 241, in process
     return self.handle()
    File "/home/mark/work/common/web/application.py", line 232, in handle
    return self._delegate(fn, self.fvars, args)
    File "/home/mark/work/common/web/application.py", line 411, in _delegate
    return handle_class(cls)
    File "/home/mark/work/common/web/application.py", line 386, in handle_class
    return tocall(*args)
    File "user.py", line 264, in proxyfunc
    return func(self, *args, **kw)
    File "/home/mark/work/pop/code.py", line 1801, in GET
    return web.render('subclass.html')
    File "/home/mark/work/common/web/cheetah.py", line 104, in render
    return str(compiled_tmpl)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__
    def __str__(self): return getattr(self, mainMethName)()
    File "templates/mafbase.py", line 713, in respond
    self._handleCheetahInclude("widgetbox.html", trans=trans, includeFrom="file", raw=False)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude
    nestedTemplateClass = compiler.compile(source=source,file=file)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile
     fileHash = str(hash(file))+str(os.path.getmtime(file))
   File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime
    return os.stat(filename).st_mtime
   OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文