-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·276 lines (204 loc) · 8.26 KB
/
Copy pathinstall.py
File metadata and controls
executable file
·276 lines (204 loc) · 8.26 KB
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
#!/usr/bin/env python3
"""Installer for VITRIOL keyboard layout.
Author....: John Murowaniecki
Repository: https://github.com/jmurowaniecki/vitriol
"""
import xml.etree.ElementTree as ET
import xml.dom.minidom
import argparse
import os
import re
class Application:
"""Installer for VITRIOL keyboard layout.
Raises:
NotADirectoryError: Informs that the target directory doesn't exist.
"""
Name = "V.I.T.R.I.O.L."
Description = "Keyboard mapping installer."
Documentation = "For more information see https://github.com/jmurowaniecki/vitriol"
def __init__(self):
"""Installer constructor.
"""
self.parser = argparse.ArgumentParser(
description=(f"\033[1;32m{Application.Name}\033[0m - {Application.Description}"),
epilog=Application.Documentation,
)
self.parser.add_argument(
"action",
nargs="?",
default="check",
help="Action to be performed.",
choices=["check", "install", "update", "uninstall", "help"],
)
self.parser.add_argument(
"-T",
"--target",
default="/usr/share/X11/xkb",
type=self.isValidTargetDirectory,
help="Target folder to install artefacts.",
)
self.parser.add_argument(
"-V",
"--verbose",
default=False,
action='store_true',
help="Output verbosity.",
)
self.parser.add_argument(
"-F",
"--force",
default=False,
action='store_true',
help="Force install/update.",
)
self.variants = [
"vitrioles",
"vitriolas",
"vitriolic",
"vitriolma",
]
self.source = {
"evdev.lst": { "from": "rules/evdev", "to": "rules/evdev", "ext": ".lst", "marks": [ "vitriol" ], "checked": False, "exec": self.installerLST },
"evdev.xml": { "from": "rules/evdev", "to": "rules/evdev", "ext": ".xml", "marks": [ "<name>vitrioles</name>" ], "checked": False, "exec": self.installerXML },
"base.lst": { "from": "rules/evdev", "to": "rules/base", "ext": ".lst", "marks": [ "vitriol" ], "checked": False, "exec": self.installerLST },
"base.xml": { "from": "rules/evdev", "to": "rules/base", "ext": ".xml", "marks": [ "<name>vitrioles</name>" ], "checked": False, "exec": self.installerXML },
"symbol.br": { "from": "symbols/br.xkb", "to": "symbols/br", "ext": "", "marks": [ "// <vitriol>", "// </vitriol>" ], "checked": False, "exec": self.installerSymbol },
}
self.args = self.parser.parse_args()
{
"help" : self.help,
"check" : self.check,
"update" : self.update,
"install" : self.install,
"uninstall": self.uninstall,
}[self.args.action]()
@staticmethod
def read(file, mode="rt"):
"""Open file for reading only.
"""
opens = open(file, mode, encoding="utf-8")
lines = opens.readlines()
opens.close()
return lines
@staticmethod
def isValidTargetDirectory(string):
"""Check if target directory is valid.
"""
if os.path.isdir(string):
return string if string[-1] != "/" else string[:-1]
raise NotADirectoryError(f"{string} is NOT a valid target directory.")
def help(self):
"""Method: help - Shows standard help for arguments parsed.
"""
self.parser.print_help()
def installerSymbol(self, target, fragment):
"""Installer for Symbol/XKB file.
"""
if not [line for line in open(target, encoding="utf-8") if re.search("^.*vitriol.*$", line)]:
if self.simulate:
print(f"- {target} not installed.")
return
orig = self.read(fragment)
file = self.read(target)
dest = open(target, "w", encoding="utf-8")
dest.writelines(file)
dest.writelines(orig)
dest.close()
print("- Symbol files installed.")
def installerLST(self, target, fragment):
"""Installer for LST files.
"""
if not [line for line in open(target, encoding="utf-8") if re.search("^.*vitriol.*$", line)]:
if self.simulate:
print(f"- {target} not installed.")
return
orig = self.read(fragment)
file = self.read(target)
if not self.source["evdev.lst"]["marks"][0] in file:
regExprComp = re.compile(r'^.*nativo .* br: Portuguese \(Brazil,.*$')
for line in file:
found = regExprComp.search(line)
if found:
file.insert(file.index(line), ''.join(orig))
break
open(target, "w", encoding="utf-8").writelines(file)
print("- LST files installed.")
def installerXML(self, target, fragment):
"""Installer for XML files.
"""
found = [line for line in open(target, encoding="utf-8") if re.search("^.*vitriol.*$", line)]
if self.force or not found:
if self.simulate:
print(f"- {target} not installed.")
return
orig = self.getSymbols(target)
tree = ET.parse(target)
root = tree.getroot()
for model_list in root.findall(".//name/[.='br']../..variantList"):
model_list.extend(orig)
tree.write(target)
print("- XML files installed.")
def getSource(self, source, default_path = "install/"):
"""Decode source path.
"""
(name, rules) = source
if self.verbose:
print(f"Name and rules: {name} {rules}.")
return f"{default_path}{rules['from']}{rules['ext']}"
def getTarget(self, target):
"""Decode target path.
"""
(name, rules) = target
if self.verbose:
print(f"Name and rules: {name} {rules}.")
return f"{self.args.target}/{rules['to']}{rules['ext']}"
def getSymbols(self, target):
symbols = f"{self.args.target}/{self.source['symbol.br']['to']}"
updates = f"install/{self.source['symbol.br']['from']}"
content = open(symbols, encoding="utf-8").read()
content+= open(updates, encoding="utf-8").read()
# content = re.sub(r'// <vitriol>.*?// </vitriol>', '', content, flags=re.DOTALL)
pattern = r'xkb_symbols\s+"([^"]+)"\s*\{[^}]*?name\[Group1\]="([^"]+)"'
matches = re.findall(pattern, content)
results = [{"name": name, "description": description} for name, description in matches]
rootvar = ET.Element("variantList")
for item in results:
variant = ET.SubElement(rootvar, "variant")
xmlNode = ET.SubElement(variant, "configItem")
name = ET.SubElement(xmlNode, "name")
name.text = item["name"]
description = ET.SubElement(xmlNode, "description")
description.text = item["description"]
return rootvar
simulate = False
verbose = False
force = False
def check(self):
"""Method: check - Only check for files having signatures.
"""
self.simulate = True
self.verbose = False
self.force = False
self.install()
def install(self):
"""Method: install - Perform installation procedures.
"""
for target in self.source.items():
origins = self.getSource(target)
destiny = self.getTarget(target)
(source, rules) = target
if self.verbose:
print(f"Source name and rules: {source} {rules}.")
self.source[source]["exec"](destiny, origins)
def update(self):
"""Method: update - Perform update procedures.
"""
print("Update method not implemented.")
self.help()
def uninstall(self):
"""Method: uninstall - Perform uninstall procedures.
"""
print("Uninstall method not implemented.")
self.help()
if __name__ == "__main__":
Application()