3
3
4
4
"""
5
5
6
- from ast import Num
7
- from typing import Any , Callable , Sequence , cast , Dict , Generator , Generic , Iterator
8
- from typing import List , NamedTuple , Optional , Type , TypeVar , Tuple , Union
9
- from typing import overload
6
+ from typing import Any , Callable , Dict , Iterator , List , Optional , Union
7
+
10
8
from .pep249 import Connection , Cursor , Error
11
9
10
+
12
11
class HostInfo :
13
12
url : str
14
13
port : int
15
14
15
+
16
16
class Properties (Dict [str , str ]):
17
17
...
18
18
19
+
19
20
class Plugin :
20
21
21
- _num : int = 0 ;
22
+ _num : int = 0
22
23
23
24
def __init__ (self , num : int ):
24
25
self ._num = num
25
26
26
- def connect (self , hostInfo : HostInfo , props : Properties , initial : bool , executeFunc : Callable ) -> Any :
27
- #print("Plugin {}: connect before".format(self._num))
28
- result = executeFunc ();
29
- #print("Plugin {}: connect after".format(self._num))
27
+ def connect (self , hostInfo : HostInfo , props : Properties ,
28
+ initial : bool , executeFunc : Callable ) -> Any :
29
+ # print("Plugin {}: connect before".format(self._num))
30
+ result = executeFunc ()
31
+ # print("Plugin {}: connect after".format(self._num))
30
32
return result
31
33
32
34
def execute (self , executeFunc : Callable ) -> Any :
33
- #print("Plugin {}: execute before".format(self._num))
34
- result = executeFunc ();
35
- #print("Plugin {}: execute after".format(self._num))
35
+ # print("Plugin {}: execute before".format(self._num))
36
+ result = executeFunc ()
37
+ # print("Plugin {}: execute after".format(self._num))
36
38
return result
37
39
38
40
def subscribed (self , methodName : str ) -> bool :
39
41
return True
40
42
43
+
41
44
class PluginManager :
42
45
43
46
_plugins : List [Plugin ] = []
44
- _numOfPlugins = 0
45
- _functionCache : bool = True
46
- _executeFunc : Callable = None
47
- _connectFunc : Callable = None
47
+ _executeFunc : Optional [Callable ] = None
48
+ _connectFunc : Optional [Callable ] = None
48
49
49
- def __init__ (self , props : Properties = Properties (), numOfPlugins : int = 10 , functionCache : bool = True ):
50
+ def __init__ (self , props : Properties = Properties (),
51
+ numOfPlugins : int = 10 , functionCache : bool = True ):
50
52
51
53
self ._numOfPlugins = numOfPlugins
52
54
self ._functionCache = functionCache
@@ -59,7 +61,7 @@ def __init__(self, props: Properties = Properties(), numOfPlugins: int = 10, fun
59
61
60
62
@property
61
63
def numOfPlugins (self ) -> int :
62
- return self ._numOfPlugins ;
64
+ return self ._numOfPlugins
63
65
64
66
@property
65
67
def executeFunc (self ) -> Callable :
@@ -75,11 +77,13 @@ def executeFunc(self) -> Callable:
75
77
76
78
for i in range (numOfPlugins - 1 , 0 , - 1 ):
77
79
code = "plugins[{}].execute(lambda : {})" .format (i - 1 , code )
78
-
80
+
79
81
code = "lambda x : {}" .format (code )
80
- #print(code)
81
- self ._executeFunc = eval (code , { "plugins" : self ._plugins })
82
+ # print(code)
83
+
84
+ self ._executeFunc = eval (code , {"plugins" : self ._plugins })
82
85
86
+ assert (self ._executeFunc is not None )
83
87
return self ._executeFunc
84
88
85
89
@property
@@ -96,92 +100,92 @@ def connectFunc(self) -> Callable:
96
100
97
101
for i in range (numOfPlugins - 1 , 0 , - 1 ):
98
102
code = "plugins[{}].connect(hostInfo, props, initial, lambda : {})" .format (i - 1 , code )
99
-
103
+
100
104
code = "lambda hostInfo, props, initial, x : {}" .format (code )
101
- #print(code)
102
- self ._connectFunc = eval (code , { "plugins" : self ._plugins })
105
+ # print(code)
106
+ self ._connectFunc = eval (code , {"plugins" : self ._plugins })
103
107
108
+ assert (self ._connectFunc is not None )
104
109
return self ._connectFunc
105
110
106
111
107
112
class AwsWrapperConnection (Connection ):
108
113
109
114
__module__ = "pawswrapper"
110
-
111
- _targetConn : Connection = None
112
- _pluginManager : PluginManager = None
115
+
116
+ def __init__ (self , pluginManager : PluginManager , targetConn : Connection ):
117
+ self ._targetConn = targetConn
118
+ self ._pluginManager = pluginManager
113
119
114
120
@staticmethod
115
121
def connect (
116
122
conninfo : str = "" ,
117
- target : Union [str , callable ] = None ,
123
+ target : Union [None , str , Callable ] = None ,
118
124
** kwargs : Union [None , int , str ]
119
125
) -> "AwsWrapperConnection" :
120
126
# Check if target provided
121
127
if not target :
122
128
raise Error ("Target driver is required" )
123
129
124
130
if type (target ) == str :
125
- target = eval (target );
131
+ target = eval (target )
126
132
127
133
if not callable (target ):
128
134
raise Error ("Target driver should be a target driver's connect() method/function" )
129
135
130
136
numOfPlugins = kwargs .get ("numOfPlugins" )
131
- if numOfPlugins == None :
132
- numOfPlugins = 10
133
- else :
137
+ if numOfPlugins is not None :
134
138
kwargs .pop ("numOfPlugins" )
139
+ if type (numOfPlugins ) != int :
140
+ numOfPlugins = 10
141
+
135
142
functionCache = kwargs .get ("functionCache" )
136
- if functionCache == None :
143
+ if functionCache is None :
137
144
functionCache = True
138
145
else :
139
146
kwargs .pop ("functionCache" )
147
+ functionCache = bool (functionCache )
140
148
141
149
props : Properties = Properties ()
142
150
pluginManager : PluginManager = PluginManager (props = props , numOfPlugins = numOfPlugins , functionCache = functionCache )
143
151
hostInfo : HostInfo = HostInfo ()
144
152
145
-
146
153
# Target driver is a connect function
147
154
if pluginManager .numOfPlugins == 0 :
148
155
conn = target (conninfo , ** kwargs )
149
156
else :
150
- conn = pluginManager .connectFunc (hostInfo , props , True , lambda : target (conninfo , ** kwargs ))
151
-
152
- return AwsWrapperConnection (pluginManager , conn );
157
+ conn = pluginManager .connectFunc (hostInfo , props , True , lambda : target (conninfo , ** kwargs ))
153
158
154
- def __init__ (self , pluginManager : PluginManager , targetConn : Connection ):
155
- self ._targetConn = targetConn
156
- self ._pluginManager = pluginManager
159
+ return AwsWrapperConnection (pluginManager , conn )
157
160
158
161
def close (self ) -> None :
159
- self ._targetConn .close ();
162
+ if self ._targetConn :
163
+ self ._targetConn .close ()
160
164
161
165
def cursor (self , ** kwargs ) -> "AwsWrapperCursor" :
162
166
_cursor = self ._targetConn .cursor (** kwargs )
163
- return AwsWrapperCursor (self , self ._pluginManager , _cursor );
167
+ return AwsWrapperCursor (self , self ._pluginManager , _cursor )
164
168
165
169
def commit (self ) -> None :
166
- self ._targetConn .commit ();
170
+ self ._targetConn .commit ()
167
171
168
172
def rollback (self ) -> None :
169
- self ._targetConn .rollback ();
173
+ self ._targetConn .rollback ()
170
174
171
175
def tpc_begin (self , xid : Any ) -> None :
172
- self ._targetConn .tpc_begin (xid );
176
+ self ._targetConn .tpc_begin (xid )
173
177
174
178
def tpc_prepare (self ) -> None :
175
- self ._targetConn .tpc_prepare ();
179
+ self ._targetConn .tpc_prepare ()
176
180
177
181
def tpc_commit (self , xid : Any = None ) -> None :
178
- self ._targetConn .tpc_commit (xid );
182
+ self ._targetConn .tpc_commit (xid )
179
183
180
184
def tpc_rollback (self , xid : Any = None ) -> None :
181
- self ._targetConn .tpc_rollback (xid );
185
+ self ._targetConn .tpc_rollback (xid )
182
186
183
187
def tpc_recover (self ) -> Any :
184
- return self ._targetConn .tpc_recover ();
188
+ return self ._targetConn .tpc_recover ()
185
189
186
190
def __enter__ (self : "AwsWrapperConnection" ) -> "AwsWrapperConnection" :
187
191
return self
@@ -191,7 +195,6 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
191
195
self ._targetConn .close ()
192
196
193
197
194
-
195
198
class AwsWrapperCursor (Cursor ):
196
199
197
200
__module__ = "pawswrapper"
@@ -208,35 +211,36 @@ def __init__(self, conn: AwsWrapperConnection, pluginManager: PluginManager, tar
208
211
# It's not part of PEP249
209
212
@property
210
213
def connection (self ) -> AwsWrapperConnection :
211
- return self ._conn ;
214
+ return self ._conn
212
215
213
216
@property
214
217
def description (self ):
215
- return self ._targetCursor .description ;
218
+ return self ._targetCursor .description
216
219
217
220
@property
218
221
def rowcount (self ) -> int :
219
- return self ._targetCursor .rowcount ;
222
+ return self ._targetCursor .rowcount
220
223
221
224
@property
222
225
def arraysize (self ) -> int :
223
- return self ._targetCursor .arraysize ;
226
+ return self ._targetCursor .arraysize
224
227
225
228
def close (self ) -> None :
226
- return self ._targetCursor .close ;
229
+ self ._targetCursor .close
227
230
228
231
def callproc (self , ** kwargs ):
229
- return self ._targetCursor .callproc (** kwargs );
232
+ return self ._targetCursor .callproc (** kwargs )
230
233
231
234
def execute (
232
235
self ,
233
236
query : str ,
234
237
** kwargs
235
238
) -> "AwsWrapperCursor" :
236
239
if self ._pluginManager .numOfPlugins == 0 :
237
- return self ._targetCursor .execute (query , ** kwargs )
240
+ self ._targetCursor = self ._targetCursor .execute (query , ** kwargs )
241
+ return self
238
242
239
- result = self ._pluginManager .executeFunc (lambda : self ._targetCursor .execute (query , ** kwargs ))
243
+ result = self ._pluginManager .executeFunc (lambda : self ._targetCursor .execute (query , ** kwargs ))
240
244
return result
241
245
242
246
def executemany (
@@ -264,12 +268,11 @@ def __iter__(self) -> Iterator[Any]:
264
268
def setinputsizes (self , sizes : Any ) -> None :
265
269
return self ._targetCursor .setinputsizes (sizes )
266
270
267
- def setoutputsize (self , size : Any , column : int = None ) -> None :
271
+ def setoutputsize (self , size : Any , column : Optional [ int ] = None ) -> None :
268
272
return self ._targetCursor .setoutputsize (size , column )
269
273
270
274
def __enter__ (self : "AwsWrapperCursor" ) -> "AwsWrapperCursor" :
271
275
return self
272
276
273
277
def __exit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
274
278
self .close ()
275
-
0 commit comments