Re: Re: Trying to build a fully synthesized site
"Joseph S. Tate" <[email protected]> Sat, 29 Oct 2016 04:18:44 +0000
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <CA+WDQbGv8gJgB2KWz0S23LtQaAAcFj1sNUMoYZ87Ti03vDfE5Q@mail.gmail.com> |
On Fri, Oct 28, 2016 at 8:44 PM <[email protected]> wrote: > Here's more info and clearer questions: > I have a config that loads a set of pages and tells which variables need > to be extracted from the memory state to satisfy the query. > > Here's what I am trying to do: > Build up something that knows which pages are defined and send 404s for > things not defined. If cherrypy can do that, it would be great... > have a generic class/function that all the defines queries get funneled to: > That wants to get instance of the page description class that was > loaded from the config > it looks at the page that was requested, gets the list of data that was > desired and builds up the response json > if there was a jinja template specified, feed the data into the > template ann capture the rendered output > send the response back > How do you define the pages? How do your urls map to those pages? If you can describe this, you can create a dispatcher. If you want a CMS, you should really just use one. If you want to serve files, just use the static file tool/handler. If you want to run code based on static files, maybe you should start with the static file tools and modify them to fit your needs. > > Here are some specific questions: > can I build the dispatcher at runtime? > You build it at server start time, not at request time. is it worth doing this or should I generate the 404s from my > class/function? > raise cherrypy.NotFound, but the default dispatcher already does this for any url not mapped to a handler. > if I don't do a dispatcher, how do I say that everything below a point > on the path is handled by the class/function? > cherrypy.tree.mount() You should read the documentation for this anyway. cherrypy.quickstart is just the beginning, and anything more complicated than a single class root will need this. http://docs.cherrypy.org/en/latest/basics.html#multiple-applications As Tim said, that's handled for you in the default dispatcher. If the dispatcher walks a tree and finds a callable, it passes everything left in the url (query params as kwargs, path segments as positional args) to that method. Here's an example: import cherrypy class Root(): @cherrypy.expose def foo(s, *a, **kw): return cherrypy.request.path_info + str(a) + str(kw) cherrypy.tree.mount(Root(), '/absalom', {}) cherrypy.engine.start() cherrypy.engine.block() ### Requesting http://localhost:8080/absalom/foo/bar/baz?a=arg&b=blarg returns ### /foo/bar/baz('bar', 'baz'){'a': u'arg', 'b': u'blarg'} How do I pass the instance of the page description class and other things > into class/function? > Classes are instantiated at start time and kept in RAM in thread-unsafe memory. Pass anything you want into __init__() method for the class before mounting or quickstarting. Otherwise, just define everything in the cherrypy ini file and keep everything in cherrypy.config. You can also store information in the request object by injecting it with tools at various injection points, or on the server engine using engine plugins, but I doubt you'll need to do that. > I would assume a class is easier since it then can have private storage. > I am thinking right about this? > yes. -- You received this message because you are subscribed to the Google Groups "cherrypy-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected] Visit this group at https://groups.google.com/group/cherrypy-users. For more options, visit https://groups.google.com/d/optout.