-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.py
executable file
·220 lines (155 loc) · 6.51 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
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
#!/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 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/rules",
type=self.isValidTargetDirectory,
help="Target folder to install artefacts.",
)
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>vitriol</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>vitriol</name>" ], "checked": False, "exec": self.installerXML },
"symbol.br": { "from": "symbols/vitriol.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:
for line in file:
found = re.search("^.*nativo .* br: Portuguese \(Brazil,.*$", line)
if found:
file.insert(file.index(line), orig[0])
print (file[file.index(line) - 5: file.index(line) + 5])
break
open(target, "w", encoding="utf-8").writelines(file)
print("- LST files installed.")
def installerXML(self, target, fragment):
"""Installer for XML 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 = ET.parse(fragment)
tree = ET.parse(target)
root = tree.getroot()
for model_list in root.findall(".//name/[.='br']../..variantList"):
model_list.insert(0, orig.getroot())
tree.write(target)
print("- XML files installed.")
def getSource(self, source, default_path = "install/"):
"""Decode source path.
"""
(name, rules) = source
# return "%s%s%s" % (default_path, self.source[source]['from'], self.source[source]['ext'])
return f"{default_path}{rules['from']}{rules['ext']}"
def getTarget(self, target):
"""Decode target path.
"""
(name, rules) = target
return f"{self.args.target}/{rules['to']}{rules['ext']}"
simulate = False
def check(self):
"""Method: check - Only check for files having signatures.
"""
self.simulate = True
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
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()