-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
69 lines (55 loc) · 1.68 KB
/
common.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
"""
Common functions and concepts used by multiple scripts
"""
from collections import namedtuple
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
RADICAL_DIRECTORY = "radicals"
CHARACTER_DIRECTORY = "characters"
DEFAULT_WIDTH = 109
DEFAULT_HEIGHT = 109
DEFAULT_VIEWBOX = f"0 0 {DEFAULT_WIDTH} {DEFAULT_HEIGHT}"
XML_HEADER = b'<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
XML_SVG_URL = "http://www.w3.org/2000/svg"
XML_KVG_URL = "http://kanjivg.tagaini.net"
XML_SVG_PREFIX = f"{{{XML_SVG_URL}}}"
XML_KVG_PREFIX = f"{{{XML_KVG_URL}}}"
XML_PREFIX_MAP = {
"": XML_SVG_URL,
"kvg": XML_KVG_URL,
}
def register_xml_namespaces():
for key, value in XML_PREFIX_MAP.items():
ElementTree.register_namespace(key, value)
def parse_xml(path):
tree = ElementTree.parse(path)
root = tree.getroot()
assert root.tag == "{http://www.w3.org/2000/svg}svg"
return tree, root
def build_svg(inner, *, add_style) -> Element:
root = Element(
"svg",
attrib={
"width": str(DEFAULT_WIDTH),
"height": str(DEFAULT_HEIGHT),
"viewBox": DEFAULT_VIEWBOX,
},
)
if add_style:
wrap = Element(
"g",
attrib={
"style": "fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;"
},
)
root.append(wrap)
inner(wrap)
else:
inner(root)
ElementTree.indent(root, space="\t", level=0)
return root
def write_svg(path, root):
with open(path, "wb") as file:
file.write(XML_HEADER)
file.write(b"\n")
file.write(ElementTree.tostring(root, encoding="utf-8"))