-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema-maker.py
145 lines (129 loc) · 4.47 KB
/
schema-maker.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
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
import re
md_file = 'data-model.md'
chunks = re.compile(r'^([A-Za-z ]+?)[ \t]*\n(?:[: ] +([^\n]*)\n)+', re.MULTILINE)
links = re.compile(r' `\[([^\]]*)\]`')
def curie(s):
return s#.replace('elf:','https://terms.fhiso.org/elf/').replace('elfm:','https://terms.fhiso.org/elf/metadata/')
themap = {}
with open(md_file) as f:
txt = f.read()
bits = re.split(r' *###+ *',txt)
for bit in bits:
name = bit[:bit.find('{#')].strip()[1:-1]
if ' ' in name: continue
isa = []
within = []
contains = []
canbe = []
tag = []
parts = {}
for chunk in chunks.finditer(bit):
if chunk.group(1).startswith('Supertype'):
isa.extend(links.findall(chunk.group()))
if chunk.group(1).startswith('Superstructure'):
within.extend(links.findall(chunk.group()))
if chunk.group(1).startswith('Default tag'):
tag.extend(re.findall(r'`([^`\n]*)`', chunk.group()))
if chunk.group(1).startswith('Subtype'):
canbe.extend(links.findall(chunk.group()))
if chunk.group(1).startswith('Substructure'):
contains.extend(links.findall(chunk.group()))
parts[chunk.group(1)] = chunk.group()
themap[name] = {
'isa': isa,
'within': within,
'contains': contains,
'canbe': canbe,
'tag': tag,
'parts': parts,
'bit': bit
}
if 0:
print('\n\n###',bit)
if tag is not None:
print('''
Schema fragment
: `1 SCHMA`
: `2 IRI {}`
: `3 TAG {} {}`'''.format(
curie(name),
tag,
' '.join(curie(_) for _ in within)
)
)
from sys import stderr, argv
missing = set()
for k,v in themap.items():
for p in v['isa']:
if p not in themap:
missing.add(p)
else:
if k not in themap[p]['canbe']:
print('Unlisted subtype:',p,'can be a',k, file=stderr)
for p in v['canbe']:
if p not in themap:
missing.add(p)
else:
if k not in themap[p]['isa']:
print('Unlisted supertype:',p,'can be a',k, file=stderr)
for p in v['within']:
if p not in themap:
missing.add(p)
else:
if k not in themap[p]['contains']:
print('Unlisted element:',p,'can contain',k, file=stderr)
for p in v['contains']:
if p not in themap:
missing.add(p)
else:
if k not in themap[p]['within']:
print('Unlisted element:',p,'can be within',k, file=stderr)
print('missing tags:', missing, file=stderr)
for i in range(3): # short-circuit fixed point
for k,v in themap.items():
for sup in v['isa']:
if sup in themap:
v['within'] = list(sorted(set(v['within'] + themap[sup]['within'])))
if 1:
import datetime
print('''0 HEAD
1 CHAR UTF-8
1 GEDC
2 VERS 5.5.1
2 FORM LINEAGE-LINKED
2 ELF 1.0.0
1 SCHMA
2 PRFX elf https://terms.fhiso.org/elf/
2 PRFX elfm https://terms.fhiso.org/elf/metadata/
2 ESC DATE D''')
for k in sorted(themap):
if ':' in k:
print('2 IRI', k)
for sup in sorted(themap[k]['isa']):
if sup != 'elf:Structure':
print('3 ISA', sup)
for tag in themap[k].get('tag', []):
print('3 TAG', tag, ('\n4 CONT ' if False else ' ').join(sorted(themap[k]['within'])))
print('''1 SOUR https://fhiso.org/elf/
1 NOTE This file was automatically generated from '''+md_file+'''
2 CONT by '''+argv[0]+''' at '''+datetime.datetime.utcnow().isoformat()+'''
1 SUBM @fhiso_elf1@
0 @fhiso_elf1@ SUBM
1 NAME FHISO Extended Legacy Format, version 1
0 TRLR''')
elif 1:
print('| {:36} | {:30} | {:5} |'.format('$I$', '$S$', '$T$'))
print('|:'+ '-'*36 +'-|:'+ '-'*30 +'-|:'+ '-'*5 +'-|')
for k in sorted(themap):
if 'tag' in themap[k] and themap[k]['tag'] is not None:
for sup in sorted(themap[k]['within']):
print('| {:36} | {:30} | {:5} |'.format(k, sup, themap[k]['tag']))
else:
tis = []
for k in sorted(themap):
if 'tag' in themap[k] and themap[k]['tag'] is not None:
for sup in sorted(themap[k]['within']):
tis.append((themap[k]['tag'], k, sup))
tis.sort()
for t,i,s in tis:
print(t,i)