121 Read transform file. Reads text file containing JSON formatted specification of transforms.
122 file_uri: file must exist and be accessible. Otherwise, returned title will begin with notok: and have error message
123 Returns transforms list of Transform objects. If error occurs then transforms[0].title will start with notok:
134 hash_transforms:dict={}
136 file_uri=file_uri.strip()
138 raise ValueError(
"missing file_uri")
140 with open(file_uri,
"r",encoding=
"utf-8")
as f:
142 jstr=jstr.replace(
"\r\n",
"").replace(
"\r",
"").replace(
"\n",
"")
143 jstr=jstr.replace(dq,
"")
144 if "{transform:[" not in jstr:
145 raise ValueError(
"no transforms found in read file lines")
146 tarray= jstr.split(
"{transform:[")
149 if jstr.startswith(
","):
151 if jstr.endswith(
","):
153 if jstr.endswith(
"}"):
155 if jstr.endswith(
"]"):
157 if "{title:" in jstr:
158 jstr=jstr[jstr.find(
"{title:")+7:]
159 curstr=jstr[:jstr.find(
"}")]
160 curstr=curstr.strip()
161 jstr=jstr[jstr.find(
"}")+1:]
162 if jstr.startswith(
","):
165 raise ValueError(
"transform title is empty")
166 if curstr.lower()
in hash_transforms:
167 raise ValueError(
"duplicate transform title: " + curstr.lower())
170 jstr=jstr[jstr.find(
"{ops:[")+6:]
171 if jstr.endswith(
","):
173 if jstr.endswith(
"}"):
175 if jstr.endswith(
"]"):
178 if "{title:" in jstr:
179 oparray= jstr.split(
"{title:")
182 curstr=s1[:s1.find(
",order:")]
183 jstr= s1[s1.find(
",order:")+1:]
184 if jstr.endswith(
","):
186 if jstr.endswith(
"}"):
188 if jstr.startswith(
","):
192 tobj.ops.append(opobj)
194 curstr=jstr[jstr.find(
"order:")+6:]
195 curstr=curstr[:curstr.find(
",")]
196 if numfuncs.is_int(curstr):
197 opobj.order= int(curstr)
198 if "param1:" in jstr:
199 curstr=jstr[jstr.find(
"param1:")+7:]
200 if ",param" in curstr:
201 curstr=curstr[:curstr.find(
",param")]
203 if "param2:" in jstr:
204 curstr=jstr[jstr.find(
"param2:")+7:]
205 if ",param" in curstr:
206 curstr=curstr[:curstr.find(
",param")]
208 if "param3:" in jstr:
209 curstr=jstr[jstr.find(
"param3:")+7:]
211 hash_transforms[tobj.title.lower()]= len(transforms)
212 transforms.append(tobj)
213 except (RuntimeError, ValueError, OSError)
as err:
214 err_msg=
"notok:" + str(err)
224 Writes text file containing JSON formatted specification of transforms.
225 file_uri: file must exist and be accessible.
226 transforms: list of Transform object to write to file in JSON
227 Returns message that starts with notok: if error occurs.
232 file_uri=file_uri.strip()
234 raise ValueError(
"missing file_uri")
235 if len(transforms)==0:
236 raise ValueError(
"missing transforms")
238 with open(file_uri,
"w", encoding=
"utf-8")
as f:
239 for i
in range(len(transforms)):
240 if not isinstance(transforms[i], Transform):
241 raise ValueError(
"object in transforms list is not type=Transform")
246 f.write(delim + transforms[i].get_json(
True,
True))
247 except (RuntimeError, ValueError, OSError)
as err:
248 err_msg=
"notok:" + str(err)
254 Extract list of lookup dict titles used in transforms
255 transforms: list of Transform objects
256 Returns list of lookup dict titles. If error occurs, 0th entry will have
257 title starting with notok:
261 hash_lkupdicts:dict={}
264 if len(transforms)==0:
267 if not isinstance(t, Transform):
268 raise ValueError(
"object in transforms list is not type=Transform")
270 if not isinstance(op, Op):
271 raise ValueError(
"object in transform " + t.title +
" Ops is not type=Op")
272 if op.title.lower().startswith(
"lookup"):
273 parm_str= op.param1.lower()
274 if len(parm_str)>0
and parm_str
not in hash_lkupdicts:
275 lkupdicts.append(parm_str)
276 hash_lkupdicts[parm_str]= len(lkupdicts)-1
277 except (RuntimeError, ValueError, OSError)
as err:
278 lkupdicts.insert(0,
"notok:" + str(err))
284 Finds fields set in param2,param3 of tranform Ops for lookup
285 transforms: list of Transform objects
286 Returns new transform collection with lookup Ops having fields in
287 param2,param3 extracted into p2list,p3list.
294 if len(transforms)==0:
297 if not isinstance(t, Transform):
298 raise ValueError(
"object in transforms list is not type=Transform")
300 if not isinstance(op, Op):
301 raise ValueError(
"object in transform " + t.title +
" Ops is not type=Op")
302 if op.title.lower().startswith(
"lookup"):
303 parm_str= op.param2.lower()
305 atemp= parm_str.split(
"|")
309 op.p2list.append(fld)
310 parm_str= op.param3.lower()
312 atemp= parm_str.split(
"|")
316 op.p3list.append(fld)
317 except (RuntimeError, ValueError, OSError):
324 Extract referenced fields used in transforms
325 transforms: list of Transform objects
326 Returns dictionary with keys= field titles and thier values=number instances used. If error occurs, there will be a
327 key starting with notok:<error reason>
335 if len(transforms)==0:
338 if not isinstance(t, Transform):
339 raise ValueError(
"object in transforms list is not type=Transform")
341 optitle= op.title.lower()
342 if not isinstance(op, Op):
343 raise ValueError(
"object in transform " + t.title +
" Ops is not type=Op")
344 if optitle.endswith(
"ref")
or optitle.endswith(
"refs"):
345 parm_str= op.param1.lower()
347 if parm_str
not in hash_fields:
348 hash_fields[parm_str]= 0
349 hash_fields[parm_str] += 1
350 if optitle.endswith(
"refs"):
351 parm_str= op.param2.lower()
353 if parm_str
not in hash_fields:
354 hash_fields[parm_str]= 0
355 hash_fields[parm_str] += 1
356 parm_str= op.param3.lower()
358 if parm_str
not in hash_fields:
359 hash_fields[parm_str]= 0
360 hash_fields[parm_str] += 1
361 elif optitle==
"settofreqlist":
362 parm_str= op.param3.lower()
364 if parm_str
not in hash_fields:
365 hash_fields[parm_str]= 0
366 hash_fields[parm_str] += 1
367 elif optitle==
"lookup":
368 parm_str= op.param2.lower()
372 parm_str2= parm_str[(parm_str.find(
"|")+1):]
373 parm_str= parm_str[:parm_str.find(
"|")]
374 if parm_str
not in hash_fields:
375 hash_fields[parm_str]= 0
376 hash_fields[parm_str] += 1
378 if parm_str2
not in hash_fields:
379 hash_fields[parm_str2]= 0
380 hash_fields[parm_str2] += 1
381 except (RuntimeError, ValueError, OSError)
as err:
382 hash_fields[
"notok:" + str(err)]=0