I am attempting to define my on custom Mako loader in my cherrpy server via the following in my server.py
:
from my.path.templating import MakoLoader
from HandleEvent import HandleEvent
cherrypy.tools.mako = cherrypy.Tool('on_start_resource', MakoLoader(os.path.join(os.path.dirname(__file__), 'templates')))
root = HandleEvent()
conf = { '/' : { 'request.dispatch' : cherrypy.dispatch.MethodDispatcher()}}
app = cherrypy.tree.mount(root, '/', conf)
I then try to use the custom mako loader as a decorator on my HandleEvent
class as shown:
class HandleEvent(object):
exposed = True
@cherrypy.tools.mako(template="template.html")
def GET(self, **urlParams):
return 'It worked!'
However when I try to start the server I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/cherryd", line 5, in <module>
pkg_resources.run_script('CherryPy==3.1.2', 'cherryd')
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 461, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1194, in run_script
execfile(script_filename, namespace, namespace)
File "/usr/local/lib/python2.6/dist-packages/CherryPy-3.1.2-py2.6.egg/EGG-INFO/scripts/cherryd", line 95, in <module>
options.imports)
File "/usr/local/lib/python2.6/dist-packages/CherryPy-3.1.2-py2.6.egg/EGG-INFO/scripts/cherryd", line 15, in start
exec "import %s" % i
File "<string>", line 1, in <module>
File "/var/my/path/server.py", line 4, in <module>
from my.path.HandleEvent import http_methods_allowed
File "/var/my/path/HandleEvent.py", line 63, in <module>
class handleEvent(object):
File "/var/my/path/HandleEvent.py", line 76, in HandleEvent
@cherrypy.tools.mako(template="template.html")
AttributeError: 'Toolbox' object has no attribute 'mako'
I am not sure why this happening as I have defined this custom loader in this manner before. Any insights would be much appreciated.
My import of my HandleEvent
Class needed to come after I assigned the custom Mako loader to cherrypy.tools.mako
. So the correct code is as follows:
from my.path.templating import MakoLoader
cherrypy.tools.mako = cherrypy.Tool('on_start_resource', MakoLoader(os.path.join(os.path.dirname(__file__), 'templates')))
# The import should come here
from HandleEvent import HandleEvent
root = HandleEvent()
conf = { '/' : { 'request.dispatch' : cherrypy.dispatch.MethodDispatcher()}}
app = cherrypy.tree.mount(root, '/', conf)