forked from embassy-rs/stm32-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_regs.py
executable file
·68 lines (53 loc) · 1.53 KB
/
merge_regs.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
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
#!/usr/bin/env python3
import xmltodict
import yaml
import re
import json
import sys
import os
from glob import glob
def item_key(a):
return int(a["byte_offset"])
def field_key(a):
return int(a["bit_offset"])
def merge_block(origin, new):
for newval in new:
found = False
for val in origin:
if val["name"] == newval["name"] and val["byte_offset"] == newval["byte_offset"]:
found = True
if not found:
origin.append(newval)
origin.sort(key=item_key)
def merge_fields(origin, new):
for newval in new:
found = False
for val in origin:
if val["name"] == newval["name"] and val["bit_offset"] == newval["bit_offset"]:
found = True
if not found:
origin.append(newval)
origin.sort(key=field_key)
def merge_dicts(origin, new):
for k, v in new.items():
if k in origin:
if type(v) is dict:
merge_dicts(origin[k], v)
elif type(v) is list:
if k == "items":
merge_block(origin[k], v)
if k == "fields":
merge_fields(origin[k], v)
else:
origin[k] = v
else:
origin[k] = v
first = True
reg_map = {}
for regfile in sys.argv[1:]:
print("Loading", regfile)
with open(regfile, 'r') as f:
y = yaml.load(f, Loader=yaml.SafeLoader)
merge_dicts(reg_map, y)
with open('regs_merged.yaml', 'w') as f:
f.write(yaml.dump(reg_map))