-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfallback_infos.py
40 lines (36 loc) · 1.71 KB
/
fallback_infos.py
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
fallback_associated_unique_id = 'p_unique_associated_id##80502300-9DE6-4510-8768-EC42B0EF14E6'
fallback_associated_exclude_ids = 'p_exclude_associated_ids##073F5543-C70A-46AA-8529-E05168852D8F'
fallback_associated_map = {
fallback_associated_unique_id: 'repo',
fallback_associated_exclude_ids: ['id'],
'repo': {
'releases': {
fallback_associated_unique_id: 'tagName',
fallback_associated_exclude_ids: ['currentVersion'],
}
}
}
def fallback_if_need(current, previous, associate_map=None):
if associate_map is None:
associate_map = fallback_associated_map
if previous and current and isinstance(previous, type(current)):
if isinstance(current, dict):
for key, value in previous.items():
if key in associate_map.get(fallback_associated_exclude_ids, []):
continue
if key in current and (isinstance(value, (list, dict))):
if current[key]:
fallback_if_need(current[key], value, associate_map.get(key, {}))
else:
print(f'fallback {key}')
current[key] = value
elif key not in current:
print(f'fallback {key}')
current[key] = value
elif isinstance(current, list) and (uid := associate_map.get(fallback_associated_unique_id)):
for value in previous:
if not isinstance(value, dict):
continue
if cur := next((x for x in current if isinstance(x, dict) and x[uid] == value[uid]), None):
fallback_if_need(cur, value, associate_map.get(uid, {}))
return current