iro/model/dbdefer.py
branchdevel
changeset 258 0a5eb5aac0be
parent 155 ff1edf7c1329
child 294 0e75bd39767d
equal deleted inserted replaced
257:31114e40178d 258:0a5eb5aac0be
     5 from .utils import WithSession
     5 from .utils import WithSession
     6 
     6 
     7 import inspect
     7 import inspect
     8 
     8 
     9 class DBDefer(object):
     9 class DBDefer(object):
    10     '''a twisted sqlalchemy connector this Decorator adds a session parameter, with a valid session connection'''
    10     '''a twisted sqlalchemy connector.
       
    11     
       
    12     This is used as a Decorator class.
       
    13     It adds a session parameter to the function with a valid session.
       
    14     If the session parmaeter is used in calling this function only calls a commit after running the function, instead of createing a new session.'''
    11     def __init__(self, engine, autocommit=False):
    15     def __init__(self, engine, autocommit=False):
       
    16         """
       
    17         :param `sqlalchemy.engine.base.Engine` engine: a valid sqlalchemy engine object (normally created via :func:`sqlalchemy.create_engine`).
       
    18         :param boolean autocommit: autocommit after running the function.
       
    19         """
    12         self.autocommit=autocommit
    20         self.autocommit=autocommit
    13         self.engine = engine
    21         self.engine = engine
    14 
    22 
    15     def __call__(self, func):
    23     def __call__(self, func):
    16         @runInDBPool
    24         @runInDBPool
    51               evaldict, defaults=defaults, undecorated=caller, __wrapped__=caller,
    59               evaldict, defaults=defaults, undecorated=caller, __wrapped__=caller,
    52               doc=caller.__doc__, module=caller.__module__, addsource=True)
    60               doc=caller.__doc__, module=caller.__module__, addsource=True)
    53         return wrap
    61         return wrap
    54 
    62 
    55 dbdefer=DBDefer(None)
    63 dbdefer=DBDefer(None)
       
    64 """the decorator to use. Use :func:`setEngine` to set a valid engine after program has started."""
    56 
    65 
    57 def setEngine(engine,autocommit=False): 
    66 def setEngine(engine,autocommit=False): 
       
    67     """set the engine and autocommit for the decorator (see :class:`DBDefer`)."""
    58     dbdefer.engine = engine
    68     dbdefer.engine = engine
    59     dbdefer.autocommit = autocommit
    69     dbdefer.autocommit = autocommit
    60 
    70 
    61 
    71