Skip to content

Commit 2af36d8

Browse files
committed
add a print_sphinx_tables method
1 parent 31d5052 commit 2af36d8

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

util/runparams.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ def print_paramfile(self):
260260
inputs file, with all known parameters and values
261261
"""
262262

263-
keys = list(self.params.keys())
264-
keys.sort()
263+
all_keys = list(self.params.keys())
265264

266265
try:
267266
f = open('inputs.auto', 'w')
@@ -270,34 +269,63 @@ def print_paramfile(self):
270269

271270
f.write('# automagically generated parameter file\n')
272271

273-
current_section = " "
272+
# find all the sections
273+
secs = set([q for (q, _) in [k.split(".") for k in all_keys]])
274+
275+
for sec in sorted(secs):
276+
keys = [q for q in all_keys if q.startswith("{}.".format(sec))]
277+
278+
f.write("\n[{}]\n".format(sec))
279+
280+
for key in keys:
281+
_, option = key.split('.')
274282

275-
for key in keys:
276-
parts = key.split('.')
277-
section = parts[0]
278-
option = parts[1]
279-
280-
if section != current_section:
281-
current_section = section
282-
f.write('\n')
283-
f.write('[' + section + ']\n')
284-
285-
if isinstance(self.params[key], int):
286-
value = '%d' % self.params[key]
287-
elif isinstance(self.params[key], float):
288-
value = '%f' % self.params[key]
289-
else:
290283
value = self.params[key]
291284

292-
if self.param_comments[key] != '':
293-
f.write(option + ' = ' + value + ' ; ' + self.param_comments[key] + '\n')
294-
else:
295-
f.write(option + ' = ' + value + '\n')
285+
if self.param_comments[key] != '':
286+
f.write("{} = {} ; {}\n".format(option, value, self.param_comments[key]))
287+
else:
288+
f.write("{} = {}\n".format(option, value))
289+
290+
f.close()
291+
292+
def print_sphinx_tables(self, outfile="params-sphinx.inc"):
293+
"""Output Sphinx-formatted tables for inclusion in the documentation.
294+
The table columns will be: param, default, description.
295+
296+
"""
297+
298+
all_keys = list(self.params.keys())
299+
300+
try:
301+
f = open(outfile, 'w')
302+
except IOError:
303+
msg.fail("ERROR: unable to open inputs.auto")
304+
305+
# find all the sections
306+
secs = set([q for (q, _) in [k.split(".") for k in all_keys]])
307+
308+
heading = "+=" + 32*"=" + "=+=" + 10*"=" + "=+=" + 50*"=" + "=+" + "\n"
309+
separator = "+-" + 32*"-" + "-+-" + 10*"-" + "-+-" + 50*"-" + "-+" + "\n"
310+
entry = "| {:32} | {:10} | {:50} |\n"
311+
312+
for sec in sorted(secs):
313+
keys = [q for q in all_keys if q.startswith("{}.".format(sec))]
314+
315+
f.write("\n**{}**\n\n".format(sec.strip()))
316+
317+
f.write(separator)
318+
f.write(entry.format("option", "value", "description"))
319+
f.write(heading)
296320

321+
for key in keys:
322+
_, option = key.split('.')
323+
f.write(entry.format(option, self.params[key], self.param_comments[key].strip()))
324+
f.write(separator)
297325
f.close()
298326

299327

300328
if __name__ == "__main__":
301329
rp = RuntimeParameters()
302330
rp.load_params("inputs.test")
303-
rp.print_paramfile()
331+
rp.print_sphinx_tables()

0 commit comments

Comments
 (0)