forked from luxcem/programmer-beop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
94 lines (80 loc) · 3.38 KB
/
install.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
#!/bin/python3
import os, shutil
import xml.etree.ElementTree as ET
NAME = 'programmer_beop'
DESCRIPTION = 'Francais (Programmer Beop, ergonomique, derive de Bpo)'
LANGUAGE = 'fr'
def install_symbols():
temp_file = './new_symbols'
symbols_file = '/usr/share/X11/xkb/symbols/fr'
symbols_layout = './symbols'
write_no_edit = True
with open(temp_file, 'w') as fout:
# Clear old layout from symbols
with open(symbols_file, 'r') as fr_keys:
for line in fr_keys:
if line.strip() == '// -- start {} --'.format(NAME):
write_no_edit = False
if write_no_edit:
fout.write(line)
if line.strip() == '// -- end {} --'.format(NAME):
write_no_edit = True
with open(symbols_layout, 'r') as symbols:
fout.write("\n")
for line in symbols:
fout.write(line)
shutil.move(symbols_file, symbols_file + '.bak')
shutil.move(temp_file, symbols_file)
def install_lst(lst_file):
temp_file = './new_lst.lst'
already_installed = False
found_language = False
with open(temp_file, 'w') as fout:
with open(lst_file, 'r') as fin:
for line in fin:
if len(line.split()) > 1 and line.split()[1] == '{}:'.format(LANGUAGE):
found_language = True
if len(line.split()) > 0 and line.split()[0] == NAME:
already_installed = True
if ( len(line.split()) > 1 and line.split()[1] != '{}:'.format(LANGUAGE)
and found_language and not already_installed):
fout.write(" {name} {language}: {description}\n".format(
name=NAME,
language=LANGUAGE,
description=DESCRIPTION))
fout.write(line)
shutil.move(lst_file, lst_file + '.bak')
shutil.move(temp_file, lst_file)
def install_xml(xml_file):
temp_file = './new_xml.xml'
tree = ET.parse(xml_file)
root = tree.getroot()
layouts = root.findall("./layoutList/layout")
for layout in layouts:
name = layout.find('configItem/name')
if name.text == LANGUAGE:
variantList = layout.find('variantList')
sublayouts = variantList.findall('variant/configItem/name')
if not list(filter(lambda x: x.text == NAME, sublayouts)):
variant = ET.SubElement(variantList, 'variant')
configItem = ET.SubElement(variant, 'configItem')
name = ET.SubElement(configItem, 'name')
name.text = NAME
description = ET.SubElement(configItem, 'description')
description.text = DESCRIPTION
with open(xml_file) as f:
header = f.readline() + f.readline()
with open(temp_file, 'w+') as fout:
# fout.write(header)
tree.write(temp_file, encoding="utf8", xml_declaration=False)
content = fout.read() + "\n"
fout.seek(0)
fout.write(header + content)
shutil.move(xml_file, xml_file + '.bak')
shutil.move(temp_file, xml_file)
if __name__ == '__main__':
install_symbols()
install_lst('/usr/share/X11/xkb/rules/base.lst')
install_lst('/usr/share/X11/xkb/rules/evdev.lst')
install_xml('/usr/share/X11/xkb/rules/base.xml')
install_xml('/usr/share/X11/xkb/rules/evdev.xml')