Skip to content

Commit 4a4dd91

Browse files
authored
Merge pull request #73 from NCAR/hua-work-common
mv pg_pass.py back to pgpassword.py
2 parents 925c8f0 + 31a8e2f commit 4a4dd91

File tree

5 files changed

+160
-162
lines changed

5 files changed

+160
-162
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "rda_python_common"
7-
version = "2.0.2"
7+
version = "2.0.3"
88
authors = [
99
{ name="Zaihua Ji", email="zji@ucar.edu" },
1010
]
@@ -33,4 +33,3 @@ pythonpath = [
3333

3434
[project.scripts]
3535
pgpassword = "rda_python_common.pgpassword:main"
36-
pg_pass = "rda_python_common.pg_pass:main"

src/rda_python_common/pg_pass.py

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
#
3+
##################################################################################
4+
#
5+
# Title: pgpassword
6+
# Author: Zaihua Ji, zji@ucar.edu
7+
# Date: 2025-10-27
8+
# Purpose: python script to retrieve passwords for postgrsql login to connect a
9+
# gdex database from inside an python application
10+
#
11+
# Github: https://github.com/NCAR/rda-python-common.git
12+
#
13+
##################################################################################
14+
15+
import os
16+
import sys
17+
import re
18+
import pwd
19+
import hvac
20+
from . import PgLOG
21+
from . import PgDBI
22+
23+
DBFLDS = {
24+
'd' : 'dbname',
25+
'c' : 'scname',
26+
'h' : 'dbhost',
27+
'p' : 'dbport',
28+
'u' : 'lnname'
29+
}
30+
31+
DBINFO = {
32+
'dbname' : "",
33+
'scname' : "",
34+
'lnname' : "",
35+
'dbhost' : "",
36+
'dbport' : 5432
37+
}
38+
39+
#
40+
# main function to excecute this script
41+
#
42+
def main():
43+
44+
permit = False
45+
aname = 'pgpassword'
46+
argv = sys.argv[1:]
47+
opt = None
48+
dohelp = True
49+
dbopt = False
50+
51+
for arg in argv:
52+
if re.match(r'^-\w+$', arg):
53+
opt = arg[1:]
54+
elif opt:
55+
if opt == 'l':
56+
PgDBI.PGDBI['BAOURL'] = arg
57+
elif opt == 'k':
58+
PgDBI.PGDBI['BAOTOKEN'] = arg
59+
elif opt in DBFLDS:
60+
dbopt = True
61+
DBINFO[DBFLDS[opt]] = arg
62+
else:
63+
PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX)
64+
dohelp = False
65+
else:
66+
PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX)
67+
68+
if dohelp:
69+
print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
70+
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
71+
print(" -l OpenBao URL to retrieve passwords")
72+
print(" -k OpenBao Token Name to retrieve passwords")
73+
print(" -d PostgreSQL Database Name")
74+
print(" -c PostgreSQL Schema Name")
75+
print(" -u PostgreSQL Login User Name")
76+
print(" -h PostgreSQL Server Host Name")
77+
print(" -p PostgreSQL Port Number")
78+
sys.exit(0)
79+
80+
if dbopt:
81+
PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'],
82+
DBINFO['lnname'], None, DBINFO['dbport'])
83+
84+
pwname = PgDBI.get_baopassword()
85+
if not pwname: pwname = PgDBI.get_pgpassword()
86+
print(pwname)
87+
sys.exit(0)
88+
89+
#
90+
# call main() to start program
91+
#
92+
if __name__ == "__main__": main()

src/rda_python_common/pgpassword.py

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,90 @@
22
#
33
##################################################################################
44
#
5-
# Title: pgpassword
5+
# Title: pg_pass
66
# Author: Zaihua Ji, zji@ucar.edu
77
# Date: 2025-10-27
8+
# 2025-12-02 convert to class PgPassword
89
# Purpose: python script to retrieve passwords for postgrsql login to connect a
910
# gdex database from inside an python application
1011
#
1112
# Github: https://github.com/NCAR/rda-python-common.git
1213
#
1314
##################################################################################
1415

15-
import os
1616
import sys
1717
import re
18-
import pwd
19-
import hvac
20-
from . import PgLOG
21-
from . import PgDBI
18+
from .pg_dbi import PgDBI
2219

23-
DBFLDS = {
24-
'd' : 'dbname',
25-
'c' : 'scname',
26-
'h' : 'dbhost',
27-
'p' : 'dbport',
28-
'u' : 'lnname'
29-
}
20+
class PgPassword(PgDBI):
3021

