forked from anssih/finqwerty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_layouts.py
executable file
·319 lines (294 loc) · 9.1 KB
/
generate_layouts.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/python3
# This is called automatically by the build process to generate some
# layouts that are similar to other layouts.
import os
import pathlib
import sys
NAME = "name"
SOURCE = "source"
# was the source file generated by us as well?
IS_SOURCE_GENERATED = "is_source_generated"
# simple string replacement
REPLACE = "replace"
# remove "map key scancode KEYCODE" by scancode
REMOVE_SCANCODES = "remove_scancodes"
# remove "key KEYCODE { ... } " by KEYCODE
REMOVE_KEYCODES = "remove_keycodes"
ADD = "add"
# complex replacement
# replace only within keycode
REPL_KEYCODE = "repl_keycode"
# prev string
REPL_OLD = "repl_old"
# replacement string
REPL_NEW = "repl_new"
# do not replace if line contains:
REPL_SKIP = "repl_skip"
DAN_SWE_REPLACE = [
("æ", "ö"),
("u00e6", "u00f6"),
("Æ", "Ö"),
("u00c6", "u00d6"),
("ø", "ä"),
("u00f8", "u00e4"),
("Ø", "Ä"),
("u00d8", "u00c4"),
]
DAN_NOR_REPLACE = [
("æ", "ø"),
("u00e6", "u00f8"),
("Æ", "Ø"),
("u00c6", "u00d8"),
("ø", "æ"),
("u00f8", "u00e6"),
("Ø", "Æ"),
("u00d8", "u00c6"),
]
USINTL_ALTGR_REPLACE_APOSTROPHE = [
{
REPL_KEYCODE: "APOSTROPHE",
REPL_OLD: r"'\''",
REPL_NEW: r"'\u0301'",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "APOSTROPHE",
REPL_OLD: "'\"'",
REPL_NEW: r"'\u0308'",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "APOSTROPHE",
REPL_OLD: r"'\u0301'",
REPL_NEW: r"'\''",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "APOSTROPHE",
REPL_OLD: r"'\u0308'",
REPL_NEW: "'\"'",
REPL_SKIP: ["alt:", "label:"],
},
]
USINTL_ALTGR_REPLACE_GRAVE = [
{
REPL_KEYCODE: "GRAVE",
REPL_OLD: r"'`'",
REPL_NEW: r"'\u0300'",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "GRAVE",
REPL_OLD: "'~'",
REPL_NEW: r"'\u0303'",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "GRAVE",
REPL_OLD: r"'\u0300'",
REPL_NEW: r"'`'",
REPL_SKIP: ["alt:", "label:"],
},
{
REPL_KEYCODE: "GRAVE",
REPL_OLD: r"'\u0303'",
REPL_NEW: "'~'",
REPL_SKIP: ["alt:", "label:"],
},
]
USINTL_POL_PROG_REPLACE = [
("u00e9", "u0119"), # é to ę
("u00c9", "u0118"), # É to Ę
("u00e1", "u0105"), # á to ą
("u00c1", "u0104"), # Á to Ą
{
REPL_KEYCODE: "S",
REPL_OLD: r"'\u00df'",
REPL_NEW: r"'\u015b'",
REPL_SKIP: ["alt:"],
}, # ß to ś (except on alt modifier)
("u00a7", "u015a"), # § to Ś
("u00f8", "u0142"), # ø to ł
("u00d8", "u0141"), # Ø to Ł
("u00e6", "u017c"), # æ to ż
("u00c6", "u017b"), # Æ to Ż
("u00a9", "u0107"), # © to ć
("u00a2", "u0106"), # ¢ to Ć
("u00f1", "u0144"), # ñ to ń
("u00d1", "u0143"), # Ñ to Ń
]
GENERATED_LAYOUTS = [
{
NAME: "pro1_qwerty_cze_1.kcm",
SOURCE: "pro1_qwerty_cze_2.kcm",
REMOVE_SCANCODES: [
21, # Z
44, # Y
],
},
{
NAME: "pro1_qwertz_cze_1.kcm",
SOURCE: "pro1_qwertz_cze_2.kcm",
REPLACE: [
("map key 20 Z", "map key 20 Y"),
("map key 27 Y", "map key 27 Z"),
("key Y", "key Z"),
("key Z", "key Y"),
("'z'", "'y'"),
("'Z'", "'Y'"),
("'y'", "'z'"),
("'Y'", "'Z'"),
],
},
{
NAME: "pro1_qwerty_nor_1.kcm",
SOURCE: "pro1_qwerty_dan_1.kcm",
REPLACE: DAN_NOR_REPLACE,
},
{
NAME: "pro1_qwertz_nor_1.kcm",
SOURCE: "pro1_qwertz_dan_1.kcm",
REPLACE: DAN_NOR_REPLACE,
},
{
NAME: "pro1_qwerty_swe_1.kcm",
SOURCE: "pro1_qwerty_dan_1.kcm",
REPLACE: DAN_SWE_REPLACE,
},
{
NAME: "pro1_qwerty_usaintl_fndead.kcm",
SOURCE: "pro1_qwerty_usaintl_1.kcm",
REPLACE: USINTL_ALTGR_REPLACE_APOSTROPHE+
USINTL_ALTGR_REPLACE_GRAVE,
},
{
NAME: "pro1_qwertz_usaintl_fndead.kcm",
SOURCE: "pro1_qwertz_usaintl_1.kcm",
REPLACE: USINTL_ALTGR_REPLACE_APOSTROPHE,
# no GRAVE key on QWERTZ variant
},
{
NAME: "pro1_qwertz_fin_1.kcm",
SOURCE: "pro1_qwertz_dan_1.kcm",
REPLACE: DAN_SWE_REPLACE,
},
{
NAME: "pro1_qwerty_fin_1.kcm",
SOURCE: "pro1_qwerty_swe_1.kcm",
IS_SOURCE_GENERATED: True,
REPLACE: [
("å", "ä"),
("u00e5", "u00e4"),
("Å", "Ä"),
("u00c5", "u00c4"),
],
REMOVE_SCANCODES: [
111, # DEL
13, # =
],
REMOVE_KEYCODES: [
"PLUS",
],
ADD: r"""
key O {
label: 'O'
base: 'o'
shift, capslock: 'O'
fn: '\u00e5' # å
fn+shift, fn+capslock, fn+ctrl: '\u00c5' # Å
}
""",
},
{
NAME: "pro1_qwerty_pol_prog.kcm",
SOURCE: "pro1_qwerty_usaintl_fndead.kcm",
IS_SOURCE_GENERATED: True,
REPLACE: USINTL_POL_PROG_REPLACE,
REMOVE_KEYCODES : [
"X",
],
ADD: r"""
key X {
label: 'X'
base: 'x'
shift, capslock: 'X'
fn: '\u017a' # ź
fn+shift, ctrl+fn, fn+capslock: '\u0179' # Ź
}
""",
},
]
def expand_replacement(rule):
if isinstance(rule, tuple):
return {REPL_OLD: rule[0], REPL_NEW: rule[1]}
return rule
def generate_layout(layout, target_dir):
pathlib.Path(target_dir).mkdir(parents=True, exist_ok=True)
layout_dir = os.path.join(os.path.dirname(__file__), "finqwerty", "src", "main", "res", "raw")
source_dir = target_dir if layout.get(IS_SOURCE_GENERATED) else layout_dir
with \
open(os.path.join(source_dir, layout[SOURCE]), 'r', encoding="utf-8") as src, \
open(os.path.join(target_dir, layout[NAME]), 'w', encoding="utf-8") as dst:
remove_this_key = False
cur_keycode = None
rules_matched = {REPLACE: [], REMOVE_SCANCODES: [], REMOVE_KEYCODES: []}
for line in src:
if remove_this_key:
if line.startswith('}'):
remove_this_key = False
# skip this key
continue
if line.startswith('}'):
cur_keycode = None
if line.startswith("map key "):
for scancode in layout.get(REMOVE_SCANCODES, []):
if line.startswith("map key {} ".format(scancode)):
line = "#" + line
rules_matched[REMOVE_SCANCODES].append(scancode)
break
if line.startswith("key "):
cur_keycode = line.split()[1]
for keycode in layout.get(REMOVE_KEYCODES, []):
if line.startswith("key {} ".format(keycode)):
remove_this_key = True
rules_matched[REMOVE_KEYCODES].append(keycode)
break
if remove_this_key:
continue
for replacement_orig in layout.get(REPLACE, []):
replacement = expand_replacement(replacement_orig)
if REPL_KEYCODE in replacement and cur_keycode != replacement[REPL_KEYCODE]:
continue
if any(skip in line for skip in replacement.get(REPL_SKIP, [])):
continue
if replacement[REPL_OLD] in line:
line = line.replace(replacement[REPL_OLD], replacement[REPL_NEW])
rules_matched[REPLACE].append(replacement_orig)
break
dst.write(line)
for ruletype in rules_matched.keys():
for rule in layout.get(ruletype, []):
if rule not in rules_matched[ruletype]:
if ruletype == REPLACE and any(ord(c) >= 128 for c in expand_replacement(rule)[REPL_OLD]):
# non-ASCII rule, it is just for comments so allow not matching
continue
raise RuntimeError(f"Rule {rule} for {layout[NAME]} were never executed")
dst.write(layout.get(ADD, ""))
def generate_layouts(layouts, target_dir):
for layout in layouts:
generate_layout(layout, target_dir)
def remove_previous_layouts(target_dir):
if pathlib.Path(target_dir).exists():
# for safety, only remove .kcm files
for path in pathlib.Path(target_dir).iterdir():
if path.suffix == '.kcm':
path.unlink()
pathlib.Path(target_dir).rmdir()
def generate_all_layouts(target_dir):
# in case layouts are removed, clean the target directory
remove_previous_layouts(target_dir)
generate_layouts(layouts=GENERATED_LAYOUTS, target_dir=target_dir)
def main():
generate_all_layouts(target_dir=sys.argv[1])
if __name__ == "__main__":
main()