CherryPy with Cheetah as plugin + tool - blank pages

烂漫一生 提交于 2019-11-27 08:30:18

问题


CherryPy keeps returning blank pages or with the values I return in the controllers. I rewrote a django and jinja2 version that did work, apparently this one doesn't which is almost identical to the previous mentioned.

I did some pprint's in the tool bit which does fill the request.body with parsed html but doesn't output it when pass is set in the controller. If I return a {'user':True} in the controller that is shown in the form of a simple "User".

with a few examples online and the code of SickBeard I came to the following:

controller:

class RootController(object):
    @cherrypy.expose
    @cherrypy.tools.render(template="page/home.html")
    def index(self):
        pass

tool:

class CheetahTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self._render,
                               priority=30)

    def _render(self, template=None, debug=False):
        if cherrypy.response.status > 399:
            return

        # retrieve the data returned by the handler
        data = cherrypy.response.body or {}
        template = cherrypy.engine.publish("lookup-template", template).pop()

        if template and isinstance(data, dict):
            for k,v in data:
                template.__setattr__(k, v)

            # dump the template using the dictionary
            if debug:
                try:
                    cherrypy.response.body = unicode(template).encode('utf-8', 'xmlcharrefreplace')
                except Exception as e:
                    from pprint import pprint
                    pprint(e.message)
            else:
                cherrypy.response.body = template.respond()

plugin:

class PageTemplate(Template):
    """
    Thank you SickBeard
    """
    def __init__(self, base_dir, template, *args, **KWs):
        KWs['file'] = os.path.join(base_dir, template)
        super(PageTemplate, self).__init__(*args, **KWs)
        application = cherrypy.tree.apps['']
        config = application.config 
        self.sbRoot = base_dir
        self.sbHttpPort = config['global']['server.socket_port']
        self.sbHttpsPort = self.sbHttpPort
        self.sbHttpsEnabled = False
        if cherrypy.request.headers['Host'][0] == '[':
            self.sbHost = re.match("^\[.*\]", cherrypy.request.headers['Host'], re.X|re.M|re.S).group(0)
        else:
            self.sbHost = re.match("^[^:]+", cherrypy.request.headers['Host'], re.X|re.M|re.S).group(0)

        if "X-Forwarded-Host" in cherrypy.request.headers:
            self.sbHost = cherrypy.request.headers['X-Forwarded-Host']
        if "X-Forwarded-Port" in cherrypy.request.headers:
            self.sbHttpPort = cherrypy.request.headers['X-Forwarded-Port']
            self.sbHttpsPort = self.sbHttpPort
        if "X-Forwarded-Proto" in cherrypy.request.headers:
            self.sbHttpsEnabled = True if cherrypy.request.headers['X-Forwarded-Proto'] == 'https' else False

        self.sbPID = str(aquapi.PID)
        self.menu = [
            { 'title': 'Home',            'key': 'home'           },
            { 'title': 'Users',           'key': 'users'          },
            { 'title': 'Config',          'key': 'config'         },
        ]

    def render(self):
        return unicode(self).encode('utf-8', 'xmlcharrefreplace')


class CheetahTemplatePlugin(plugins.SimplePlugin):
    def __init__(self, bus, base_dir=None, base_cache_dir=None, 
                 collection_size=50, encoding='utf-8'):
        plugins.SimplePlugin.__init__(self, bus)
        self.base_dir = base_dir
        self.base_cache_dir = base_cache_dir or tempfile.gettempdir()
        self.encoding = encoding
        self.collection_size = collection_size

    def start(self):
        self.bus.log('Setting up Cheetah resources')
        self.bus.subscribe("lookup-template", self.get_template)

    def stop(self):
        self.bus.log('Freeing up Cheetah resources')
        self.bus.unsubscribe("lookup-template", self.get_template)
        self.lookup = None

    def get_template(self, name):
        """
        Returns Cheetah's template by name.
        """
        return PageTemplate(self.base_dir, name)

