iro/model/dbdefer.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 29 Mar 2012 18:27:15 +0200
branchdevel
changeset 263 52284710c0b4
parent 258 0a5eb5aac0be
child 294 0e75bd39767d
permissions -rw-r--r--
iro.model: adding docstrings

from decorator import FunctionMaker
import sqlalchemy

from .pool import runInDBPool
from .utils import WithSession

import inspect

class DBDefer(object):
    '''a twisted sqlalchemy connector.
    
    This is used as a Decorator class.
    It adds a session parameter to the function with a valid session.
    If the session parmaeter is used in calling this function only calls a commit after running the function, instead of createing a new session.'''
    def __init__(self, engine, autocommit=False):
        """
        :param `sqlalchemy.engine.base.Engine` engine: a valid sqlalchemy engine object (normally created via :func:`sqlalchemy.create_engine`).
        :param boolean autocommit: autocommit after running the function.
        """
        self.autocommit=autocommit
        self.engine = engine

    def __call__(self, func):
        @runInDBPool
        def wrapper(func,*a, **kw):
            i = argspec.args.index("session")
            ab = a[:i]
            ae = a[i:-1]
            if isinstance(a[-1],sqlalchemy.orm.session.Session):
                al = ab + (a[-1],) + ae
                ret = func(*al, **kw)
                if self.autocommit:
                    a[-1].commit()
                return ret
            else:
                with WithSession(self.engine, self.autocommit) as session:
                    al = ab + (session,) + ae
                    return func(*al, **kw)

        caller=func
        argspec = inspect.getargspec(caller)
        args =[i for i in argspec.args if i != "session" ]
        sargs=", ".join(args)       
        if sargs:
            sargs+=", session"
        else:
            sargs="session"
        defaults = argspec.defaults
        if not defaults:
            defaults = (None,)
        else:
            defaults += (None,)
        evaldict = caller.func_globals.copy()
        evaldict['_call_'] = func
        evaldict['decorator'] = wrapper
        wrap = FunctionMaker.create(
              '%s(%s)' % (caller.__name__, sargs), 
              'return decorator(_call_, %s)' % sargs,
              evaldict, defaults=defaults, undecorated=caller, __wrapped__=caller,
              doc=caller.__doc__, module=caller.__module__, addsource=True)
        return wrap

dbdefer=DBDefer(None)
"""the decorator to use. Use :func:`setEngine` to set a valid engine after program has started."""

def setEngine(engine,autocommit=False): 
    """set the engine and autocommit for the decorator (see :class:`DBDefer`)."""
    dbdefer.engine = engine
    dbdefer.autocommit = autocommit