routesdispatcher in cherrypy

732 views
Skip to first unread message

Nightfury

unread,
Aug 1, 2012, 6:58:04 AM8/1/12
to cherryp...@googlegroups.com
 i tried the RoutesDispatcher example from http://appmecha.wordpress.com/2008/10/27/cherrypy-gae-routing-2/  but when i runned the program it thorws the following error:


404 Not Found

The path '/' was not found.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/CherryPy-3.1.2-py2.7.egg/cherrypy/_cprequest.py", line 606, in respond
    cherrypy.response.body = self.handler()
  File "/usr/local/lib/python2.7/site-packages/CherryPy-3.1.2-py2.7.egg/cherrypy/_cperror.py", line 227, in __call__
    raise self
NotFound: (404, "The path '/' was not found.")



this is the code:



# main.py
import cherrypy
import wsgiref.handlers

class HelloWorld:
  @cherrypy.expose
  def index(self):
    return "Hello world!"
class GoodbyeWorld:
  @cherrypy.expose
  def index(self,num=None):
    return "Goodbye World!"

def main():
  hw = HelloWorld()
  gw = GoodbyeWorld()
  mapper = cherrypy.dispatch.RoutesDispatcher()
  mapper.connect('home',"/",controller=hw)
  mapper.connect('hello','/hello',controller=hw)
  mapper.connect('goodbye','/goodbye',controller=gw)
  app = cherrypy.tree.mount(None,config={"/":{"request.dispatch": mapper}})
  wsgiref.handlers.CGIHandler().run(app)

if __name__ == '__main__':
  main()
 someone please help. what am i missing..i want to know ,where i can find more examples of routesdispatcher in cherrypy.

Sylvain Hellegouarch

unread,
Aug 1, 2012, 9:30:48 AM8/1/12
to cherryp...@googlegroups.com

 someone please help. what am i missing..i want to know ,where i can find more examples of routesdispatcher in cherrypy.

When all is dark, the CherryPy unit test suite shows the light (well sometimes a flickering one but still).

Joel Rivera

unread,
Aug 1, 2012, 1:37:42 PM8/1/12
to cherryp...@googlegroups.com
Ok this is what I found:

The packages that handler the mapping is routes[1], CherryPy just
provides a convenient dispatcher, in between versions of the package
they broke the implicit api of CherryPy with routes, because of the new
default of the Mapper object, as this [2] changelog express in the last
point.

As for your example in CherryPy 3.1.X; should work if you specify the
"action" parameter to each call of the mapper, like:

'mapper.connect('home',"/", controller=hw, action='index')'

If you update to CherryPy 3.2, you can gain advantage of a new implicit
behavior of the dispatcher, if the controller is a "class" [3] (like in
your example), call the object [HelloWorld.__call__] , so you could
define the HelloWorld class like this:

class HelloWorld:

def index(self):
return "Hello world!"

__call__ = index

of just:

class HelloWorld:

def __call__(self):
return "Hello world!"

Or just stick to the explicitness of "action='index'".


The other possibility is to modify cherrypy._cpdispatch [4] to :

self.mapper = routes.Mapper(explicit=False)

and you example should work just fine, notice that the
`@cherrypy.expose` is unnecessary.

By the way the `explicit` argument is explained here [5].

I think that solves the mystery.

Cheers.


[1] http://routes.readthedocs.org/en/latest/index.html
[2]
https://github.com/bbangert/routes/blob/master/CHANGELOG.rst#release-112-february-28-2010
[3]
https://bitbucket.org/cherrypy/cherrypy/src/b0c48210b250/cherrypy/_cpdispatch.py#cl-541
[4]
https://bitbucket.org/cherrypy/cherrypy/src/b0c48210b250/cherrypy/_cpdispatch.py#cl-466
[5]
http://routes.readthedocs.org/en/latest/modules/mapper.html?highlight=explicit#routes.mapper.Mapper
> --
> You received this message because you are subscribed to the Google
> Groups "cherrypy-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/cherrypy-users/-/g7MI3l6Vue0J.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to cherrypy-users
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cherrypy-users?hl=en.

--
Rivera²

David Bolen

unread,
Aug 1, 2012, 6:17:42 PM8/1/12
to cherryp...@googlegroups.com
Joel Rivera <riv...@joel.mx> writes:

> The other possibility is to modify cherrypy._cpdispatch [4] to :
>
> self.mapper = routes.Mapper(explicit=False)
>
> and you example should work just fine, notice that the
> `@cherrypy.expose` is unnecessary.

Or just set explicit on the mapper at the higher level, so no need to
tweak cherrypy itself. For example, my application setup does:

# Establish application request dispatching
d = cherrypy.dispatch.RoutesDispatcher()
d.mapper.minimization = True
d.mapper.explicit = False

> By the way the `explicit` argument is explained here [5].
>
> I think that solves the mystery.

Most likely. Note that explicit=False was the default behavior in
older versions of Routes, which probably explains the discrepancy with
the older book - the code would have worked at the point the book was
written.

I also show the minimization setting above, which the OP may need to
consider when working with more elaborate older examples (though I
don't think it would be an issue with the posted code). Minimization
was also the default behavior in older Routes releases.

I used the above code to maintain compatibility with my existing routes
definitions during the last upgrade to the Routes package.

-- David

Nightfury

unread,
Aug 3, 2012, 12:41:00 AM8/3/12
to cherryp...@googlegroups.com

 
thankyou Joel Rivera  your reply really helped. i added action ='index' to my code and it worked.
this is my changed code:


import cherrypy
import wsgiref.handlers

class HelloWorld:
    @cherrypy.expose
    def index(self):
        return "Hello world!"
class GoodbyeWorld:
    @cherrypy.expose
    def index(self,num=None):
        return "Goodbye World!"

def main():
    hw = HelloWorld()
    gw = GoodbyeWorld()
    mapper = cherrypy.dispatch.RoutesDispatcher()
    mapper.connect('home','/',controller=hw,action='index')
    mapper.connect('hello','/hello',controller=hw,action='index')
    mapper.connect('goodbye','/goodbye',controller=gw,action='index')
    conf = {'/': {'request.dispatch': mapper}}
    app=cherrypy.tree.mount(root=None,config=conf)
    cherrypy.quickstart(app)

if __name__ == '__main__':
    cherrypy.quickstart(main())

thankyou cherrypy team

Reply all
Reply to author
Forward
0 new messages