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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#!/usr/bin/python3
import json
import tempfile
import unittest
import xml.etree.ElementTree
import cldr2json
class TestParseSingleKey(unittest.TestCase):
def test_ascii(self):
self.assertEqual(cldr2json.parse_single_key("a"), "a")
def test_nonascii(self):
self.assertEqual(cldr2json.parse_single_key("Æ"), "Æ")
def test_twochars(self):
self.assertEqual(cldr2json.parse_single_key("ԵՒ"), "ԵՒ")
def test_decode(self):
self.assertEqual(cldr2json.parse_single_key("\\u{200D}"), "\u200d")
def test_decode_threechars(self):
self.assertEqual(cldr2json.parse_single_key("ज\\u{94D}ञ"), "ज\u094Dञ")
class TestParseRow(unittest.TestCase):
def test_parse_row(self):
xml_string = """
<keyMap>
<map iso="D01" to="a" longPress="à â % æ á ä ã å ā ª"/> <!-- Q -->
<map iso="D02" to="z"/> <!-- W -->
<map iso="D03" to="e" longPress="é è ê ë % ę ė ē"/>
<map iso="D04" to="r"/>
<map iso="D05" to="t"/>
<map iso="D06" to="y" longPress="% ÿ"/>
<map iso="D07" to="u" longPress="ù û % ü ú ū"/>
<map iso="D08" to="i" longPress="î % ï ì í į ī"/>
<map iso="D09" to="o" longPress="ô œ % ö ò ó õ ø ō º"/>
<map iso="D10" to="p"/>
<map iso="C01" to="q"/> <!-- A -->
<map iso="C02" to="s"/>
<map iso="C03" to="d"/>
<map iso="C04" to="f"/>
<map iso="C05" to="g"/>
<map iso="C06" to="h"/>
<map iso="C07" to="j"/>
<map iso="C08" to="k"/>
<map iso="C09" to="l"/>
<map iso="C10" to="m"/> <!-- ; -->
<map iso="B01" to="w"/> <!-- Z -->
<map iso="B02" to="x"/>
<map iso="B03" to="c" longPress="ç ć č"/>
<map iso="B04" to="v"/>
<map iso="B05" to="b"/>
<map iso="B06" to="n"/>
<map iso="B07" to="'" longPress="‘ ’ ‹ ›"/> <!-- M -->
<map iso="A02" to=","/> <!-- (key to left of space) -->
<map iso="A03" to=" "/> <!-- space -->
<map iso="A04" to="." longPress="# ! , ? - : ' @"/> <!-- (key to right of space) -->
</keyMap>
"""
xml_tree = xml.etree.ElementTree.XML(xml_string)
json = [[
['a', 'à', 'â', '%', 'æ', 'á', 'ä', 'ã', 'å', 'ā', 'ª'],
['z'],
['e', 'é', 'è', 'ê', 'ë', '%', 'ę', 'ė', 'ē'],
['r'],
['t'],
['y', '%', 'ÿ'],
['u', 'ù', 'û', '%', 'ü', 'ú', 'ū'],
['i', 'î', '%', 'ï', 'ì', 'í', 'į', 'ī'],
['o', 'ô', 'œ', '%', 'ö', 'ò', 'ó', 'õ', 'ø', 'ō', 'º'],
['p']
], [
['q'],
['s'],
['d'],
['f'],
['g'],
['h'],
['j'],
['k'],
['l'],
['m']
], [
['w'],
['x'],
['c', 'ç', 'ć', 'č'],
['v'],
['b'],
['n'],
["'", '‘', '’', '‹', '›']
], [
[','],
[' '],
['.', '#', '!', ',', '?', '-', ':', "'", '@']
]]
self.assertEqual(cldr2json.parse_rows(xml_tree), json)
class TestConvertXml(unittest.TestCase):
def test_convert_xml(self):
xml_string = """<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE keyboard SYSTEM "../dtd/ldmlKeyboard.dtd">
<keyboard locale="fr-t-k0-android">
<version platform="4.4" number="$Revision: 11914 $"/>
<names>
<name value="French"/>
</names>
<keyMap>
<map iso="D01" to="a" longPress="à â % æ á ä ã å ā ª"/> <!-- Q -->
</keyMap>
<keyMap modifiers="shift caps">
<map iso="D01" to="A" longPress="À Â % Æ Á Ä Ã Å Ā ª"/> <!-- Q -->
</keyMap>
<keyMap modifiers="opt">
<map iso="D01" to="1" longPress="¹ ½ ⅓ ¼ ⅛"/> <!-- Q base=a -->
</keyMap>
<keyMap modifiers="opt+shift">
<map iso="D01" to="~"/> <!-- Q base=a -->
</keyMap>
</keyboard>
"""
xml_tree = xml.etree.ElementTree.XML(xml_string)
json = {
"locale": "fr",
"name": "French",
"levels": [
{
"level": "",
"mode": "default",
"rows": [
[
['a', 'à', 'â', '%', 'æ', 'á', 'ä', 'ã', 'å', 'ā', 'ª'],
]
]
},
{
"level": "shift",
"mode": "latched",
"rows": [
[
['A', 'À', 'Â', '%', 'Æ', 'Á', 'Ä', 'Ã', 'Å', 'Ā', 'ª'],
]
]
},
{
"level": "opt",
"mode": "locked",
"rows": [
[
["1", "¹", "½", "⅓", "¼", "⅛"],
]
]
},
{
"level": "opt+shift",
"mode": "locked",
"rows": [
[
["~"],
]
]
}
]
}
self.assertEqual(cldr2json.convert_xml(xml_tree), json)
class TestConvertFile(unittest.TestCase):
def test_fr(self):
outdir = tempfile.mkdtemp()
cldr2json.convert_file("test/data/fr-t-k0-android.xml", outdir)
with open("test/data/fr.json", encoding="utf-8") as expected_json_fd:
expected_json = json.load(expected_json_fd)
with open(outdir + "/fr.json", encoding="utf-8") as actual_json_fd:
actual_json = json.load(actual_json_fd)
self.assertEqual(expected_json, actual_json)
class TestLocaleToXKB(unittest.TestCase):
def test_simple(self):
self.assertEqual(cldr2json.locale_to_xkb("fr", "French"),
"fr")
def test_fallback(self):
self.assertEqual(cldr2json.locale_to_xkb("nb", "Norwegian Bokmål"),
"no")
def test_fallback2(self):
self.assertEqual(cldr2json.locale_to_xkb("km", "Khmer"),
"kh")
def test_override(self):
self.assertEqual(cldr2json.locale_to_xkb("en-GB",
"English Great Britain"),
"uk")
class LoadXKBMapplings(unittest.TestCase):
def test_dictionnary(self):
self.assertIsInstance(cldr2json.load_xkb_mappings(), dict)
def test_mapping(self):
mapping = cldr2json.load_xkb_mappings()
self.assertEqual(mapping["French"], "fr")
if __name__ == '__main__':
unittest.main()
|