-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathspec_mapper.py
More file actions
470 lines (395 loc) · 18.4 KB
/
spec_mapper.py
File metadata and controls
470 lines (395 loc) · 18.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Dict, Tuple, Optional, Any
from constants import FrameworkPaths, resolve_framework_config_path
import pipeline_config
import utility
class SpecMapper:
"""
Handles dataflow spec version migrations and key mappings.
Supports operations:
- move: Move a key to a new location
- rename_all: Rename keys recursively throughout the spec
- rename_specific: Rename specific keys at exact paths
- delete: Delete keys from the spec
Each operation supports conditional execution based on spec properties.
"""
class Keys:
"""Constants for spec mapping keys."""
DATA = "data"
DATA_FLOW_ID = "dataFlowId"
DATA_FLOW_TYPE = "dataFlowType"
DATA_FLOW_VERSION = "dataFlowVersion"
GLOBAL = "global"
class Operators:
"""Constants for condition operators."""
EQUAL_TO = "equal_to"
NOT_EQUAL_TO = "not_equal_to"
IN = "in"
NOT_IN = "not_in"
def __init__(self, framework_path: str, max_workers: int = 1):
"""
Initialize the SpecMapper.
Args:
framework_path: Path to the framework directory
max_workers: Maximum parallel workers for processing
"""
self.framework_path = framework_path
self._framework_config_path = resolve_framework_config_path(framework_path)
self.max_workers = max_workers
self._mapping_cache: Dict[str, Dict] = {}
self.logger = pipeline_config.get_logger()
self.validator = utility.JSONValidator(
os.path.join(self.framework_path, FrameworkPaths.SPEC_MAPPING_SCHEMA_PATH)
)
def apply_mappings(
self,
specs: Dict,
global_version: Optional[str] = None,
ignore_errors: bool = False
) -> Dict:
"""
Apply version mappings to specs using parallel processing.
Args:
specs: Dictionary of spec_path -> spec_payload
global_version: Global mapping version to apply
ignore_errors: If True, continue processing on errors
Returns:
Dictionary of processed specs
"""
self.logger.info(
"Spec Mapper - Applying Mappings:\n"
f" Max Workers: {self.max_workers}\n"
f" Global Dataflow Spec Version (optional): {global_version}"
f" Spec Count: {len(specs) if specs else 0}"
)
if not specs:
return specs
# Process specs in parallel
results = {}
errors = {}
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
future_to_path = {
executor.submit(
self._apply_mapping_to_spec,
spec_path,
spec_payload,
global_version
): spec_path
for spec_path, spec_payload in specs.items()
}
for future in as_completed(future_to_path):
spec_path, spec_payload, error = future.result()
if error:
errors[spec_path] = error
self.logger.warning(f"Failed to apply mapping for {spec_path}: {error}")
results[spec_path] = spec_payload
if errors and not ignore_errors:
self.logger.warning(f"Some specs failed during mapping: {errors}")
return results
def get_mapping(self, version: str) -> Dict:
"""
Load, validate, and cache mapping configuration for a version.
Args:
version: Version string (e.g., "0.2.0")
Returns:
Mapping configuration dictionary
"""
if version in self._mapping_cache:
return self._mapping_cache[version]
mapping_path = os.path.join(
self.framework_path,
self._framework_config_path,
FrameworkPaths.DATAFLOW_SPEC_MAPPING,
version,
"dataflow_spec_mapping.json"
)
try:
mapping = utility.get_json_from_file(mapping_path, True)
errors = self.validator.validate(mapping)
if errors:
raise ValueError(f"Spec mapping validation failed for {mapping_path}: {errors}")
self._mapping_cache[version] = mapping
return mapping
except Exception as e:
msg = f"Error loading spec mapping version {version}: {str(e)}"
self.logger.error(msg)
raise FileNotFoundError(msg) from e
def _apply_mapping_to_spec(
self,
spec_path: str,
spec_payload: Dict,
global_version: Optional[str]
) -> Tuple[str, Dict, Optional[str]]:
"""
Apply mapping to a single spec.
Returns:
Tuple of (spec_path, updated_payload, error_message)
"""
try:
spec_id = spec_payload.get(self.Keys.DATA_FLOW_ID, "").strip().lower()
spec_type = spec_payload.get(self.Keys.DATA_FLOW_TYPE, "").strip().lower()
spec_data = spec_payload.get(self.Keys.DATA)
# Get mapping configuration
mapping = {}
if global_version:
mapping = self.get_mapping(global_version)
# Check for spec-specific version override
spec_version = spec_data.get(self.Keys.DATA_FLOW_VERSION)
if spec_version:
mapping = self.get_mapping(spec_version)
self.logger.info(
f"Using spec-specific mapping version {spec_version} for {spec_id}"
)
if not mapping:
return spec_path, spec_payload, None
# Merge global and spec-type specific mappings
global_mappings = mapping.get(self.Keys.GLOBAL, {})
spec_type_mappings = mapping.get(spec_type, {})
key_mappings = {**global_mappings, **spec_type_mappings}
if key_mappings:
spec_data = self._apply_operations(spec_data, key_mappings, spec_path)
spec_payload[self.Keys.DATA] = spec_data
return spec_path, spec_payload, None
except Exception as e:
return spec_path, spec_payload, str(e)
def _apply_operations(
self,
spec_data: Dict,
mappings: Dict,
spec_path: str
) -> Dict:
"""
Apply all mapping operations to spec data.
Operations are applied in order:
1. move - Copy values to new locations
2. rename_specific - Rename specific key paths
3. rename_all - Recursively rename keys
4. delete - Remove keys (including moved source keys)
"""
rename_all_ops = mappings.get("rename_all", {})
rename_specific_ops = mappings.get("rename_specific", {})
move_ops = mappings.get("move", {})
delete_ops = mappings.get("delete", {})
moved_keys = []
self.logger.debug(f"Applying mapping to spec: {spec_path}")
# 1. Move operations (recursive)
if move_ops:
self.logger.debug(f"Applying move operations: {move_ops}")
for src, dest_config in move_ops.items():
# Parse the destination and condition from config
if isinstance(dest_config, str):
dest = dest_config
condition = None
elif isinstance(dest_config, dict):
dest = dest_config.get("to")
condition = dest_config.get("condition")
else:
continue
if dest:
# Apply move recursively throughout the spec
spec_data = self._move_key_recursive(spec_data, src, dest, condition)
moved_keys.append(src)
# 2. Rename specific operations
if rename_specific_ops:
self.logger.debug(f"Applying rename specific operations: {rename_specific_ops}")
for src, dest_config in rename_specific_ops.items():
dest, should_apply = self._parse_conditional_operation(dest_config, spec_data)
if should_apply and dest:
self._rename_key_specific(spec_data, src, dest)
elif not should_apply:
self.logger.debug(f"Skipping rename '{src}' - condition not met")
# 3. Rename all operations (recursive)
if rename_all_ops:
self.logger.debug(f"Applying rename all operations: {rename_all_ops}")
filtered_rename_all = {}
for src, dest_config in rename_all_ops.items():
dest, should_apply = self._parse_conditional_operation(dest_config, spec_data)
if should_apply and dest:
filtered_rename_all[src] = dest
if filtered_rename_all:
spec_data = self._rename_keys_recursive(spec_data, filtered_rename_all)
# 4. Delete moved source keys (already handled by _move_key_recursive)
# The recursive move operation removes the source key as part of the move
# 5. Delete explicit keys
if delete_ops:
self.logger.debug(f"Applying delete operations: {delete_ops}")
for key, delete_config in delete_ops.items():
if isinstance(delete_config, bool) and delete_config:
self._delete_key(spec_data, key)
elif isinstance(delete_config, dict):
_, should_apply = self._parse_conditional_operation(delete_config, spec_data)
if should_apply:
self._delete_key(spec_data, key)
else:
self._delete_key(spec_data, key)
self.logger.info(f"Mapping applied to spec: {spec_path}")
self.logger.info(f"Mapped spec: {spec_data}")
return spec_data
# Condition Evaluation
def _parse_conditional_operation(
self,
operation_value: Any,
data: Dict
) -> Tuple[Optional[str], bool]:
"""
Parse an operation value that may be simple or conditional.
Simple format: "targetDetails.newKey"
Conditional format: {"to": "...", "condition": {...}}
Returns:
Tuple of (target_value, should_apply)
"""
if isinstance(operation_value, str):
return operation_value, True
elif isinstance(operation_value, dict):
target = operation_value.get("to")
condition = operation_value.get("condition")
should_apply = self._evaluate_condition(data, condition)
return target, should_apply
else:
self.logger.warning(f"Invalid operation value format: {operation_value}")
return None, False
def _evaluate_condition(self, data: Dict, condition: Optional[Dict]) -> bool:
"""
Evaluate a condition against spec data.
Condition format:
{
"key": "sourceType", # dot notation path
"operator": "not_equal_to", # equal_to, not_equal_to, in, not_in
"value": "python" # comparison value
}
"""
if not condition:
return True
key_path = condition.get("key", "")
operator = condition.get("operator", self.Operators.EQUAL_TO)
expected_value = condition.get("value")
actual_value = self._get_nested_value(data, key_path)
if operator == self.Operators.EQUAL_TO:
return actual_value == expected_value
elif operator == self.Operators.NOT_EQUAL_TO:
return actual_value != expected_value
elif operator == self.Operators.IN:
if not isinstance(expected_value, list):
raise ValueError(f"Spec mapping error: Invalid expected value type: {type(expected_value)} specified in condition: {condition}. Expected list.")
return actual_value in expected_value
elif operator == self.Operators.NOT_IN:
if not isinstance(expected_value, list):
raise ValueError(f"Spec mapping error: Invalid expected value type: {type(expected_value)} specified in condition: {condition}. Expected list.")
return actual_value not in expected_value
else:
raise ValueError(f"Spec mapping error: Unknown condition operator: {operator} specified in condition: {condition}.")
# Key Operations
@staticmethod
def _get_nested_value(data: Dict, key_path: str) -> Any:
"""Get a value from a nested dict using dot notation."""
parts = key_path.split(".")
current = data
for part in parts:
if isinstance(current, dict) and part in current:
current = current[part]
else:
return None
return current
@staticmethod
def _get_parent_and_key(data: Dict, path: list, create_missing: bool = True) -> Tuple[Optional[Dict], str]:
"""
Return the parent dict and final key name for a path.
Args:
data: The dictionary to traverse
path: List of keys representing the path
create_missing: If True, create intermediate dicts. If False, return None for parent if path doesn't exist.
Returns:
Tuple of (parent_dict, final_key). parent_dict is None if create_missing=False and path doesn't exist.
"""
current = data
for k in path[:-1]:
if create_missing:
current = current.setdefault(k, {})
else:
if not isinstance(current, dict) or k not in current:
return None, path[-1]
current = current[k]
return current, path[-1]
def _move_key_recursive(
self,
obj: Any,
src_path: str,
dest_path: str,
condition: Optional[Dict],
context: Optional[Dict] = None
) -> Any:
"""
Recursively move keys throughout the spec structure.
For a src_path like "sourceDetails.pythonFunctionPath" and dest_path
"sourceDetails.pythonTransform.functionPath", this will find ALL occurrences
of sourceDetails containing pythonFunctionPath and move them.
The condition is evaluated against the parent context (e.g., the object
containing sourceDetails, which also has sourceType).
"""
if isinstance(obj, dict):
src_parts = src_path.split(".")
dest_parts = dest_path.split(".")
result = {}
for key, value in obj.items():
# Check if this is a match: key starts path, value is dict with source key, condition met
if (key == src_parts[0] and
isinstance(value, dict) and
len(src_parts) == 2 and
src_parts[1] in value and
self._evaluate_condition(obj, condition)):
# Transform: remove source key and create nested destination
new_value = {k: v for k, v in value.items() if k != src_parts[1]}
current = new_value
for dest_key in dest_parts[1:-1]:
current = current.setdefault(dest_key, {})
current[dest_parts[-1]] = value[src_parts[1]]
result[key] = self._move_key_recursive(new_value, src_path, dest_path, condition, obj)
else:
result[key] = self._move_key_recursive(value, src_path, dest_path, condition, obj)
return result
if isinstance(obj, list):
return [self._move_key_recursive(item, src_path, dest_path, condition, context) for item in obj]
return obj
def _move_key(self, data: Dict, src_path: str, dest_path: str) -> None:
"""Move a key and its value from src_path to dest_path (non-recursive, for root-level moves)."""
src_parts = src_path.split(".")
dest_parts = dest_path.split(".")
# Check source exists WITHOUT creating intermediate dicts
src_parent, src_key = self._get_parent_and_key(data, src_parts, create_missing=False)
if src_parent is None or src_key not in src_parent:
return
value = src_parent[src_key]
# Create destination path (this is intentional)
dest_parent, dest_key = self._get_parent_and_key(data, dest_parts, create_missing=True)
dest_parent[dest_key] = value
def _rename_key_specific(self, data: Dict, current_key: str, new_key: str) -> None:
"""Rename a specific key at an exact path."""
current_parts = current_key.split(".")
# Check source exists WITHOUT creating intermediate dicts
parent, key = self._get_parent_and_key(data, current_parts, create_missing=False)
if parent is None or key not in parent:
return
new_key_name = new_key.split(".")[-1]
parent[new_key_name] = parent.pop(key)
def _rename_keys_recursive(self, obj: Any, key_mapping: Dict) -> Any:
"""Recursively rename keys in nested dictionaries."""
if isinstance(obj, dict):
renamed_obj = {}
for key, value in obj.items():
new_key = key_mapping.get(key, key)
if new_key != key:
self.logger.debug(f"Renaming key '{key}' to '{new_key}'")
renamed_obj[new_key] = self._rename_keys_recursive(value, key_mapping)
return renamed_obj
elif isinstance(obj, list):
return [self._rename_keys_recursive(item, key_mapping) for item in obj]
else:
return obj
def _delete_key(self, data: Dict, src_path: str) -> None:
"""Delete a key and its value from a nested dict."""
src_parts = src_path.split(".")
# Check source exists WITHOUT creating intermediate dicts
src_parent, src_key = self._get_parent_and_key(data, src_parts, create_missing=False)
if src_parent is not None and src_key in src_parent:
src_parent.pop(src_key)