-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctions.py
More file actions
231 lines (186 loc) · 8.22 KB
/
Copy pathfunctions.py
File metadata and controls
231 lines (186 loc) · 8.22 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
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
"""Public, free-standing functions for PALS."""
import os
def inspect_file_extensions(filename: str, check_extension: bool = True):
"""Attempt to strip two levels of file extensions to determine the schema.
filename examples: fodo.pals.yaml, fodo.pals.json, ...
"""
file_noext, extension = os.path.splitext(filename)
file_noext_noext, extension_inner = os.path.splitext(file_noext)
if check_extension and extension_inner != ".pals":
raise RuntimeError(
f"inspect_file_extensions: No support for file {filename} with extension {extension}. "
f"PALS files must end in .pals.json or .pals.yaml or similar."
)
return {
"file_noext": file_noext,
"extension": extension,
"file_noext_noext": file_noext_noext,
"extension_inner": extension_inner,
}
def process_includes(data, base_dir: str):
"""Recursively process 'include' directives in the data structure."""
if isinstance(data, dict):
# Handle 'include' key in dictionary
if "include" in data:
include_file = data["include"]
# Check if include_file is a string (filename)
if isinstance(include_file, str):
filepath = os.path.join(base_dir, include_file)
# Load included file without strict extension check
included_data = load_file_to_dict(filepath, check_extension=False)
# Remove 'include' key
local_data = data.copy()
del local_data["include"]
# Recursively process local data
local_data = {
k: process_includes(v, base_dir) for k, v in local_data.items()
}
# Merge logic
# If included data is a list of single-key dicts (PALS special case), try to merge as dict
if isinstance(included_data, list):
try:
merged_included = {}
all_dicts = True
for item in included_data:
if isinstance(item, dict) and len(item) == 1:
merged_included.update(item)
else:
all_dicts = False
break
if all_dicts:
included_data = merged_included
except Exception:
pass
if isinstance(included_data, dict):
# Merge included data with local data (local overrides included?)
# Spec: "Included file data will be included verbatim at the current level of nesting."
# Usually specific (local) overrides generic (included).
# So we take included, update with local.
result = included_data.copy()
result.update(local_data)
return result
else:
# If included data is not a dict, we can't merge it into a dict.
# Unless the dict was JUST the include?
if not local_data:
return included_data
# Fallback: return local data (ignore include) or error?
# For now, let's return local_data but maybe warn?
# Or maybe return included_data if local_data is empty?
return local_data
# Recurse on values if no include or after processing
return {k: process_includes(v, base_dir) for k, v in data.items()}
elif isinstance(data, list):
new_list = []
for item in data:
# Check if item is a dict with ONLY 'include' key
if isinstance(item, dict) and "include" in item and len(item) == 1:
include_file = item["include"]
if isinstance(include_file, str):
filepath = os.path.join(base_dir, include_file)
included_data = load_file_to_dict(filepath, check_extension=False)
if isinstance(included_data, list):
new_list.extend(included_data)
else:
new_list.append(included_data)
else:
new_list.append(process_includes(item, base_dir))
else:
new_list.append(process_includes(item, base_dir))
return new_list
else:
return data
def load_file_to_dict(filename: str, check_extension: bool = True) -> dict:
# Attempt to strip two levels of file extensions to determine the schema.
# Examples: fodo.pals.yaml, fodo.pals.json, ...
file_noext, extension, file_noext_noext, extension_inner = inspect_file_extensions(
filename, check_extension=check_extension
).values()
# examples: fodo.pals.yaml, fodo.pals.json
with open(filename, "r") as file:
if extension == ".json":
import json
pals_data = json.loads(file.read())
elif extension == ".yaml":
import yaml
pals_data = yaml.safe_load(file)
# TODO: toml, xml
else:
raise RuntimeError(
f"load_file_to_dict: No support for PALS file {filename} with extension {extension} yet."
)
# Process includes
pals_data = process_includes(pals_data, base_dir=os.path.dirname(filename))
return pals_data
def _numpy_to_native(obj):
"""Convert a numpy scalar/array to its Python-native equivalent.
Returns ``None`` when the object is not a numpy type or when numpy is not
installed; callers use that to decide whether to fall back to the default
serializer behavior. numpy is an optional dependency.
"""
try:
import numpy as np
except ImportError:
return None
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, np.generic):
return obj.item()
return None
def store_dict_to_file(filename: str, pals_dict: dict):
file_noext, extension, file_noext_noext, extension_inner = inspect_file_extensions(
filename
).values()
# examples: fodo.pals.yaml, fodo.pals.json
if extension == ".json":
import json
def _json_default(obj):
native = _numpy_to_native(obj)
if native is not None:
return native
raise TypeError(
f"Object of type {type(obj).__name__} is not JSON serializable"
)
json_data = json.dumps(
pals_dict, sort_keys=False, indent=2, default=_json_default
)
with open(filename, "w") as file:
file.write(json_data)
elif extension == ".yaml":
import yaml
# Subclass the safe dumper so numpy representers are scoped to PALS
# serialization and do not leak into the global pyyaml state used by
# other code in the same process.
class _PALSDumper(yaml.SafeDumper):
pass
try:
import numpy as np
except ImportError:
np = None
if np is not None:
def _represent_numpy_scalar(dumper, value):
native = value.item()
if isinstance(native, bool):
return dumper.represent_bool(native)
if isinstance(native, int):
return dumper.represent_int(native)
if isinstance(native, float):
return dumper.represent_float(native)
return dumper.represent_data(native)
def _represent_numpy_array(dumper, value):
return dumper.represent_list(value.tolist())
_PALSDumper.add_multi_representer(np.generic, _represent_numpy_scalar)
_PALSDumper.add_representer(np.ndarray, _represent_numpy_array)
yaml_data = yaml.dump(
pals_dict,
Dumper=_PALSDumper,
default_flow_style=False,
sort_keys=False,
)
with open(filename, "w") as file:
file.write(yaml_data)
# TODO: toml, xml
else:
raise RuntimeError(
f"store_dict_to_file: No support for PALS file {filename} with extension {extension} yet."
)