|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 from decorator import FunctionMaker |
|
23 import sqlalchemy |
|
24 |
|
25 from .pool import runInDBPool |
|
26 from .utils import WithSession |
|
27 |
|
28 import inspect |
|
29 |
|
30 class DBDefer(object): |
|
31 '''a twisted sqlalchemy connector. |
|
32 |
|
33 This is used as a Decorator class. |
|
34 It adds a session parameter to the function with a valid session. |
|
35 If the session parmaeter is used in calling this function only calls a commit after running the function, instead of createing a new session.''' |
|
36 def __init__(self, engine, autocommit=False): |
|
37 """ |
|
38 :param `sqlalchemy.engine.base.Engine` engine: a valid sqlalchemy engine object (normally created via :func:`sqlalchemy.create_engine`). |
|
39 :param boolean autocommit: autocommit after running the function. |
|
40 """ |
|
41 self.autocommit=autocommit |
|
42 self.engine = engine |
|
43 |
|
44 def __call__(self, func): |
|
45 @runInDBPool |
|
46 def wrapper(func,*a, **kw): |
|
47 i = argspec.args.index("session") |
|
48 ab = a[:i] |
|
49 ae = a[i:-1] |
|
50 if isinstance(a[-1],sqlalchemy.orm.session.Session): |
|
51 al = ab + (a[-1],) + ae |
|
52 ret = func(*al, **kw) |
|
53 if self.autocommit: |
|
54 a[-1].commit() |
|
55 return ret |
|
56 else: |
|
57 with WithSession(self.engine, self.autocommit) as session: |
|
58 al = ab + (session,) + ae |
|
59 return func(*al, **kw) |
|
60 |
|
61 caller=func |
|
62 argspec = inspect.getargspec(caller) |
|
63 args =[i for i in argspec.args if i != "session" ] |
|
64 sargs=", ".join(args) |
|
65 if sargs: |
|
66 sargs+=", session" |
|
67 else: |
|
68 sargs="session" |
|
69 defaults = argspec.defaults |
|
70 if not defaults: |
|
71 defaults = (None,) |
|
72 else: |
|
73 defaults += (None,) |
|
74 evaldict = caller.func_globals.copy() |
|
75 evaldict['_call_'] = func |
|
76 evaldict['decorator'] = wrapper |
|
77 wrap = FunctionMaker.create( |
|
78 '%s(%s)' % (caller.__name__, sargs), |
|
79 'return decorator(_call_, %s)' % sargs, |
|
80 evaldict, defaults=defaults, undecorated=caller, __wrapped__=caller, |
|
81 doc=caller.__doc__, module=caller.__module__, addsource=True) |
|
82 return wrap |
|
83 |
|
84 dbdefer=DBDefer(None) |
|
85 """the decorator to use. Use :func:`setEngine` to set a valid engine after program has started.""" |
|
86 |
|
87 def setEngine(engine,autocommit=False): |
|
88 """set the engine and autocommit for the decorator (see :class:`DBDefer`).""" |
|
89 dbdefer.engine = engine |
|
90 dbdefer.autocommit = autocommit |
|
91 |
|
92 |