Skip to content

Commit

Permalink
Merge pull request #10064 from gem/to_ini
Browse files Browse the repository at this point in the history
Fixed OqParam.to_ini()
  • Loading branch information
micheles authored Oct 17, 2024
2 parents 7d6944f + 24ff1a2 commit 5e2cfa3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions openquake/calculators/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def check_ini(path, hc):
oq = readinput.get_oqparam(dic)
ini = oq.to_ini()
tmp_ini = path[:-3] + 'tmp.ini'
print('Saving', tmp_ini)
with open(tmp_ini, 'w') as f:
f.write(ini)
dic2 = readinput.get_params(tmp_ini)
Expand All @@ -136,4 +137,5 @@ def check_inis(demo_dir):


if __name__ == '__main__':
# called by run-demos.sh
check_inis(sys.argv[1])
26 changes: 16 additions & 10 deletions openquake/commonlib/oqvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import json
import inspect
import logging
import pathlib
import functools
import collections
import numpy
Expand Down Expand Up @@ -2225,15 +2226,15 @@ def docs(cls):
dic[name] = doc
return dic

# tested in geese; expected to work for the hazard mosaic
# tested in run-demos.sh
def to_ini(self):
"""
Converts the parameters into a string in .ini format
"""
dic = {k: v for k, v in vars(self).items() if not k.startswith('_')}
del dic['base_path']
del dic['req_site_params']
#dic.pop('export_dir', None)
dic.pop('export_dir', None)
dic.pop('all_cost_types', None)
if 'secondary_perils' in dic:
dic['secondary_perils'] = ' '.join(dic['secondary_perils'])
Expand Down Expand Up @@ -2270,14 +2271,20 @@ def __fromh5__(self, array, attrs):
self.maximum_distance = Idist(**self.maximum_distance)


def _rel_fnames(obj, P):
# strip the first P characters and convert to relative paths
def _rel_fnames(obj, base):
# convert to relative paths
if isinstance(obj, str):
return obj[P:]
*b, n = pathlib.Path(obj).parts
offset = len(base) - len(b)
if offset > 0:
relpath = ['..'] * offset + [n]
else:
relpath = b[len(base):] + [n]
return '/'.join(relpath)
elif isinstance(obj, list):
return '\n '.join(s[P:] for s in obj)
return '\n '.join(_rel_fnames(s, base) for s in obj)
else: # assume dict
dic = {k: v[P:] for k, v in obj.items()}
dic = {k: _rel_fnames(v, base) for k, v in obj.items()}
return str(dic)


Expand All @@ -2286,6 +2293,7 @@ def to_ini(key, val):
Converts key, val into .ini format
"""
if key == 'inputs':
*base, _name = pathlib.Path(val.pop('job_ini')).parts
fnames = []
for v in val.values():
if isinstance(v, str):
Expand All @@ -2294,9 +2302,7 @@ def to_ini(key, val):
fnames.extend(v)
elif isinstance(v, dict):
fnames.extend(v.values())
del val['job_ini']
P = len(os.path.commonprefix(fnames))
return '\n'.join(f'{k}_file = {_rel_fnames(v, P)}'
return '\n'.join(f'{k}_file = {_rel_fnames(v, base)}'
for k, v in val.items()
if not k.startswith('_'))
elif key == 'sites':
Expand Down

0 comments on commit 5e2cfa3

Please sign in to comment.