-
Notifications
You must be signed in to change notification settings - Fork 183
/
kvg_lookup.py
153 lines (123 loc) · 4.52 KB
/
kvg_lookup.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
146
147
148
149
150
151
152
153
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 Sebastian Grygiel
# Copyright (C) 2011-2013 Alexandre Courbot
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, re, datetime
from kanjivg import Stroke, StrokeGr
from utils import listSvgFiles, readXmlFile, canonicalId, PYTHON_VERSION_MAJOR
if PYTHON_VERSION_MAJOR > 2:
def unicode(s):
return s
def unichr(c):
return chr(c)
helpString = """Usage: %s <find-svg|find-xml> <element1> [...elementN]
Recognized commands:
find-svg Find and view summary of an SVG file for the given
element in the /kanji directory.
find-xml Find and view summary of a <kanji> entry for
the given element from the file kanjivg.xml
Parameters:
element May either be the singular character, e.g. 並 or its
unicode code-point e.g. 4e26.
Examples:
%s find-svg 並 Will list SVG files describing given character.
%s find-xml 4e26 Will list <kanji> entry for the same character.
""" % (sys.argv[0], sys.argv[0], sys.argv[0])
# Output helper
lossInWeirdEncoding = False
def writeOutput(data, output):
if PYTHON_VERSION_MAJOR >= 3:
output.write(data)
return
global lossInWeirdEncoding
if output.encoding == None:
encoding = 'utf8'
else:
encoding = output.encoding
encoded = data.encode(encoding, errors="replace")
if encoding != 'utf8' and encoding != 'utf-8':
if encoded.decode(encoding) != data:
lossInWeirdEncoding = encoding
output.write(encoded)
# Summary generators
def strokeGroupSummary(gr, indent = 0):
if not isinstance(gr, StrokeGr):
raise Exception("Invalid structure")
ret = unicode(" " * indent * 4)
# ret += gr.element if gr.element is not None and len(gr.element) > 0 else "・"
ret += "- group"
if gr.element is not None and len(gr.element) > 0:
ret += " %s" % (gr.element,)
if gr.position:
ret += " (%s)" % (gr.position,)
childStrokes = [s.stype for s in gr.childs if isinstance(s, Stroke) and s.stype]
if len(childStrokes):
ret += "\n%s- strokes: %s" % (" " * (indent+1) * 4, ' '.join(childStrokes))
ret += "\n"
for g in gr.childs:
if isinstance(g, StrokeGr):
ret += strokeGroupSummary(g, indent + 1)
return ret
def characterSummary(c):
ret = "Character summary: %s (%s)" % (c.code, c.strokes.element)
if c.variant:
ret += " - variant: %s" % (c.variant)
ret += "\n"
ret += strokeGroupSummary(c.strokes)
return ret
# Commands
def commandFindSvg(arg):
id = canonicalId(arg)
kanji = [(f.path, f.read()) for f in listSvgFiles("./kanji/") if f.id == id]
print("Found %d files matching ID %s" % (len(kanji), id))
for i, (path, c) in enumerate(kanji):
print("\nFile %s (%d/%d):" % (path, i+1, len(kanji)))
writeOutput(characterSummary(c) + "\n", sys.stdout)
def commandFindXml(arg):
id = canonicalId(arg)
files = readXmlFile('./kanjivg.xml')
if id in files:
writeOutput(characterSummary(files[id]) + "\n", sys.stdout)
else:
writeOutput(unicode("Character %s (%s) not found.\n") % (id, unichr(int(id, 16))), sys.stdout)
# Main wrapper
actions = {
"find-svg": (commandFindSvg, 2),
"find-xml": (commandFindXml, 2),
}
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] not in actions.keys() or \
len(sys.argv) <= actions[sys.argv[1]][1]:
print(helpString)
sys.exit(0)
if sys.argv[1] == "find-xml":
if not os.path.exists("./kanjivg.xml"):
print("Re-run \"./kvg.py release\" to regenerate kanjivg.xml")
sys.exit(1)
action = actions[sys.argv[1]][0]
args = sys.argv[2:]
if len(args) == 0:
action()
else:
for f in args:
action(f)
if lossInWeirdEncoding:
notice = """\nNotice: SOME CHARACTERS IN THE OUTPUT HAVE BEEN REPLACED WITH QUESTION MARKS.
The text output has been encoded using your encoding of the standard
output (%s). Try redirecting output to file if you can't get it to
work in terminal. e.g.: kvg-lookup.py find-svg 4e26 > output.txt\n""" % (lossInWeirdEncoding,)
writeOutput(notice, sys.stderr)