rda-python-common 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rda-python-common might be problematic. Click here for more details.

@@ -0,0 +1,298 @@
1
+ #
2
+ ###############################################################################
3
+ #
4
+ # Title : PgSplit.py -- PostgreSQL DataBase Interface foe table wfile
5
+ # Author : Zaihua Ji, zji@ucar.edu
6
+ # Date : 09/010/2024
7
+ # Purpose : Python library module to handle query and manipulate table wfile
8
+ #
9
+ # Work File : $DSSHOME/lib/python/PgSplit.py
10
+ # Github : https://github.com/NCAR/rda-shared-libraries.git
11
+ #
12
+ ###############################################################################
13
+
14
+ import os
15
+ import re
16
+ from os import path as op
17
+ import PgLOG
18
+ import PgDBI
19
+ import PgUtil
20
+
21
+ #
22
+ # compare wfile records between tables wfile and wfile_dNNNNNN,
23
+ # and return the records need to be added, modified and deleted
24
+ #
25
+ def compare_wfile(wfrecs, dsrecs):
26
+
27
+ flds = dsrecs.keys()
28
+ flen = len(flds)
29
+ arecs = dict(zip(flds, [[]]*flen))
30
+ mrecs = {}
31
+ drecs = []
32
+ wfcnt = len(wfrecs['wid'])
33
+ dscnt = len(dsrecs['wid'])
34
+ pi = pj = -1
35
+ i = j = 0
36
+ while i < wfcnt and j < dscnt:
37
+ if i > pi:
38
+ wfrec = PgUtil.onerecord(wfrecs, i)
39
+ wwid = wfrec['wid']
40
+ pi = i
41
+ if j > pj:
42
+ dsrec = PgUtil.onerecord(dsrecs, j)
43
+ dwid = dsrec['wid']
44
+ pj = j
45
+ if wwid == dwid:
46
+ mrec = compare_one_record(flds, wfrec, dsrec)
47
+ if mrec: mrecs[wwid] = mrec
48
+ i += 1
49
+ j += 1
50
+ elif wwid > dwid:
51
+ drecs.append(dwid)
52
+ j += 1
53
+ else:
54
+ for fld in flds:
55
+ arecs[fld].append(wfrec[fld])
56
+ i += 1
57
+ if i < wfcnt:
58
+ for fld in flds:
59
+ arecs[fld].extend(wfrecs[fld][i:wfcnt])
60
+ elif j < dscnt:
61
+ drecs.extend(dsrecs['wid'][j:dscnt])
62
+
63
+ if len(arecs['wid']) == 0: arecs = {}
64
+
65
+ return (arecs, mrecs, drecs)
66
+
67
+ #
68
+ # Compare column values and return the new one; empty if the same
69
+ #
70
+ def compare_one_record(flds, wfrec, dsrec):
71
+
72
+ mrec = {}
73
+ for fld in flds:
74
+ if wfrec[fld] != dsrec[fld]: mrec[fld] = wfrec[fld]
75
+
76
+ return mrec
77
+
78
+ #
79
+ # convert wfile records to wfile_dsid records
80
+ #
81
+ def wfile2wdsid(wfrecs, wids = None):
82
+
83
+ dsrecs = {}
84
+ if wfrecs:
85
+ for fld in wfrecs:
86
+ if fld == 'dsid': continue
87
+ dsrecs[fld] = wfrecs[fld]
88
+ if wids: dsrecs['wid'] = wids
89
+ return dsrecs
90
+
91
+ #
92
+ # trim wfile records
93
+ #
94
+ def trim_wfile_fields(wfrecs):
95
+
96
+ records = {}
97
+ if 'wfile' in wfrecs: records['wfile'] = wfrecs['wfile']
98
+ if 'dsid' in wfrecs: records['dsid'] = wfrecs['dsid']
99
+
100
+ return records
101
+
102
+ #
103
+ # check the condition string, and add dsid if needed
104
+ #
105
+ def get_dsid_condition(dsid, condition):
106
+
107
+ if condition:
108
+ if re.search('(^|.| )(wid|dsid)\s*=', condition):
109
+ return condition
110
+ else:
111
+ dscnd = "wfile.dsid = '{}' ".format(dsid)
112
+ if not re.match(r'^\s*(ORDER|GROUP|HAVING|OFFSET|LIMIT)\s', condition, re.I): dscnd += 'AND '
113
+ return dscnd + condition # no where clause, append directly
114
+ else:
115
+ return "wfile.dsid = '{}'".format(dsid)
116
+
117
+ #
118
+ # insert one record into wfile and/or wfile_dsid
119
+ #
120
+ def pgadd_wfile(dsid, wfrec, logact = PgLOG.LOGERR, getid = None):
121
+
122
+
123
+ record = {'wfile' : wfrec['wfile'],
124
+ 'dsid' : (wfrec['dsid'] if 'dsid' in wfrec else dsid)}
125
+ wret = PgDBI.pgadd('wfile', record, logact, 'wid')
126
+ if wret:
127
+ record = wfile2wdsid(wfrec, wret)
128
+ PgDBI.pgadd('wfile_' + dsid, record, logact|PgLOG.ADDTBL)
129
+
130
+ if logact&PgLOG.AUTOID or getid:
131
+ return wret
132
+ else:
133
+ return 1 if wret else 0
134
+
135
+ #
136
+ # insert multiple records into wfile and/or wfile_dsid
137
+ #
138
+ def pgmadd_wfile(dsid, wfrecs, logact = PgLOG.LOGERR, getid = None):
139
+
140
+ records = {'wfile' : wfrecs['wfile'],
141
+ 'dsid' : (wfrecs['dsid'] if 'dsid' in wfrecs else [dsid]*len(wfrecs['wfile']))}
142
+ wret = PgDBI.pgmadd('wfile', records, logact, 'wid')
143
+ wcnt = wret if isinstance(wret, int) else len(wret)
144
+ if wcnt:
145
+ records = wfile2wdsid(wfrecs, wret)
146
+ PgDBI.pgmadd('wfile_' + dsid, records, logact|PgLOG.ADDTBL)
147
+
148
+ if logact&PgLOG.AUTOID or getid:
149
+ return wret
150
+ else:
151
+ return wcnt
152
+
153
+ #
154
+ # update one or multiple rows in wfile and/or wfile_dsid
155
+ # exclude dsid in condition
156
+ #
157
+ def pgupdt_wfile(dsid, wfrec, condition, logact = PgLOG.LOGERR):
158
+
159
+ record = trim_wfile_fields(wfrec)
160
+ if record:
161
+ wret = PgDBI.pgupdt('wfile', record, get_dsid_condition(dsid, condition), logact)
162
+ else:
163
+ wret = 1
164
+ if wret:
165
+ record = wfile2wdsid(wfrec)
166
+ if record: wret = PgDBI.pgupdt("wfile_" + dsid, record, condition, logact|PgLOG.ADDTBL)
167
+
168
+ return wret
169
+
170
+ #
171
+ # update one row in wfile and/or wfile_dsid with dsid change
172
+ # exclude dsid in condition
173
+ #
174
+ def pgupdt_wfile_dsid(dsid, odsid, wfrec, wid, logact = PgLOG.LOGERR):
175
+
176
+ record = trim_wfile_fields(wfrec)
177
+ cnd = 'wid = {}'.format(wid)
178
+ if record:
179
+ wret = PgDBI.pgupdt('wfile', record, cnd, logact)
180
+ else:
181
+ wret = 1
182
+ if wret:
183
+ record = wfile2wdsid(wfrec)
184
+ tname = 'wfile_' + dsid
185
+ doupdt = True
186
+ if odsid and odsid != dsid:
187
+ oname = 'wfile_' + odsid
188
+ pgrec = PgDBI.pgget(oname, '*', cnd, logact|PgLOG.ADDTBL)
189
+ if pgrec:
190
+ for fld in record:
191
+ pgrec[fld] = record[fld]
192
+ wret = PgDBI.pgadd(tname, pgrec, logact|PgLOG.ADDTBL)
193
+ if wret: PgDBI.pgdel(oname, cnd, logact)
194
+ doupdt = False
195
+ if doupdt and record:
196
+ wret = PgDBI.pgupdt(tname, record, cnd, logact|PgLOG.ADDTBL)
197
+
198
+ return wret
199
+
200
+ #
201
+ # delete one or multiple rows in wfile and/or wfile_dsid, and add the record(s) into wfile_delete
202
+ # exclude dsid in conidtion
203
+ #
204
+ def pgdel_wfile(dsid, condition, logact = PgLOG.LOGERR):
205
+
206
+ pgrecs = pgmget_wfile(dsid, '*', condition, logact|PgLOG.ADDTBL)
207
+ wret = PgDBI.pgdel('wfile', get_dsid_condition(dsid, condition), logact)
208
+ if wret: PgDBI.pgdel("wfile_" + dsid, condition, logact)
209
+ if wret and pgrecs: PgDBI.pgmadd('wfile_delete', pgrecs, logact)
210
+
211
+ return wret
212
+
213
+ #
214
+ # delete one or multiple rows in sfile, and add the record(s) into sfile_delete
215
+ #
216
+ def pgdel_sfile(condition, logact = PgLOG.LOGERR):
217
+
218
+ pgrecs = PgDBI.pgmget('sfile', '*', condition, logact)
219
+ sret = PgDBI.pgdel('sfile', condition, logact)
220
+ if sret and pgrecs: PgDBI.pgmadd('sfile_delete', pgrecs, logact)
221
+
222
+ return sret
223
+
224
+ #
225
+ # update one or multiple rows in wfile and/or wfile_dsid for multiple dsid
226
+ # exclude dsid in condition
227
+ #
228
+ def pgupdt_wfile_dsids(dsid, dsids, brec, bcnd, logact = PgLOG.LOGERR):
229
+
230
+ record = trim_wfile_fields(brec)
231
+ if record:
232
+ wret = PgDBI.pgupdt("wfile", record, bcnd, logact)
233
+ else:
234
+ wret = 1
235
+ if wret:
236
+ record = wfile2wdsid(brec)
237
+ if record:
238
+ wret = 0
239
+ dids = [dsid]
240
+ if dsids: dids.extend(dsids.split(','))
241
+ for did in dids:
242
+ wret += PgDBI.pgupdt("wfile_" + did, record, bcnd, logact|PgLOG.ADDTBL)
243
+
244
+ return wret
245
+
246
+ #
247
+ # get one record from wfile or wfile_dsid
248
+ # exclude dsid in fields and condition
249
+ #
250
+ def pgget_wfile(dsid, fields, condition, logact = PgLOG.LOGERR):
251
+
252
+ tname = "wfile_" + dsid
253
+ flds = fields.replace('wfile.', tname + '.')
254
+ cnd = condition.replace('wfile.', tname + '.')
255
+ record = PgDBI.pgget(tname, flds, cnd, logact|PgLOG.ADDTBL)
256
+ if record and flds == '*': record['dsid'] = dsid
257
+ return record
258
+
259
+ #
260
+ # get one record from wfile or wfile_dsid joing other tables
261
+ # exclude dsid in fields and condition
262
+ #
263
+ def pgget_wfile_join(dsid, tjoin, fields, condition, logact = PgLOG.LOGERR):
264
+
265
+ tname = "wfile_" + dsid
266
+ flds = fields.replace('wfile.', tname + '.')
267
+ jname = tname + ' ' + tjoin.replace('wfile.', tname + '.')
268
+ cnd = condition.replace('wfile.', tname + '.')
269
+ record = PgDBI.pgget(jname, flds, cnd, logact|PgLOG.ADDTBL)
270
+ if record and flds == '*': record['dsid'] = dsid
271
+ return record
272
+
273
+ #
274
+ # get multiple records from wfile or wfile_dsid
275
+ # exclude dsid in fields and condition
276
+ #
277
+ def pgmget_wfile(dsid, fields, condition, logact = PgLOG.LOGERR):
278
+
279
+ tname = "wfile_" + dsid
280
+ flds = fields.replace('wfile.', tname + '.')
281
+ cnd = condition.replace('wfile.', tname + '.')
282
+ records = PgDBI.pgmget(tname, flds, cnd, logact|PgLOG.ADDTBL)
283
+ if records and flds == '*': records['dsid'] = [dsid]*len(records['wid'])
284
+ return records
285
+
286
+ #
287
+ # get multiple records from wfile or wfile_dsid joining other tables
288
+ # exclude dsid in fields and condition
289
+ #
290
+ def pgmget_wfile_join(dsid, tjoin, fields, condition, logact = PgLOG.LOGERR):
291
+
292
+ tname = "wfile_" + dsid
293
+ flds = fields.replace('wfile.', tname + '.')
294
+ jname = tname + ' ' + tjoin.replace('wfile.', tname + '.')
295
+ cnd = condition.replace('wfile.', tname + '.')
296
+ records = PgDBI.pgmget(jname, flds, cnd, logact|PgLOG.ADDTBL)
297
+ if records and flds == '*': records['dsid'] = [dsid]*len(records['wid'])
298
+ return records