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,152 @@
1
+ #
2
+ ###############################################################################
3
+ #
4
+ # Title : PgPGS.py -- PostgreSQL Interface for CDP DataBase Per psql
5
+ # Author : Zaihua Ji, zji@ucar.edu
6
+ # Date : 08/31/2020
7
+ # Purpose : python library module to handle sql scripts to retrieve info
8
+ # from cdp database per psql
9
+ #
10
+ # Work File : $DSSHOME/lib/python/PgPGS.py
11
+ # Github : https://github.com/NCAR/rda-shared-libraries.git
12
+ #
13
+ ###############################################################################
14
+ #
15
+ import PgLOG
16
+ import os
17
+ import re
18
+
19
+ PGPGS = {}
20
+ PGPGS["PGSSERV"] = PgLOG.get_environment("PGSSERV", '-h vetsdbprod -p 5432 -U acadmin access_control');
21
+ PGPGS["SQLPATH"] = PgLOG.get_environment("SQLPATH", PgLOG.PGLOG['DSSHOME']+ "/dssdb/sql");
22
+
23
+ #
24
+ # local function: create sql file
25
+ #
26
+ def pgs_sql_file(tablenames, fields, condition = None):
27
+
28
+ sqlfile = "{}/pgs{}.sql".format(PGPGS['SQLPATH'], os.getpid())
29
+
30
+ sqlstr = "SELECT {}\nFROM {}".format(fields, tablenames)
31
+ if condition:
32
+ if re.match(r'^\s*(ORDER|GROUP|HAVING)\s', condition, re.I):
33
+ sqlstr += "\n{}".format(condition)
34
+ else:
35
+ sqlstr += "\nWHERE {}".format(condition)
36
+ sqlstr += ";\n"
37
+ try:
38
+ SQL = open(sqlfile, 'w')
39
+ SQL.write(sqlstr)
40
+ SQL.close()
41
+ except Exception as e:
42
+ PgLOG.pglog("Error Open '{}': {}".format(sqlfile, str(e)), PgLOG.LGWNEX)
43
+
44
+ if PgLOG.PGLOG['DBGLEVEL']: PgLOG.pgdbg(1000, sqlstr)
45
+
46
+ return sqlfile
47
+
48
+ #
49
+ # tablenames: comma deliminated string of table names
50
+ # fields: fieldnames for query pgscle database,
51
+ # condition: querry conditions for where clause)
52
+ # Return: one record from tablename, a hash reference with keys as field names
53
+ # and values as field values upon success, FAILURE otherwise
54
+ #
55
+ def pgsget(tablenames, fields, condition = None, logact = 0):
56
+
57
+ sqlfile = pgs_sql_file(tablenames, fields, condition)
58
+ sqlout = PgLOG.pgsystem("psql {} < {}".format(PGPGS['PGSSERV'], sqlfile), logact, 273+1024) # 1+16+256
59
+
60
+ colcnt = 0
61
+ record = {}
62
+ if sqlout:
63
+ for line in re.split(r'\n', sqlout):
64
+ vals = re.split(r'\s*\|\s+', line)
65
+ if colcnt: # gather data
66
+ record = dict(zip(fields, vals))
67
+ break
68
+ else: # gather field names
69
+ flds = vals
70
+ colcnt = len(flds)
71
+ elif PgLOG.PGLOG['SYSERR']: # error happens
72
+ PgLOG.pglog(PgLOG.PGLOG['SYSERR'], logact|PgLOG.ERRLOG)
73
+
74
+ if PgLOG.PGLOG['DBGLEVEL']:
75
+ if record:
76
+ PgLOG.pgdbg(1000, "pgsget: 1 record retrieved from {}:\n{}".format(tablenames, str(record)))
77
+ else:
78
+ PgLOG.pgdbg(1000, "pgsget: 0 record retrieved from " + tablenames)
79
+
80
+ os.remove(sqlfile)
81
+
82
+ return record
83
+
84
+ #
85
+ # tablenames: comma deliminated string of tables
86
+ # fields: fieldnames for query pgscle database,
87
+ # condition: querry conditions for where clause)
88
+ # Return: mutiple records from tablenames, a dict with field names as keys and lists
89
+ # of retrieved values. All arrays are same size. FAILURE if not success
90
+ #
91
+ def pgsmget(tablenames, fields, condition = None, logact = 0):
92
+
93
+ sqlfile = pgs_sql_file(tablenames, fields, condition)
94
+ sqlout = PgLOG.pgsystem("psql {} < {}".format(PGPGS['PGSSERV'], sqlfile), logact, 273+1024) # 1+16+256
95
+
96
+ rowcnt = colcnt = 0
97
+ records = {}
98
+ vals = []
99
+ if sqlout:
100
+ for line in re.split(r'\n', sqlout):
101
+ row = re.split(r'\s*\|\s+', line)
102
+ if colcnt: # gather data
103
+ vals.append(row)
104
+ rowcnt += 1
105
+ else: # gather field names
106
+ flds = row
107
+ colcnt = len(flds)
108
+ if rowcnt > 0:
109
+ records = dict(zip(flds, list(zip(*vals))))
110
+ elif PgLOG.PGLOG['SYSERR']: # error happens
111
+ PgLOG.pglog(PgLOG.PGLOG['SYSERR'], logact|PgLOG.ERRLOG)
112
+
113
+ if PgLOG.PGLOG['DBGLEVEL']:
114
+ PgLOG.pgdbg(1000, "pgsmget: {} record(s) retrieved from {}".format(rowcnt, tablenames))
115
+
116
+ os.remove(sqlfile) # remove sqlfile when successful
117
+
118
+ return records
119
+
120
+ #
121
+ # email: cdp user email address,
122
+ # userid: cdp user ID,
123
+ # username: cdp user name
124
+ # Return: one record from CDP PostGreSQL database; PGLOG.FAILURE otherwise
125
+ #
126
+ def get_cdp_user(email, userid = 0, username = None, logact = 0):
127
+
128
+ if userid:
129
+ condition = "id = {}".format(userid)
130
+ elif email:
131
+ condition = "email = '{}'".format(email)
132
+ elif username:
133
+ condition = "username = '{}'".format(username)
134
+ else:
135
+ return PgLOG.FAILURE
136
+
137
+ fields = ("id as cdpid, firstname as fstname, middlename as midinit, " +
138
+ "lastname as lstname, email, username as cdpname, " +
139
+ "organization as org_name, organization_type as org_type, country")
140
+ return pgsget('users', fields, condition, logact)
141
+
142
+ #
143
+ # name: field name
144
+ # value: field value
145
+ # Return: converted value from upcases to lower case
146
+ #
147
+ def convert_pgs_case(name, value):
148
+
149
+ if name == "username" or name == "email":
150
+ return value.lower()
151
+ else:
152
+ return value # no change