|
Jeff Hinrichs - DM&T
jeffh at dundeemt.com
Sun May 6 01:06:47 EDT 2007
Graham,
Thanks for the feedback. I reverted back to the method you wrote
about. I had that originally but I was trying to tidy up a lonely
looking function. Not that big of a deal. Thanks again.
Hey, -- while we are chattting, have you ever tried putting a
decorator around a vampire.pathArgs wrapped class method? i..e
def mysession(fn):
def _setup(req, *args, **kwargs):
#if session isn't part of the req object, add it
if not hasattr(req,"session"):
req.session = Session.Session(req)
#register session.save as a cleanup
req.register_cleanup(_savesession, req)
#now call the original
return fn(req,*args,**kwargs)
return _setup
class Foo:
def __init__(self):
self.bar = vampire.PathArgs(self._bar)
@mysession
def _bar(self, req, id):
... do something..
@mysession
def baz(req):
...do something...
Using the decorator on the global works just fine, but using the
decorator on the _bar method of the Foo class gives back some very
curious errors -- appears to be confusing the heck out of things.
Complains about Foo not having an "options" -- feels like the vampire
wrapper and the decorator are intermingling in an unhappy way.
I am quite new to making my own decorators so I could very well be
doing something quite idiotic ;)
I don't require the use of decorators as I can replace them with a
different idiom that is almost as effective. using mysession as an
example, I rip out the fancy wrapper then make it into a normal bit.
def mysession(req):
#if session isn't part of the req object, add it
if not hasattr(req,"session"):
req.session = Session.Session(req)
#register session.save as a cleanup
req.register_cleanup(_savesession, req)
Then I just:
class Foo:
def __init__(self):
self.bar = vampire.PathArgs(self._bar)
def _bar(self, req, id):
mysession(req)
... do something..
Amount of code is about the same, no functionality given up in this
instance -- so the question about the decorator is out of curiosity
not that my app won't work without them. The same could be done with
@require_admin v. require_admin(req), kind of an anti-decorator
pattern.<g>
Thanks for all of your help.
-Jeff
http://www.OmahaPython.org
On 5/5/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> A cleanup function will always be passed one argument. This will be
> the data which is supplied when register_cleanup() is called. If no
> cleanup data is supplied, then None will be passed as cleanup data.
> Because Session object save() method already has a 'self' argument,
> the cleanup data becomes the second argument it complains about.
>
> In short, do this:
>
> def _cleanup(session):
> session.save()
>
> req.register_cleanup(_cleanup, req.session)
>
> Graham
>
> On 06/05/07, Jeff Hinrichs - DM&T <jeffh at dundeemt.com> wrote:
> > I know that I am missing something here but googling and banging my
> > head aren't working so I'll ask.
> >
> > I'm trying to register the session.save method from the Session I've
> > attached to the req object in the req.register_cleanup() like this:
> >
> > # housekeeping setup for a request
> >
> > def housekeeping(fn):
> >
> > def _setup(req, *args, **kwargs):
> >
> > #if session isn't part of the req object, add it
> >
> > if not hasattr(req,"session"):
> >
> > req.session = Session.Session(req)
> >
> > req.session['housekeeped']=1
> >
> > #register session.save as a cleanup
> >
> > req.register_cleanup(req.session.save)
> >
> > #now call the original
> >
> > return fn(req,*args,**kwargs)
> >
> > return _setup
> >
> >
> > @housekeeping
> > def login(req):
> >
> > req.session['foo']='bar'
> >
> > return "We are in login() %s" % req.session.keys()
> >
> > Everything seems peachy, the results returned are:
> >
> > We are in login() ['foo', 'housekeeped']
> >
> > However, the register_cleanup fails to save the session, and is
> > returning the following err:
> >
> > [notice] vampire: Reimporting module
> > '/usr/home/apache22/lakota.dundeemt.pri/dora/index.py'
> > [error] [client 192.168.2.52] python_cleanup: Error calling cleanup
> > object <bound method DbmSession.save of {'housekeeped': 1}>
> > [error] [client 192.168.2.52] exceptions.TypeError: save() takes
> > exactly 1 argument (2 given)
> >
> > I've tried changing the register_cleanup to call req.session.save()
> > (as opposed to .save) but that complains, correctly, that save() is
> > not callable.
> >
> > First question, can I register multiple items with multiple calls to
> > req.register_cleanup? i.e.
> > req.register_cleanup(_closedb, req)
> > req.register_cleanup(req.session.save)
> >
> > or is that my problem? Or have I just been at the keyboard too long
> > to see the error that probably is staring me in the face? The db
> > cleanup appears to be working without error nor phantom connections.
> >
> > Thanks,
> >
> > Jeff Hinrichs
> > http://www.OmahaPython.org/
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >
>
--
Jeff Hinrichs
Dundee Media & Technology, Inc
jeffh at dundeemt.com
402.320.0821
|