1212import types
1313import io
1414import numpy as np
15- from functools import reduce
1615
1716
1817class NumpyEncoder (json .JSONEncoder ):
@@ -45,18 +44,18 @@ def dumps(cls, obj):
4544 return json .dumps (obj , cls = cls )
4645
4746
48- class FetchComponent :
47+ class Component :
4948 def __init__ (
5049 self ,
5150 name ,
5251 component_config ,
5352 static_config ,
5453 connect_creds : dict ,
54+ payload = None ,
5555 ):
56- lcls = locals ()
5756 self .name = name
58- if static_config :
59- self .static_variables = types . MappingProxyType ( static_config )
57+ self . type = component_config [ "type" ]
58+ self .route = component_config [ "route" ]
6059 if not all (k in component_config for k in ("x" , "y" , "height" , "width" )):
6160 self .mode = "dynamic"
6261 else :
@@ -65,8 +64,22 @@ def __init__(
6564 self .y = component_config ["y" ]
6665 self .height = component_config ["height" ]
6766 self .width = component_config ["width" ]
68- self .type = component_config ["type" ]
69- self .route = component_config ["route" ]
67+ if static_config :
68+ self .static_variables = types .MappingProxyType (static_config )
69+ self .connection = dj .conn (
70+ host = connect_creds ["databaseAddress" ],
71+ user = connect_creds ["username" ],
72+ password = connect_creds ["password" ],
73+ reset = True ,
74+ )
75+ self .payload = payload
76+
77+
78+ class FetchComponent (Component ):
79+ def __init__ (self , * args , ** kwargs ):
80+ super ().__init__ (* args , ** kwargs )
81+ component_config = kwargs .get ("component_config" , args [1 ] if args else None )
82+ lcls = locals ()
7083 exec (component_config ["dj_query" ], globals (), lcls )
7184 self .dj_query = lcls ["dj_query" ]
7285 if "restriction" in component_config :
@@ -78,12 +91,7 @@ def __init__(
7891 dj .VirtualModule (
7992 s ,
8093 s ,
81- connection = dj .conn (
82- host = connect_creds ["databaseAddress" ],
83- user = connect_creds ["username" ],
84- password = connect_creds ["password" ],
85- reset = True ,
86- ),
94+ connection = self .connection ,
8795 )
8896 for s in inspect .getfullargspec (self .dj_query ).args
8997 ]
@@ -125,37 +133,12 @@ def dj_query_route(self):
125133 )
126134
127135
128- class InsertComponent :
136+ class InsertComponent ( Component ) :
129137 fields_route_format = "{route}/fields"
130138
131- def __init__ (
132- self ,
133- name ,
134- component_config ,
135- static_config ,
136- payload ,
137- connect_creds : dict ,
138- ):
139- self .name = name
140- self .payload = payload
141- if static_config :
142- self .static_variables = types .MappingProxyType (static_config )
143- if not all (k in component_config for k in ("x" , "y" , "height" , "width" )):
144- self .mode = "dynamic"
145- else :
146- self .mode = "fixed"
147- self .x = component_config ["x" ]
148- self .y = component_config ["y" ]
149- self .height = component_config ["height" ]
150- self .width = component_config ["width" ]
151- self .type = component_config ["type" ]
152- self .route = component_config ["route" ]
153- self .connection = dj .conn (
154- host = connect_creds ["databaseAddress" ],
155- user = connect_creds ["username" ],
156- password = connect_creds ["password" ],
157- reset = True ,
158- )
139+ def __init__ (self , * args , ** kwargs ):
140+ super ().__init__ (* args , ** kwargs )
141+ component_config = kwargs .get ("component_config" , args [1 ] if args else None )
159142 self .fields_map = component_config .get ("map" )
160143 self .tables = [
161144 getattr (
@@ -181,37 +164,22 @@ def __init__(
181164 ),
182165 key = lambda p : p .full_table_name ,
183166 )
167+ self .destination_lookup = {
168+ sub_m .get ("input" , sub_m ["destination" ]): sub_m ["destination" ]
169+ for m in (self .fields_map or [])
170+ for sub_m in (m .get ("map" , []) + [m ])
171+ }
172+ self .input_lookup = {v : k for k , v in self .destination_lookup .items ()}
184173
185174 def dj_query_route (self ):
186175 with self .connection .transaction :
187- destination_lookup = reduce (
188- lambda m0 , m1 : dict (
189- m0 ,
190- ** (
191- {
192- m_t ["input" ]
193- if "input" in m_t
194- else m_t ["destination" ]: m_t ["destination" ]
195- for m_t in m1 ["map" ]
196- }
197- if m1 ["type" ] == "table"
198- else {
199- m1 ["input" ]
200- if "input" in m1
201- else m1 ["destination" ]: m1 ["destination" ]
202- }
203- ),
204- ),
205- self .fields_map or [],
206- {},
207- )
208176 for t in self .tables :
209177 t .insert (
210178 [
211179 {
212180 a : v
213181 for k , v in r .items ()
214- if (a := destination_lookup .get (k , k ))
182+ if (a := self . destination_lookup .get (k , k ))
215183 in t .heading .attributes
216184 }
217185 for r in self .payload ["submissions" ]
@@ -244,27 +212,11 @@ def fields_route(self):
244212 fields = [
245213 dict (
246214 (field := source_fields .pop (m ["destination" ])),
247- name = m [ "input" if "input" in m else "destination" ],
215+ name = m . get ( "input" , m [ "destination" ]) ,
248216 ** (
249217 {
250- "values" : field ["values" ]
251- if "map" not in m
252- else [
253- {
254- input_lookup [k ]: v
255- for k , v in r .items ()
256- if k
257- in (
258- input_lookup := {
259- table_m ["destination" ]: table_m [
260- "input"
261- if "input" in table_m
262- else "destination"
263- ]
264- for table_m in m ["map" ]
265- }
266- )
267- }
218+ "values" : [
219+ {self .input_lookup .get (k , k ): v for k , v in r .items ()}
268220 for r in field ["values" ]
269221 ]
270222 }
0 commit comments