Repository navigation
Expand file tree
/
Copy pathmur_config.py
More file actions
116 lines (95 loc) · 4.75 KB
/
Copy pathmur_config.py
File metadata and controls
116 lines (95 loc) · 4.75 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
"""Config loading and key-spelling normalization shared by
run_mur_pipeline.py (local `docker run` executor) and run_mur_maap.py
(MAAP/DPS executor).
The two orchestrators grew independently and read three settings under
different names:
concept pipeline spelling MAAP spelling
------------------------------------------------------------------
static-resources root static_resources_dir static_resources_root
iQuam +/- window buoy_dayrange buoy_day_range
iQuam stability stable_latency stability_latency
Only the pipeline spellings appear in the live config files (config.json,
config.example.json, config.container.json); only the MAAP spellings appear
in run_mur_maap.py and its test fixture. Rather than a flag-day rename of
both, normalize_config() accepts either and emits the canonical (live-config)
spelling, leaving every other key untouched.
This also closes a latent KeyError: run_mur_maap.py reads
config["landice"]["static_resources_root"], which exists in no config file
on disk, so the first real MAAP run would have raised before submitting
anything.
"""
import hashlib
import json
import pathlib
from typing import Any, Dict, Tuple
# (section, canonical_key, alias_key). The canonical spelling is the one the
# live config files already ship, so no existing config needs editing.
_KEY_ALIASES: Tuple[Tuple[str, str, str], ...] = (
("landice", "static_resources_dir", "static_resources_root"),
("mrva", "static_resources_dir", "static_resources_root"),
("iquam", "buoy_dayrange", "buoy_day_range"),
("iquam", "stable_latency", "stability_latency"),
)
def normalize_config(raw: Dict[str, Any]) -> Dict[str, Any]:
"""Return a copy of `raw` with alias keys folded onto canonical ones.
Non-destructive: unknown keys pass through untouched, and an alias is
only applied when the canonical key is absent, so a config that
(confusingly) sets both keeps the canonical value.
"""
config = json.loads(json.dumps(raw)) # deep copy; config is plain JSON
for section, canonical, alias in _KEY_ALIASES:
block = config.get(section)
if not isinstance(block, dict):
continue
if canonical not in block and alias in block:
block[canonical] = block[alias]
_default_static_resources(config)
return config
# Where static resources live relative to maap.workspace_root, unless a
# section names its own static_resources_dir.
STATIC_RESOURCES_SUBDIR = "mur/static-resources"
def _default_static_resources(config: Dict[str, Any]) -> None:
"""Fill landice/mrva static_resources_dir from maap.workspace_root.
So that moving the MAAP data -- personal workspace to a team bucket, or
back -- is one edit to workspace_root, not three that must agree. An
explicit static_resources_dir still wins; a config with no MAAP root
(the local pipeline's) is untouched.
"""
root = str((config.get("maap") or {}).get("workspace_root") or "").rstrip("/")
if not root.startswith("s3://"):
return
for section in ("landice", "mrva"):
block = config.setdefault(section, {})
if isinstance(block, dict) and not block.get("static_resources_dir"):
block["static_resources_dir"] = f"{root}/{STATIC_RESOURCES_SUBDIR}"
def get(config: Dict[str, Any], section: str, key: str, default: Any = None) -> Any:
"""Read a possibly-aliased setting without caring which spelling is present.
Prefer normalize_config() at load time; this is for the callers that
receive an already-parsed dict they did not load (e.g. MAAPOrchestrator,
whose __init__ takes a config rather than a path).
"""
block = config.get(section) or {}
if key in block:
return block[key]
for sec, canonical, alias in _KEY_ALIASES:
if sec != section:
continue
if key == canonical and alias in block:
return block[alias]
if key == alias and canonical in block:
return block[canonical]
return default
def load_config(path) -> Dict[str, Any]:
"""Load a pipeline config JSON file and normalize its key spellings."""
with pathlib.Path(path).open() as f:
return normalize_config(json.load(f))
def config_fingerprint(config: Dict[str, Any]) -> str:
"""Stable sha256 over a config's normalized content.
A resumed MAAP run must refuse to continue against an edited config:
changing a sensor's day_range or the static-resources root mid-run
silently changes what the already-submitted jobs mean. Key *order* must
not affect the result, so this sorts keys.
"""
normalized = normalize_config(config)
payload = json.dumps(normalized, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(payload.encode("utf-8")).hexdigest()