Debian Codegraph @

src/ucd-tools/tools/ucd.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright (C) 2012-2017 Reece H. Dunn
4 #
5 # This file is part of ucd-tools.
6 #
7 # ucd-tools is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # ucd-tools is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22
23 script_map = {}
24
25 class CodePoint:
26 def __init__(self, x):
27 if isinstance(x, str):
28 self.codepoint = int(x, 16)
29 else:
30 self.codepoint = x
31
32 def __repr__(self):
33 return '%06X' % self.codepoint
34
35 def __str__(self):
36 return '%06X' % self.codepoint
37
38 def __iter__(self):
39 yield self
40
41 def __hash__(self):
42 return self.codepoint
43
44 def __eq__(self, other):
45 return self.codepoint == other.codepoint
46
47 def __ne__(self, other):
48 return self.codepoint != other.codepoint
49
50 def __lt__(self, other):
51 return self.codepoint < other.codepoint
52
53 def char(self):
54 return unichr(self.codepoint)
55
56 class CodeRange:
57 def __init__(self, x):
58 f, l = x.split('..')
59 self.first = CodePoint(f)
60 self.last = CodePoint(l)
61
62 def __repr__(self):
63 return '%s..%s' % (self.first, self.last)
64
65 def __str__(self):
66 return '%s..%s' % (self.first, self.last)
67
68 def __iter__(self):
69 for c in range(self.first.codepoint, self.last.codepoint + 1):
70 yield CodePoint(c)
71
72 def size(self):
73 return self.last.codepoint - self.first.codepoint + 1
74
75 def char(self):
76 return unichr(self.first.codepoint)
77
78 def codepoint(x):
79 if '..' in x[0]:
80 return CodeRange(x[0]), x[1:]
81 if ' ' in x:
82 return [CodePoint(c) for c in x[0].split()], x[1:]
83 if x[0] == '':
84 return CodePoint('0000'), x[1:]
85 return CodePoint(x[0]), x[1:]
86
87 def string(x):
88 if x[0] == '':
89 return None, x[1:]
90 return x[0], x[1:]
91
92 def integer(x):
93 return int(x[0]), x[1:]
94
95 def boolean(x):
96 if x[0] == 'Y':
97 return True, x[1:]
98 return False, x[1:]
99
100 def script(x):
101 return script_map[x[0]], x[1:]
102
103 def strlist(x):
104 return x, []
105
106 data_items = {
107 # Unicode Character Data:
108 'emoji-data': [
109 ('Range', codepoint),
110 ('Property', string)
111 ],
112 'Blocks': [
113 ('Range', codepoint),
114 ('Name', string)
115 ],
116 'DerivedAge': [
117 ('Range', codepoint),
118 ('Age', string),
119 ],
120 'DerivedCoreProperties': [
121 ('Range', codepoint),
122 ('Property', string),
123 ],
124 'PropList': [
125 ('Range', codepoint),
126 ('Property', string),
127 ],
128 'PropertyValueAliases': [
129 ('Property', string),
130 ('Key', string),
131 ('Value', string),
132 ('Aliases', strlist),
133 ],
134 'Scripts': [
135 ('Range', codepoint),
136 ('Script', script),
137 ],
138 'UnicodeData': [
139 ('CodePoint', codepoint),
140 ('Name', string),
141 ('GeneralCategory', string),
142 ('CanonicalCombiningClass', integer),
143 ('BidiClass', string),
144 ('DecompositionType', string),
145 ('DecompositionMapping', string),
146 ('NumericType', string),
147 ('NumericValue', string),
148 ('BidiMirrored', boolean),
149 ('UnicodeName', string),
150 ('ISOComment', string),
151 ('UpperCase', codepoint),
152 ('LowerCase', codepoint),
153 ('TitleCase', codepoint),
154 ],
155 # ConScript Unicode Registry Data:
156 'Klingon': [
157 ('CodePoint', codepoint),
158 ('Script', string),
159 ('GeneralCategory', string),
160 ('Name', string),
161 ('Transliteration', string),
162 ],
163 }
164
165 def parse_ucd_data(ucd_rootdir, dataset):
166 keys = data_items[dataset]
167 first = None
168 with open(os.path.join(ucd_rootdir, '%s.txt' % dataset)) as f:
169 for line in f:
170 line = line.replace('\n', '').split('#')[0]
171 linedata = [' '.join(x.split()) for x in line.split(';')]
172 if len(linedata) > 1:
173 if linedata[1].endswith(', First>'):
174 first = linedata
175 continue
176
177 if linedata[1].endswith(', Last>'):
178 linedata[0] = '%s..%s' % (first[0], linedata[0])
179 linedata[1] = linedata[1].replace(', Last>', '').replace('<', '')
180 first = None
181
182 data = {}
183 for key, typemap in keys:
184 data[key], linedata = typemap(linedata)
185 yield data
186
187 def parse_property_mapping(ucd_rootdir, propname, reverse=False):
188 ret = {}
189 for data in parse_ucd_data(ucd_rootdir, 'PropertyValueAliases'):
190 if data['Property'] == propname:
191 if reverse:
192 ret[data['Value']] = data['Key']
193 else:
194 ret[data['Key']] = data['Value']
195 return ret
196
197 if __name__ == '__main__':
198 try:
199 items = sys.argv[3].split(',')
200 except:
201 items = None
202 script_map = parse_property_mapping(sys.argv[1], 'sc', reverse=True)
203 for entry in parse_ucd_data(sys.argv[1], sys.argv[2]):
204 if items:
205 print(','.join([str(entry[item]) for item in items]))
206 else:
207 print(entry)
208 else:
209 script_map = parse_property_mapping('data/ucd', 'sc', reverse=True)