31-
DBINFO = {
32-
'dbname' : "",
33-
'scname' : "",
34-
'lnname' : "",
35-
'dbhost' : "",
36-
'dbport' : 5432
37-
}
22+
def __init__(self):
23+
super().__init__() # initialize parent class
24+
self.DBFLDS = {
25+
'd' : 'dbname',
26+
'c' : 'scname',
27+
'h' : 'dbhost',
28+
'p' : 'dbport',
29+
'u' : 'lnname'
30+
}
31+
self.DBINFO = {
32+
'dbname' : "",
33+
'scname' : "",
34+
'lnname' : "",
35+
'dbhost' : "",
36+
'dbport' : 5432
37+
}
38+
self.dbopt = False
39+
self.password = ''
3840

39-
#
40-
# main function to excecute this script
41-
#
42-
def main():
43-
44-
permit = False
45-
aname = 'pgpassword'
46-
argv = sys.argv[1:]
47-
opt = None
48-
dohelp = True
49-
dbopt = False
50-
51-
for arg in argv:
52-
if re.match(r'^-\w+$', arg):
53-
opt = arg[1:]
54-
elif opt:
55-
if opt == 'l':
56-
PgDBI.PGDBI['BAOURL'] = arg
57-
elif opt == 'k':
58-
PgDBI.PGDBI['BAOTOKEN'] = arg
59-
elif opt in DBFLDS:
60-
dbopt = True
61-
DBINFO[DBFLDS[opt]] = arg
41+
# read in command line parameters
42+
def read_parameters(self):
43+
argv = sys.argv[1:]
44+
opt = None
45+
dohelp = True
46+
for arg in argv:
47+
if re.match(r'^-\w+$', arg):
48+
opt = arg[1:]
49+
elif opt:
50+
if opt == 'l':
51+
self.PGDBI['BAOURL'] = arg
52+
elif opt == 'k':
53+
self.PGDBI['BAOTOKEN'] = arg
54+
elif opt in self.DBFLDS:
55+
self.dbopt = True
56+
self.DBINFO[self.DBFLDS[opt]] = arg
57+
else:
58+
self.pglog(arg + ": Unknown option", self.LGEREX)
59+
dohelp = False
6260
else:
63-
PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX)
64-
dohelp = False
65-
else:
66-
PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX)
61+
self.pglog(arg + ": Value provided without option", self.LGEREX)
62+
if dohelp:
63+
print("Usage: pg_pass [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
64+
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
65+
print(" -l OpenBao URL to retrieve passwords")
66+
print(" -k OpenBao Token Name to retrieve passwords")
67+
print(" -d PostgreSQL Database Name")
68+
print(" -c PostgreSQL Schema Name")
69+
print(" -u PostgreSQL Login User Name")
70+
print(" -h PostgreSQL Server Host Name")
71+
print(" -p PostgreSQL Port Number")
72+
sys.exit(0)
6773

68-
if dohelp:
69-
print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
70-
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
71-
print(" -l OpenBao URL to retrieve passwords")
72-
print(" -k OpenBao Token Name to retrieve passwords")
73-
print(" -d PostgreSQL Database Name")
74-
print(" -c PostgreSQL Schema Name")
75-
print(" -u PostgreSQL Login User Name")
76-
print(" -h PostgreSQL Server Host Name")
77-
print(" -p PostgreSQL Port Number")
78-
sys.exit(0)
74+
# get the pgpassword
75+
def start_actions(self):
76+
if self.dbopt:
77+
self.default_scinfo(self.DBINFO['dbname'], self.DBINFO['scname'], self.DBINFO['dbhost'],
78+
self.DBINFO['lnname'], None, self.DBINFO['dbport'])
79+
self.password = self.get_baopassword()
80+
if not self.password: self.password = self.get_pg_pass()
7981

80-
if dbopt:
81-
PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'],
82-
DBINFO['lnname'], None, DBINFO['dbport'])
83-
84-
pwname = PgDBI.get_baopassword()
85-
if not pwname: pwname = PgDBI.get_pgpassword()
86-
print(pwname)
82+
# main function to excecute this script
83+
def main():
84+
object = PgPassword()
85+
object.read_parameters()
86+
object.start_actions()
87+
print(object.password)
8788
sys.exit(0)
8889

89-
#
9090
# call main() to start program
91-
#
9291
if __name__ == "__main__": main()

test/test_common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def test_common():
1414
import rda_python_common.PgSIG
1515
import rda_python_common.PgSplit
1616
import rda_python_common.PgUtil
17-
import rda_python_common.pgpassword
1817
import rda_python_common.pg_cmd
1918
import rda_python_common.pg_dbi
2019
import rda_python_common.pg_file
@@ -24,4 +23,4 @@ def test_common():
2423
import rda_python_common.pg_sig
2524
import rda_python_common.pg_split
2625
import rda_python_common.pg_util
27-
import rda_python_common.pg_pass
26+
import rda_python_common.pgpassword

0 commit comments

Comments
 (0)