forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlockss-plugin-props-print-parameter.py
executable file
·63 lines (52 loc) · 1.79 KB
/
lockss-plugin-props-print-parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
#
# lockss-plugin-props-print-parameter.py: accept a list of tab-separated values
# representing key-value pairs of LOCKSS Plugin properties and parameters, reformat it
# according to scripting needs and print it out in the desired format.
#
# Usage: <input> | lockss-plugin-props-print-parameter.py [--output=<FORMAT>]
#
# --output=<FORMAT> MIME-type of format for output.
# Supported formats: text/plain, application/json
#
# Input: text/tab-separated values, usually piped output from lockss-plugin-props.py
#
# @version 2019.0612
import fileinput, re, sys, json
from myLockssScripts import myPyCommandLine
if __name__ == '__main__' :
(sys.argv, switches) = myPyCommandLine(sys.argv, defaults={"output": "text/plain"}).parse()
if len(sys.argv) > 1 :
input = fileinput.input(files=sys.argv[1:2])
else :
input = fileinput.input()
table = {}
params = []
for line in input :
if len(line.strip()) > 0 :
fields = line.rstrip().split("\t", maxsplit=3)
param=fields[1]
if (re.match('^PARAM[(]', fields[0])) :
key=fields[2]
value=fields[1]
parameter_mapping = value.split("=", maxsplit=2)
if len(parameter_mapping) > 1 :
parameter_mapping[1] = json.loads(parameter_mapping[1])
params = params + [parameter_mapping]
else :
key=fields[0]
value=fields[1]
if 'application/json' == switches['output'] :
if table.get(key) is None :
table[key] = value
elif type(table.get(key)) is list :
table.get(key).append(value)
elif re.match(r'[^:]+:[^+]', key) :
table[key] = [ table.get(key), value ]
else :
table[key] = value
else :
print(key.upper() + ":\t" + value)
if ('output' in switches) and ('application/json' == switches['output']) :
table["parameters"] = params
print(json.dumps(table))