-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_keyboards.py
executable file
·96 lines (83 loc) · 3.69 KB
/
make_keyboards.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
#!/bin/python
import os
import sys
import json
import random
from os import listdir
from os.path import isfile, join
from pprint import pprint
import xml.etree.ElementTree as et
# android:popupCharacters="1"
with open("lang_acc.json", "r") as f:
langs = json.load(f)
language = sys.argv[1]
pretty_language = language.title()
language_short = sys.argv[2]
assert language in langs.keys()
accents = langs[language]
latin_dir = "latin_keyboards"
keyboards = [join(latin_dir, f) for f in listdir(latin_dir) if isfile(join(latin_dir, f))]
schema = '{http://schemas.android.com/apk/res/android}'
codes = schema + "codes"
popup = schema + "popupKeyboard"
popup_chara = schema + "popupCharacters"
index = 1
keyboards_xml = et.Element('Keyboards')
for keyboard in keyboards:
keyboard_type = keyboard.split('/')[1].split('_')[1].split('.')[0]
keyboard_type_pretty = keyboard_type.title()
with open(keyboard, 'r') as f:
xml = et.parse(f).getroot()
for alpha in accents:
for key in xml.iter('Key'):
if key.attrib[codes] == alpha:
key.attrib[popup] = f"@xml/{language}_popup_{alpha}"
for key in xml.iter('Key'):
if key.attrib[codes] not in accents:
key.attrib[popup_chara] = ""
new_keyboard = f"{language}/pack/src/main/res/xml/{language}_{keyboard_type}.xml"
tree = et.ElementTree(xml)
tree.write(new_keyboard, xml_declaration=True, encoding='utf-8')
with open(new_keyboard, "r+") as s:
data = s.read().replace("ns0", "android")
s.seek(0)
s.write(data)
s.truncate()
lang_short = language_short
md5 = hex(random.getrandbits(128))[2:]
md5 = f"{md5[:8]}-{md5[8:12]}-{md5[12:16]}-{md5[16:20]}-{md5[20:]}"
keyboard_xml = et.SubElement(keyboards_xml, 'Keyboard')
keyboard_xml.set("nameResId", f"{pretty_language} {keyboard_type_pretty}")
keyboard_xml.set("iconResId", f"@drawable/ic_status_{language}")
keyboard_xml.set("layoutResId", f"@xml/{language}_{keyboard_type}")
keyboard_xml.set("id", md5)
keyboard_xml.set("defaultDictionaryLocale", lang_short)
keyboard_xml.set("description", f"{pretty_language} {keyboard_type_pretty}")
keyboard_xml.set("index", str(index))
index += 1
new_keyboards = f"{language}/pack/src/main/res/xml/{language}_keyboards.xml"
tree = et.ElementTree(keyboards_xml)
tree.write(new_keyboards, xml_declaration=True, encoding='utf-8')
for alpha in accents:
xml = et.Element('Keyboard')
xml.set("xmlns:android", "http://schemas.android.com/apk/res/android")
row = et.SubElement(xml, 'Row')
for accent in accents[alpha]:
et.SubElement(row, 'Key').set("android:codes", accent)
new_popup = language + f"/pack/src/main/res/xml/{language}_popup_{alpha}.xml"
tree = et.ElementTree(xml)
tree.write(new_popup, xml_declaration=True, encoding='utf-8')
md5 = hex(random.getrandbits(128))[2:]
md5 = md5[:8] + "-" + md5[8:12] + "-" + md5[12:16] + "-" + md5[16:20] + "-" + md5[20:]
dictionaries_xml = et.Element('Dictionaries')
dicitonary_xml = et.SubElement(dictionaries_xml, "Dictionary")
dicitonary_xml.set("autoTextResourceId", f"@xml/{language}_autotext")
dicitonary_xml.set("description", f"{pretty_language} Dictionary")
dicitonary_xml.set("dictionaryResourceId", f"@array/{language}_words_dict_array")
dicitonary_xml.set("id", md5)
dicitonary_xml.set("locale", language_short)
dicitonary_xml.set("nameResId", f"{pretty_language} Dictionary")
dicitonary_xml.set("type", "binary_resource")
new_dictionaries = f"{language}/pack/src/main/res/xml/{language}_dictionaries.xml"
tree = et.ElementTree(dictionaries_xml)
tree.write(new_dictionaries, xml_declaration=True, encoding='utf-8')