From: Chris Lamb Date: Tue, 27 Oct 2009 09:19:00 +0000 (+0000) Subject: Attach commit_or_flush to session object to call either .commit() or .flush() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=282d1ce6f56cb8ec1f37b6d9e5c2b9b856141a0e;p=dak Attach commit_or_flush to session object to call either .commit() or .flush() Signed-off-by: Chris Lamb --- diff --git a/daklib/dbconn.py b/daklib/dbconn.py index fb322aea..ca90ba88 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -63,23 +63,38 @@ def session_wrapper(fn): Wrapper around common ".., session=None):" handling. If the wrapped function is called without passing 'session', we create a local one and destroy it when the function ends. + + Also attaches a commit_or_flush method to the session; if we created a + local session, this is a synonym for session.commit(), otherwise it is a + synonym for session.flush(). """ def wrapped(*args, **kwargs): private_transaction = False - session = kwargs.get('session') - # No session specified as last argument or in kwargs, create one. - if session is None and len(args) <= len(getargspec(fn)[0]) - 1: - private_transaction = True - kwargs['session'] = DBConn().session() + # Find the session object + try: + session = kwargs['session'] + except KeyError: + if len(args) <= len(getargspec(fn)[0]) - 1: + # No session specified as last argument or in kwargs + private_transaction = True + session = kwargs['session'] = DBConn().session() + else: + # Session is last argument in args + session = args[-1] + + if private_transaction: + session.commit_or_flush = session.commit + else: + session.commit_or_flush = session.flush try: return fn(*args, **kwargs) finally: if private_transaction: # We created a session; close it. - kwargs['session'].close() + session.close() wrapped.__doc__ = fn.__doc__ wrapped.func_name = fn.func_name