forked from haddocking/haddock-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_to_json.py
executable file
·280 lines (258 loc) · 11.3 KB
/
param_to_json.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# coding=utf-8
# To support python 2.5+
from __future__ import with_statement
import argparse
import os
import sys
import json
import collections
"""
param_to_json.py
Convert a haddockparam.web in JSON format.
Can also be used to get or modify a parameter value.
Inspired by Sjoerd de Vries parser used in HADDOCK.
"""
class HaddockParamWeb(object):
def __init__(self, filename):
self.filename = filename
self.type = self._type()
self.data = self._parse()
self._cast_type()
def _type(self):
with open(self.filename, 'r') as f:
s = f.read()
return s.splitlines()[0].split()[0]
def _parse(self):
with open(self.filename, 'r') as f:
s = f.read()
s = s.rstrip('\n').rstrip() + ","
stack = [([], True), ]
curr, listmode = stack[-1]
ident = 0
objectlist = "ObjectList"
for line in s.splitlines():
if line.endswith("),"):
if listmode is objectlist: # we are parsing an objectlist
ll = line[ident + 2 * (curr[2] - 1):]
if ll == "),": # dedent
if curr[2] > 0:
curr[1] += line[ident:] + "\n"
curr[2] -= 1
if curr[2] == 0: # we are at outer level, parse what we have and reset
# Seems to be never reached
pass
else: # dedent and leave objectlist mode
curr[:] = curr[3]
stack.pop()
curr, listmode = stack[-1]
ident -= 2
else:
if curr[2] == 0: # parsing an elemental value, e.g Float(1), at outer level
# Seems to be never reached
pass
else: # elemental value at inner level, treat it as continuation
curr[1] += line[ident:] + "\n"
else: # no objectlist, dedent
stack.pop()
curr, listmode = stack[-1]
ident -= 2
elif line.endswith(","): # continuation
if listmode: # we're in an array, we expect unnamed items
v = line[ident:-1]
curr.append(v)
elif listmode is objectlist: # we're parsing an objectlist, gather it for later
curr[1] += line[ident:] + "\n"
else: # we're in a class, we expect named items
eq = line.index("=")
k = line[ident:eq - 1]
v = line[eq + 2:-1]
curr[k] = v
else: # endswith ( => indent
new = None
if not listmode: # we're in a class, we expect named items
k = line[ident:line.find("=") - 1]
if line.find("ObjectList") > -1: # enter objectlist mode for the inner level
new = [None, None, 0, []]
listmode = objectlist
elif line.endswith("Array ("): # enter array mode for the inner level
new = []
listmode = True
else: # enter class mode for the inner level
new = {}
listmode = False
stack.append((new, listmode)) # push the stack
curr[k] = new
elif listmode is objectlist: # parsing objectlist is just gathering text
if curr[2] == 0: # reset the text if we are at the outer level
curr[0] = line[ident:-2]
curr[1] = ""
curr[1] += line[ident:] + "\n"
curr[2] += 1
ident -= 2 # to offset the +2 below, we don't want to increase ident read
else: # we're in array mode,
if line.find("ObjectList") > -1: # enter objectlist mode for the inner level
new = [None, None, 0, []]
listmode = "ObjectList"
elif line.endswith("Array ("): # enter array mode for the inner level
new = []
listmode = True
else: # enter class mode for the inner level
new = {}
listmode = False
stack.append((new, listmode))
curr.append(new)
ident += 2 # increase indentation read
curr = new
assert len(stack) == 1 # when we're done, the stack must have been popped
return curr[0]
def _change_value(self, key, new_val, dic):
if hasattr(dic, 'iteritems'):
for k, v in dic.items():
if k == key:
if type(new_val) != type(v):
raise Exception("Old and new values are not of the same type, {} expects {}".
format(key, type(v)))
else:
if hasattr(new_val, "len") and hasattr(v, "len"):
if len(new_val) != len(v):
raise Exception("Old and new values have different length, {} is {} long".
format(key, len(v)))
else:
dic[k] = new_val
yield dic
else:
dic[k] = new_val
yield dic
if isinstance(v, dict):
for result in self._change_value(key, new_val, v):
yield result
elif isinstance(v, list):
for d in v:
for result in self._change_value(key, new_val, d):
yield result
def _get_value(self, key, dic=None):
# if not dic:
# dic = self.data
if hasattr(dic, 'iteritems'):
for k, v in dic.items():
if k == key:
yield v
if isinstance(v, dict):
for result in self._get_value(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in self._get_value(key, d):
yield result
def _cast_type(self, dic=None, key=None):
if not dic:
dic = self.data
if hasattr(dic, 'iteritems'):
for k, v in dic.items():
if isinstance(v, dict):
self._cast_type(v, k)
elif isinstance(v, list):
c = 0
for s in v:
if isinstance(s, dict):
# print s
self._cast_type(s, k)
else:
dic[k][c] = eval(s)
c += 1
else:
dic[k] = eval(v)
elif isinstance(dic, list):
for s in dic:
self._cast_type(s)
else:
dic[key] = eval(dic)
@staticmethod
def write_json(path, indent=True, sort_keys=True):
try:
with open(path, 'w') as output:
if indent and sort_keys:
json.dump(haddockparams.data, output, indent=4, sort_keys=True)
elif indent:
json.dump(haddockparams.data, output, indent=4)
elif sort_keys:
json.dump(haddockparams.data, output, sort_keys=True)
else:
json.dump(haddockparams.data, output)
except IOError:
print("No such file or directory: {}".format(path))
sys.exit()
except Exception as e:
print("Error while writing the file: {}".format(e))
sys.exit()
def update(self, new_dict, orig_dict=None):
if not orig_dict:
orig_dict = self.data
for key, val in new_dict.items():
if isinstance(val, collections.Mapping):
tmp = self.update(val, orig_dict.get(key, {}))
orig_dict[key] = tmp
elif isinstance(val, list):
orig_dict[key] = (orig_dict.get(key, []) + val)
else:
orig_dict[key] = new_dict[key]
return orig_dict
def change_value(self, key, new_val):
# Check for the key existence first
if not haddockparams.get_value(key):
raise Exception("Key {} not found".format(key))
# Try to change the value (must be a match between the old and new value types)
try:
result = list(haddockparams._change_value(key, new_val, self.data))
if result:
self.data = result[0]
else:
raise Exception("An error was not caught during the update of the dictionary")
except:
raise
def get_value(self, key):
dic = self.data
value = list(haddockparams._get_value(key, dic))
if value:
if len(value) > 1:
return value
else:
return value[0]
else:
raise Exception("Key {} not found".format(key))
def dump_keys(self, d, lvl=0):
for k, v in d.items():
print("{}{}".format(lvl * " ", k))
if type(v) == dict:
self.dump_keys(v, lvl+1)
parser = argparse.ArgumentParser(description="This script parses a HADDOCK parameter file (*.web) and transforms it to "
"JSON format.\n It also allows to change a parameter of the "
"haddockparam.web")
parser.add_argument("web", nargs=1, help="HADDOCK parameter file")
parser.add_argument("-o", "--output", nargs=1, help="Path of JSON output file")
parser.add_argument("-g", "--get", nargs=1, help="Get value of a particular parameter")
parser.add_argument("-e", "--example", nargs="?", help="Print an example")
args = parser.parse_args()
if os.path.exists(args.web[0]):
haddockparams = HaddockParamWeb(args.web[0])
if args.get:
haddockparams.get_value(args.get[0])
if args.output:
haddockparams.write_json(args.output[0])
if args.example:
if int(args.example) == 1:
# EXAMPLE 1 - change waterrefine parameter from 400 to 200
print(haddockparams.get_value('hot'))
haddockparams.data['dan1']['constants']['stages']['hot'] = 10
print(haddockparams.get_value('hot'))
elif int(args.example) == 2:
# EXAMPLE 2 - change waterrefine param with key/value arguments
print(haddockparams.get_value('waterrefine'))
haddockparams.change_value('waterrefine', 200)
print(haddockparams.get_value('waterrefine'))
elif int(args.example) == 3:
# EXAMPLE 3 - print all keys of haddockparams.data
print(haddockparams.dump_keys(haddockparams.data))
else:
print("You must choose between examples 1, 2 or 3. (e.g. -e 1)")