Repository navigation
Expand file tree
/
Copy pathmake-ucd.py
More file actions
47 lines (38 loc) · 1.47 KB
/
Copy pathmake-ucd.py
File metadata and controls
47 lines (38 loc) · 1.47 KB
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
from xml.etree import ElementTree as ET
from collections import defaultdict
print('Parsing XML...')
tree = ET.parse('ucd.all.flat.xml')
names = defaultdict(set)
blocks = {}
print('1/3 - parsing characters')
for char_tag in tree.findall("./{http://www.unicode.org/ns/2003/ucd/1.0}repertoire/{http://www.unicode.org/ns/2003/ucd/1.0}char"):
try:
cp = int(char_tag.attrib["cp"], 16)
except KeyError:
print('???', char_tag.attrib)
continue
name_set = names[cp]
name_set.add(char_tag.attrib["na"])
name_set.add(char_tag.attrib["na1"])
for alias in char_tag.findall("{http://www.unicode.org/ns/2003/ucd/1.0}name-alias"):
name_set.add(alias.attrib["alias"])
print('2/3 - parsing blocks')
for block_tag in tree.findall(".//{http://www.unicode.org/ns/2003/ucd/1.0}block"):
name = block_tag.attrib["name"]
blocks[name] = {
"name": name,
"start": int(block_tag.attrib["first-cp"], 16),
"end": int(block_tag.attrib["last-cp"], 16),
}
print('3/3 - Writing unidata.py')
with open("unidata.py", "w", encoding="UTF-8") as outf:
outf.write("names = {\n")
for cp, names in sorted(names.items()):
names = sorted(n for n in names if n)
if names:
outf.write(" %d: {%s},\n" % (cp, ", ".join(repr(n) for n in names)))
outf.write("}\n")
outf.write("blocks = {\n")
for name, data in sorted(blocks.items()):
outf.write(" %r: %r,\n" % (name, data))
outf.write("}\n")