-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathns_parameters.py
More file actions
137 lines (111 loc) · 4 KB
/
ns_parameters.py
File metadata and controls
137 lines (111 loc) · 4 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Functions to handle custom parameters.
pass in a list of name and default value parameters
[
{'name': name of parameter,
'default': default value of parameter,
'notice': 'string to send notice if not set',
'isRequired: True/False,
},
{'name': name of parameter,
'default': default value of parameter,
'notice': 'string to send notice if not set',
'isRequired: True/False,
},
]
"""
try:
import polyinterface
except ImportError:
import pgc_interface as polyinterface
LOGGER = polyinterface.LOGGER
class NSParameters:
def __init__(self, parameters):
self.internal = []
for p in parameters:
self.internal.append({
'name': p['name'],
'value': '',
'default': p['default'],
'isSet': False,
'isRequired': p['isRequired'],
'notice_msg': p['notice'],
})
def set(self, name, value):
for p in self.internal:
if p['name'] == name:
p['value'] = value
p['isSet'] = True
return
def get(self, name):
for p in self.internal:
if p['name'] == name:
if p['isSet']:
return p['value']
else:
return p['default']
def isSet(self, name):
for p in self.internal:
if p['name'] == name:
return p['isSet']
return False
"""
Send notices for unconfigured parameters that are are marked
as required.
"""
def send_notices(self, poly):
for p in self.internal:
if not p['isSet'] and p['isRequired']:
if p['notice_msg'] is not None:
try:
poly.addNotice(p['notice_msg'], p['name'])
except:
poly.addNotice({p['name'] : p['notice_msg']})
"""
Read paramenters from Polyglot and update values appropriately.
return True if all required parameters are set to non-default values
otherwise return False
"""
def get_from_polyglot(self, poly):
customParams = poly.polyConfig['customParams']
params = {}
for p in self.internal:
LOGGER.debug('checking for ' + p['name'] + ' in customParams')
if p['name'] in customParams:
LOGGER.debug('found ' + p['name'] + ' in customParams')
p['value'] = customParams[p['name']]
if p['value'] != p['default']:
LOGGER.debug(p['name'] + ' is now set')
p['isSet'] = True
if p['isSet']:
params[p['name']] = p['value']
else:
params[p['name']] = p['default']
poly.addCustomParam(params)
for p in self.internal:
if not p['isSet'] and p['isRequired']:
return False
return True
"""
Called from process_config to check for configuration change
We need to know two things; 1) did the configuration change and
2) are all required fields filled in.
"""
def update_from_polyglot(self, config):
changed = False
valid = True
if 'customParams' in config:
for p in self.internal:
if p['name'] in config['customParams']:
poly_param = config['customParams'][p['name']]
# did it change?
if poly_param != p['default'] and poly_param != p['value']:
changed = True
# is it different from the default?
if poly_param != p['default']:
p['value'] = poly_param
p['isSet'] = True
for p in self.internal:
if not p['isSet'] and p['isRequired']:
valid = False
return (valid, changed)