-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrebuild_glyph_data_csv.py
executable file
·83 lines (61 loc) · 1.93 KB
/
rebuild_glyph_data_csv.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
#!/usr/bin/env python
import argparse
import csv
import io
import pathlib
from importlib.resources import files
from glyphsLib.glyphdata import GlyphData
def getGlyphData() -> GlyphData:
path = files("glyphsLib.data") / "GlyphData.xml"
with path.open("rb") as f:
return GlyphData.from_files(f)
def readLicense():
path = files("glyphsLib.data") / "GlyphData_LICENSE"
return path.read_text()
attrs = [
"unicode",
# "unicodeLegacy",
"name",
"category",
"subCategory",
"case",
"direction",
"script",
"description",
"production",
# "altNames",
]
def glyphDataAsCSV():
data = getGlyphData()
license = readLicense()
f = io.StringIO()
f.write(
"# DO NOT EDIT: This file is generated by scripts/rebuild_glyph_data_csv.py\n"
)
f.write("# The data is derived from glyphsLib/data/GlyphData.xml\n")
f.write("#\n")
for line in license.splitlines():
line = line.strip()
f.write(f"# {line}\n" if line else "#\n")
f.write("\n")
writer = csv.writer(f, delimiter=";", lineterminator="\n")
writer.writerow(attrs)
for attrib in data.names.values():
row = [attrib.get(attr, "") for attr in attrs]
writer.writerow(row)
return f.getvalue()
def rebuildGlyphData(check=False):
repoDir = pathlib.Path(__file__).resolve().parent.parent
glyphDataPath = repoDir / "src" / "fontra" / "client" / "data" / "glyph-data.csv"
csvGlyphData = glyphDataAsCSV()
if check:
oldData = glyphDataPath.read_text(encoding="utf-8")
if csvGlyphData != oldData:
raise ValueError("new source differs from old source")
else:
glyphDataPath.write_text(csvGlyphData, encoding="utf-8")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--check", action="store_true", default=False)
args = parser.parse_args()
rebuildGlyphData(check=args.check)