-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathkeybindings_changer.py
292 lines (218 loc) · 8.18 KB
/
keybindings_changer.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
import os
import sys
import traceback
import platform
class NullWriter(object):
def write(self, value): pass
class RadiumMock:
def addMessage(self, message):
print "MESSAGE: "+message
def appendFilePaths(self, path1, path2):
return path1 + "/" + path2
def getHomePath(self):
return "/home/kjetil"
def getPath(self, path):
return path
def fileExists(self, filename):
return True
def openFileForReading(self, filename):
return open(filename).readlines()
def fileAtEnd(self, disk):
return len(disk)==0
def readLineFromFile(self, disk):
return disk.pop(0).rstrip();
def closeFile(self, disk):
if not type(disk) == list:
disk.close()
def openFileForWriting(self, filename):
return open(filename, 'w')
def writeToFile(self, disk, text):
disk.write(text)
if __name__ == "__main__" or sys.g_program_path=='__main__':
radium = RadiumMock()
else:
import radium
ra = radium
def get_filename():
return ra.appendFilePaths(ra.getHomePath(),
ra.appendFilePaths(ra.getPath(".radium"),
ra.getPath("keybindings.conf")))
#return os.path.join(os.path.expanduser("~"), ".radium", "keybindings.conf")
def get_lines():
filename = get_filename()
if ra.fileExists(filename)==False:
#print "User keyboard configuration file doesn't exist"
return []
disk=ra.openFileForReading(filename)
ret = []
while ra.fileAtEnd(disk)==False:
ret += [ra.readLineFromFile(disk)]
ra.closeFile(disk)
return ret
def write_lines(lines):
filename = get_filename()
disk = ra.openFileForWriting(filename)
for line in lines:
print "line:",line
ra.writeToFile(disk, line + "\n")
ra.closeFile(disk)
def keybindings_equal(keybinding1, keybinding2):
return " ".join(sorted(keybinding1.split(" "))) == " ".join(sorted(keybinding2.split(" ")))
def remove_focus_and_mouse_from_keybinding(keybinding):
keys = keybinding.split(" ")
ret = []
for key in keys:
if key.startswith("MOUSE_"):
continue
if key.startswith("FOCUS_"):
continue
ret += [key]
return " ".join(ret)
def lines_without_focus_and_mouse_are_equal(line1, line2):
keybinding1, command1 = get_keybinding_and_command(line1)
keybinding2, command2 = get_keybinding_and_command(line2)
if not command1==command2:
return False
return keybindings_equal(remove_focus_and_mouse_from_keybinding(keybinding1), remove_focus_and_mouse_from_keybinding(keybinding2))
def has_line(line, lines):
for aline in lines:
print "comparing start->",aline,"-",line,"<-end"
if line==aline:
return True
return False
def get_keybinding_and_command(line):
stripped = line.strip()
if len(stripped)==0 or stripped[0]=="#":
return ["",""]
splitted = line.split(" : ")
if len(splitted)<2:
return ["",""]
else:
return [splitted[0].strip(), splitted[1].strip()]
# "CTRL_L A : aasdfsdf"
def get_keybinding_from_line(line):
return get_keybinding_and_command(line)[0]
def get_command_from_line(line):
return get_keybinding_and_command(line)[1]
#returns the last line with that keybinding ("old_line"), or False.
def has_keybinding(keybinding, lines):
ret = False
for line in lines:
if keybindings_equal(get_keybinding_from_line(line), keybinding):
ret = line
return ret
# returns changes lines
def change_line(old_line, new_line, lines):
def maybechangeit(line):
if line==old_line:
return new_line
else:
return line
return map(maybechangeit, lines)
def append_line(new_line, lines):
return lines + [new_line]
"""
Key : function()
Key2 : function()
Key : function2()
"""
# Returns new lines, or False, if nothing needs to be changed
#
# Works quite brutally:
# 1. Remove all lines that has either keybinding or command
# 2. Appends new_line
def ensure_has_line(new_line):
lines = get_lines()
keybinding = get_keybinding_from_line(new_line)
command = get_command_from_line(new_line)
num_removed = 0
new_lines = []
for line in lines:
if not keybindings_equal(get_keybinding_from_line(line), keybinding) and get_command_from_line(line) != command:
new_lines += [line]
else:
num_removed += 1
if num_removed==1 and has_line(new_line, lines):
return False
return append_line(new_line, new_lines)
#returns True if configuration file was changed.
def insert_new_line_into_conf_file(new_line):
lines = ensure_has_line(new_line)
if lines:
write_lines(lines)
return True
else:
return False
def insert_new_keybinding_into_conf_file(keybinding, command):
return insert_new_line_into_conf_file(keybinding + " : " + command)
def FROM_C_insert_new_keybinding_into_conf_file(keybinding, command):
old_stdout = sys.stdout
old_stderr = sys.stderr
changestdout = platform.system() != "Linux" and platform.system() != "Darwin" # and os.isatty(sys.stdout.fileno()):
if changestdout:
sys.stdout = NullWriter()
sys.stderr = NullWriter()
try:
insert_new_line_into_conf_file(keybinding + " : " + command)
except:
e = sys.exc_info()[0]
message = traceback.format_exc()
message2 = "Unable to add keybinding for %s to do %s:<br><pre>%s</pre>" % (keybinding, command, message)
print message2
ra.addMessage(message2)
return
if changestdout:
sys.stdout = old_stdout
sys.stderr = old_stderr
ra.reloadKeybindings()
def remove_keybinding_from_conf_file(keybinding, command):
line_to_remove = keybinding + " : " + command
lines = get_lines()
#if has_line(line_to_remove, lines)==False:
# message2 = "Could not remove keybinding \"%s\".<br>It might be a default keybinding, and those can't be removed. They can be overridden to be used for something else though." % line_to_remove
# print message2
# ra.addMessage(message2)
# return
num_removed = 0
new_lines = []
for line in lines:
if lines_without_focus_and_mouse_are_equal(line, line_to_remove):
num_removed += 1
else:
new_lines += [line]
if num_removed==0:
message2 = "Could not remove keybinding \"%s\".<br>It might be a default keybinding, and those can't be removed. Default keybindings can be overridden though." % line_to_remove
print message2
ra.addMessage(message2)
else:
write_lines(new_lines)
def FROM_C_remove_keybinding_from_conf_file(keybinding, command):
old_stdout = sys.stdout
old_stderr = sys.stderr
if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()):
sys.stdout = NullWriter()
sys.stderr = NullWriter()
try:
remove_keybinding_from_conf_file(keybinding, command)
except:
e = sys.exc_info()[0]
message = traceback.format_exc()
message2 = "Unable to remove keybinding %s - %s:<br><pre>%s</pre>" % (keybinding, command, message)
print message2
ra.addMessage(message2)
return
if platform.system() != "Linux": # and os.isatty(sys.stdout.fileno()):
sys.stdout = old_stdout
sys.stderr = old_stderr
ra.reloadKeybindings()
if __name__ == "__main__":
#update_conf_file("#gakkgakk")
#insert_new_line_into_conf_file("a b : 90")
print "A:",remove_focus_and_mouse_from_keybinding("A")
print "B:",remove_focus_and_mouse_from_keybinding("FOCUS_MIXER B")
print "False:",lines_without_focus_and_mouse_are_equal("MOUSE_MIXER A : doit", "FOCUS_MIXER B : doit")
print "True:",lines_without_focus_and_mouse_are_equal("MOUSE_MIXER A : doit", "FOCUS_MIXER A : doit")
print "False:",lines_without_focus_and_mouse_are_equal("MOUSE_MIXER A : doit1", "FOCUS_MIXER A : doit2")
print "True:",keybindings_equal("A B", "A B")
print "False:",keybindings_equal("A B", "A")
print "True:",keybindings_equal("A B", "B A")