]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-item-identifiers.py
Register final Variant Reification original for Slice 22B
[topic-maps.git] / scripts / check-item-identifiers.py
1 #!/usr/bin/env python3
2 """Independent XML/baseline checks for hand-built TMDM models, NOT an XTM reader."""
3 import json
4 import pathlib
5 import sys
6 import xml.etree.ElementTree as ET
7
8 ROOT = pathlib.Path(__file__).resolve().parent.parent
9 report = json.loads(pathlib.Path(sys.argv[1]).read_text())
10 assert report['kind'] == 'general-item-identifier-evidence' and report['failed'] == 0
11 assert report['passed'] == len(report['checks']) == len(set(report['checks']))
12 stems = ['itemid-tm', 'itemid', 'itemid-name', 'itemid-variant', 'itemid-occurrence',
13 'itemid-association', 'itemid-role']
14 assert [c['source'] for c in report['examples']] == ['xtm2/in/' + n + '.xtm' for n in stems]
15 for case in report['examples']:
16 assert case['contract'] == 'Manual TMDM construction; NOT XTM deserialization'
17 source = ET.parse(ROOT / 'vendor/cxtm' / case['source']).getroot()
18 assert source.tag == '{http://www.topicmaps.org/xtm/}topicMap'
19 expected = (ROOT / 'vendor/cxtm' / case['baseline']).read_bytes()
20 actual = case['actualCXTM'].encode('utf-8')
21 assert actual == expected and len(actual) == case['bytes'], case['source']
22
23 root = ET.fromstring(report['orderingCXTM'])
24 positions = {
25 'topicMap': ['itemIdentifiers', 'topic', 'association'],
26 'topic': ['subjectIdentifiers', 'subjectLocators', 'itemIdentifiers', 'name', 'occurrence', 'rolePlayed'],
27 'name': ['value', 'type', 'scope', 'variant', 'itemIdentifiers'],
28 'variant': ['value', 'datatype', 'scope', 'itemIdentifiers'],
29 'occurrence': ['value', 'datatype', 'type', 'scope', 'itemIdentifiers'],
30 'association': ['type', 'role', 'scope', 'itemIdentifiers'],
31 'role': ['player', 'type', 'itemIdentifiers'],
32 }
33 kinds = dict(topicMap='TOPIC-MAP', topic='TOPIC', name='TOPIC-NAME', variant='VARIANT',
34 occurrence='OCCURRENCE', association='ASSOCIATION', role='ASSOCIATION-ROLE')
35 found = set()
36 topics = {t.attrib['number'] for t in root.findall('topic')}
37 for item in root.iter():
38 if item.tag in positions:
39 order = [positions[item.tag].index(c.tag) for c in item]
40 assert order == sorted(order), (item.tag, order)
41 ids = item.find('itemIdentifiers')
42 if ids is not None:
43 kind = kinds[item.tag]
44 assert [l.text for l in ids] == [f'urn:{kind}:a', f'urn:{kind}:z']
45 found.add(item.tag)
46 if 'topicref' in item.attrib:
47 assert item.attrib['topicref'] in topics
48 assert 'reifier' not in item.attrib
49 assert found == set(positions)
50 print('ITEM-IDENTIFIERS-INDEPENDENT-PASS: 7 hand-built original XTM2 baseline matches; all seven II positions, locator ordering and Topic references; no XTM deserialization claim')