-
Notifications
You must be signed in to change notification settings - Fork 4
/
sync-model-notes.py
executable file
·59 lines (55 loc) · 2.06 KB
/
sync-model-notes.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
#!/bin/python3
#
# Copy notes from SMDX files to JSON models
# See: https://github.com/sunspec/models/pull/247
#
import json
from os import listdir, chdir
from xml.etree import ElementTree as ET
chdir('models')
for fn in listdir('smdx'):
if not (fn.startswith('smdx_') and fn.endswith('.xml')):
continue
model_id = int(fn[5:-4], base=10)
dom = ET.parse(f'smdx/{fn}').getroot()
strings = dom.find('strings')
# model notes
model_string = strings.find('model')
model_notes = model_string.findtext('notes')
if model_notes:
model_notes = model_notes.strip()
# point notes
points_notes = {}
symbols_notes = {}
for point in strings.findall('point'):
point_id = point.get('id')
point_notes = point.findtext('notes')
if point_notes:
point_notes = point_notes.strip()
if point_notes:
points_notes[point_id] = point_notes
for symbol in point.findall('symbol'):
symbol_id = symbol.get('id')
symbol_notes = symbol.findtext('notes')
if symbol_notes:
symbol_notes = symbol_notes.strip()
if symbol_notes:
symbols_notes[(point_id, symbol_id)] = symbol_notes
if not (model_notes or points_notes):
continue
with open(f'json/model_{model_id}.json') as fh:
jsondoc = json.load(fh)
if not jsondoc['group'].get('notes', '') and model_notes:
jsondoc['group']['notes'] = model_notes
for point in jsondoc['group']['points']:
point_name = point['name']
point_notes = points_notes.get(point_name, '')
if not point.get('notes', '') and point_notes:
point['notes'] = point_notes
for symbol in point.get('symbols', []):
symbol_name = symbol['name']
symbol_notes = symbols_notes.get((point_name, symbol_name), '')
if not symbol.get('notes', '') and symbol_notes:
symbol['notes'] = symbol_notes
with open(f'json/model_{model_id}.json', 'w') as fh:
json.dump(jsondoc, fh, indent=4)