init:

        # Template engine tool
        from aquapi.web.tools.template import CheetahTool
        cherrypy.tools.render = CheetahTool()

        # Tool to load the logged in user or redirect
        # the client to the login page
        from aquapi.web.tools.user import UserTool
        cherrypy.tools.user = UserTool()


        from aquapi.web.controllers import RootController 
        webapp = RootController()

        # Let's mount the application so that CherryPy can serve it
        app = cherrypy.tree.mount(webapp, '/', os.path.join(self.base_dir, "app.cfg"))

        # Template engine plugin
        from aquapi.web.plugin.template import CheetahTemplatePlugin
        engine.cheetah = CheetahTemplatePlugin(engine, 
                                        os.path.join(self.base_dir, 'aquapi/web/templates'),
                                        os.path.join(self.base_dir, 'cache'))
        engine.cheetah.subscribe()

回答1:


In general, to me it's some sort of over-engineering happened in your snippets. CherryPy plugins are usually used for a system task (e.g. put PID-file on engine start, remove it on stop) or for an asynchronous task (e.g. sending email in separate thread). Template rendering happens clearly synchronously to the request handling, so I don't see the point of extracting this logic out of CherryPy tool. There's a class in CherryPy, cherrypy._cptools.HandlerWrapperTool, which demonstrate the suggested approach to wrapping handler return values.

I haven't ever used Cheetah, so my example is Jinja2-based. You will just have to change the templating engine instance (to Cheetah) and correct its calls. The rest is the same.

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import types

import cherrypy
import jinja2


path   = os.path.abspath(os.path.dirname(__file__))
config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 4
  }
}


class TemplateTool(cherrypy.Tool):

  _engine = None
  '''Jinja environment instance'''


  def __init__(self):
    viewLoader   = jinja2.FileSystemLoader(os.path.join(path, 'view'))
    self._engine = jinja2.Environment(loader = viewLoader)

    cherrypy.Tool.__init__(self, 'before_handler', self.render)

  def __call__(self, *args, **kwargs):
    if args and isinstance(args[0], (types.FunctionType, types.MethodType)):
      # @template
      args[0].exposed = True
      return cherrypy.Tool.__call__(self, **kwargs)(args[0])
    else:
      # @template()
      def wrap(f):
        f.exposed = True
        return cherrypy.Tool.__call__(self, *args, **kwargs)(f)
      return wrap

  def render(self, name = None):
    cherrypy.request.config['template'] = name

    handler = cherrypy.serving.request.handler
    def wrap(*args, **kwargs):
      return self._render(handler, *args, **kwargs)
    cherrypy.serving.request.handler = wrap

  def _render(self, handler, *args, **kwargs):
    template = cherrypy.request.config['template']
    if not template:
      parts = []
      if hasattr(handler.callable, '__self__'):
        parts.append(handler.callable.__self__.__class__.__name__.lower())
      if hasattr(handler.callable, '__name__'):
        parts.append(handler.callable.__name__.lower())
      template = u'/'.join(parts)

    data     = handler(*args, **kwargs) or {}
    renderer = self._engine.get_template(u'{0}.html'.format(template))

    return renderer.render(**data)


cherrypy.tools.template = TemplateTool()


class App:

  @cherrypy.expose
  def index(self):
    '''No renderer applied, CherryPy outputs dict keys'''
    return {'user': 123}

  @cherrypy.tools.template
  def auto(self):
    return {'user': 123}

  @cherrypy.tools.template(name = 'app/auto')
  def manual(self):
    return {'user': 234}


if __name__ == '__main__':
  cherrypy.quickstart(App(), '/', config)

Along the python file, create directory view/app and put the following in file named auto.html there.

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=utf-8' />
    <title>Test</title>
  </head>
  <body>
    <p>User: <em>{{ user }}</em></p>
  </body>
</html>

Some notes on the TemplateTool. First, you can use it as a decorator in two ways: not making a call, and making a call with template name argument. You can use the tool as any other CherryPy tool in the configuration (e.g. make all controller methods to render templates). Second, following convention-over-configuration principle, the tool when not provided with template name will use classname/methodname.html. Third, the decorator exposes the method, so you don't need to add @cherrypy.expose on top.



来源:https://stackoverflow.com/questions/24578794/cherrypy-with-cheetah-as-plugin-tool-blank-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!