Load reduction config file from dirname or its parent as tool for RpLBins.
dirname is a Path.
Returns:
| Type |
Description |
|
|
- None if not present (not an error)
|
|
|
- The dictionary, if it is present
|
Source code in reduce_drm_tools/utils.py
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 | def load_reduce_config(dirname, log_origin=None):
'''Load reduction config file from dirname or its parent as tool for RpLBins.
dirname is a Path.
Returns:
- None if not present (not an error)
- The dictionary, if it is present
'''
if log_origin is None:
log_string = 'PlanetBins.py'
else:
log_string = log_origin
fn = dirname / REDUCTION_CONFIG
# OK for it not to exist
if not (fn.is_file() and os.access(fn, os.R_OK)):
dirname_alt = dirname.parent
if dirname_alt.suffix in ('.fam', '.exp') and dirname_alt.is_dir():
# look one level up
fn = dirname_alt / REDUCTION_CONFIG
if not (fn.is_file() and os.access(fn, os.R_OK)):
return None
else:
pass # fn is readable -- continue
else:
return None
# Below here: fn is a readable file, either in dirname or dirname.parent
# If fn exists, it's an error for it to not load as a mapping
try:
with open(fn, 'r') as fp:
d = json.load(fp)
except FileNotFoundError:
print(f'{log_origin}: Error: Reduction configuration ({fn}) exists but unreadable.')
raise
except json.JSONDecodeError:
print(f'{log_origin}: Error: Could not read JSON in {fn}')
raise
if not isinstance(d, dict):
print(f'{log_origin}: Error: Reduction configuration ({fn}) is not a mapping.')
raise ValueError("Reduction configuration was not a mapping.")
# record where it came from
# str() is the relative path starting from sims/
d['_config_filename'] = str(fn)
return d
|