-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimplePluginLoader.py
More file actions
234 lines (211 loc) · 11.4 KB
/
Copy pathSimplePluginLoader.py
File metadata and controls
234 lines (211 loc) · 11.4 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
# -*- coding: utf-8 -*-
# chrys
# version 0.5
import glob
import os
import importlib.util
import random
import string
import _thread
from subprocess import Popen, PIPE
import orca.orca
#settings
pluginrepo = os.path.expanduser('~')+"/.config/SOPS/plugins-enabled/"
#globals
pluginList = []
loaded = False
myKeyBindings = orca.keybindings.KeyBindings()
def outputMessage(Message):
if (orca.settings.enableSpeech):
orca.speech.speak(Message)
if (orca.settings.enableBraille):
orca.braille.displayMessage(Message)
def SetupShortcutAndHandle( settings):
settings['inputeventhandler'] = orca.input_event.InputEventHandler(settings['function'], settings['pluginname'])
# just the orca modifier
if not settings['shiftkey'] and not settings['ctrlkey'] and not settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.ORCA_MODIFIER_MASK, settings['inputeventhandler']))
# orca + alt
if not settings['shiftkey'] and not settings['ctrlkey'] and settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.ORCA_ALT_MODIFIER_MASK, settings['inputeventhandler']))
# orca + CTRL
if not settings['shiftkey'] and settings['ctrlkey'] and not settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.ORCA_CTRL_MODIFIER_MASK, settings['inputeventhandler']))
# orca + alt + CTRL
if not settings['shiftkey'] and settings['ctrlkey'] and settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.ORCA_CTRL_ALT_MODIFIER_MASK, settings['inputeventhandler']))
# orca + shift
if settings['shiftkey'] and not settings['ctrlkey'] and not settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.ORCA_SHIFT_MODIFIER_MASK, settings['inputeventhandler']))
# alt + shift
if settings['shiftkey'] and not settings['ctrlkey'] and settings['altkey']:
myKeyBindings.add(orca.keybindings.KeyBinding(settings['key'], orca.keybindings.DEFAULT_MODIFIER_MASK, orca.keybindings.SHIFT_ALT_MODIFIER_MASK, settings['inputeventhandler']))
orca.settings.keyBindingsMap["default"] = myKeyBindings
def id_generator(size=7, chars=string.ascii_letters):
return ''.join(random.choice(chars) for _ in range(size))
def initSettings():
settings={
'filepath':'',
'pluginname':'',
'functionname':'',
'key':'',
'shiftkey':False,
'ctrlkey':False,
'altkey':False,
'startnotify':False,
'stopnotify':False,
'blockcall':False,
'error':False,
'exec': False,
'executeable':False,
'parameters':'',
'function':None,
'inputeventhandler':None,
'valid':False,
'supressoutput':False
}
return settings
def getPluginSettings(filepath, settings):
try:
settings['file'] = filepath
fileName, fileExtension = os.path.splitext(filepath)
if (fileExtension and (fileExtension != '')): #if there is an extension
settings['loadable'] = (fileExtension.lower() == '.py') # only python is loadable
filename = os.path.basename(filepath) #filename
filename = os.path.splitext(filename)[0] #remove extension if we have one
#remove pluginname seperated by __-__
filenamehelper = filename.split('__-__')
filename = filenamehelper[len(filenamehelper) - 1 ]
settings['permission'] = os.access(filepath, os.X_OK )
settings['pluginname'] = 'NoNameAvailable'
if len(filenamehelper) == 2:
settings['pluginname'] = filenamehelper[0]
#now get shortcuts seperated by __+__
filenamehelper = filename.split('__+__')
if len([y for y in filenamehelper if 'parameters_' in y.lower()]) == 1 and\
len([y for y in filenamehelper if 'parameters_' in y.lower()][0]) > 11:
settings['parameters'] = [y for y in filenamehelper if 'parameters_' in y.lower()][0][11:]
if len([y for y in filenamehelper if 'key_' in y.lower()]) == 1 and\
len([y for y in filenamehelper if 'key_' in y.lower()][0]) > 4 :
settings['key'] = [y for y in filenamehelper if 'key_' in y.lower()][0][4:]
if settings['key'] == '':
settings['key'] = filenamehelper[len(filenamehelper) - 1].lower()
settings['shiftkey'] = 'shift' in map(str.lower, filenamehelper)
settings['ctrlkey'] = 'control' in map(str.lower, filenamehelper)
settings['altkey'] = 'alt' in map(str.lower, filenamehelper)
settings['startnotify'] = 'startnotify' in map(str.lower, filenamehelper)
settings['stopnotify'] = 'stopnotify' in map(str.lower, filenamehelper)
settings['blockcall'] = 'blockcall' in map(str.lower, filenamehelper)
settings['error'] = 'error' in map(str.lower, filenamehelper)
settings['supressoutput'] = 'supressoutput' in map(str.lower, filenamehelper)
settings['exec'] = 'exec' in map(str.lower, filenamehelper)
settings['loadmodule'] = 'loadmodule' in map(str.lower, filenamehelper)
settings = readSettingsFromPlugin(settings)
if not settings['loadmodule']:
if not settings['permission']: #subprocessing only works with exec permission
return initSettings()
if settings['loadmodule'] and not settings['loadable']: #sorry.. its not loadable only .py is loadable
return initSettings()
if (len(settings['key']) < 1): #no shortcut
if not settings['exec']: # and no exec -> the plugin make no sense because it isnt hooked anywhere
return initSettings() #so not load it (sets valid = False)
else:
settings['key'] = '' #there is a strange key, but exec? ignore the key..
settings['valid'] = True # we could load everything
return settings
except:
return initSettings()
def readSettingsFromPlugin(settings):
if not os.access(settings['file'], os.R_OK ):
return settings
fileName, fileExtension = os.path.splitext(settings['file'])
if (fileExtension and (fileExtension != '')): #if there is an extension
if (fileExtension.lower() != '.py') and \
(fileExtension.lower() != '.sh'):
return settings
else:
return settings
with open(settings['file'], "r") as pluginFile:
for line in pluginFile:
settings['shiftkey'] = ('sopsproperty:shift' in line.lower().replace(" ", "")) or settings['shiftkey']
settings['ctrlkey'] = ('sopsproperty:control' in line.lower().replace(" ", "")) or settings['ctrlkey']
settings['altkey'] = ('sopsproperty:alt' in line.lower().replace(" ", "")) or settings['altkey']
settings['startnotify'] = ('sopsproperty:startnotify' in line.lower().replace(" ", "")) or settings['startnotify']
settings['stopnotify'] = ('sopsproperty:stopnotify' in line.lower().replace(" ", "")) or settings['stopnotify']
settings['blockcall'] = ('sopsproperty:blockcall' in line.lower().replace(" ", "")) or settings['blockcall']
settings['error'] = ('sopsproperty:error' in line.lower().replace(" ", "")) or settings['error']
settings['supressoutput'] = ('sopsproperty:supressoutput' in line.lower().replace(" ", "")) or settings['supressoutput']
settings['exec'] = ('sopsproperty:exec' in line.lower().replace(" ", "")) or settings['exec']
settings['loadmodule'] = ('sopsproperty:loadmodule' in line.lower().replace(" ", "")) or settings['loadmodule']
return settings
def buildPluginSubprocess(settings):
currplugin = "\'\"" + settings['file'] + "\" " + settings['parameters'] + "\'"
pluginname = settings['pluginname']
if settings['blockcall']:
pluginname = "blocking " + pluginname
fun_body = "def " + settings['functionname'] + "(script=None, inputEvent=None):\n"
if settings['startnotify']:
fun_body +=" outputMessage('start " + pluginname + "')\n"
fun_body +=" p = Popen(" + currplugin + ", stdout=PIPE, stderr=PIPE, shell=True)\n"
fun_body +=" stdout, stderr = p.communicate()\n"
fun_body +=" message = ''\n"
fun_body +=" if not " + str(settings['supressoutput']) + " and stdout:\n"
fun_body +=" message += str(stdout, \"utf-8\")\n"
fun_body +=" if " + str(settings['error']) + " and stderr:\n"
fun_body +=" message += ' error: ' + str(stderr, \"utf-8\")\n"
fun_body +=" outputMessage( message)\n"
if settings['stopnotify']:
fun_body +=" outputMessage('finish " + pluginname + "')\n"
fun_body +=" return True\n\n"
fun_body +="def " + settings['functionname'] + "T(script=None, inputEvent=None):\n"
fun_body +=" _thread.start_new_thread("+ settings['functionname'] + ",(script, inputEvent))\n\n"
return fun_body
def buildPluginExec(settings):
pluginname = settings['pluginname']
if settings['blockcall']:
pluginname = "blocking " + pluginname
fun_body = "def " + settings['functionname'] + "(script=None, inputEvent=None):\n"
if settings['startnotify']:
fun_body +=" outputMessage('start " + pluginname + "')\n"
fun_body += " try:\n"
fun_body += " spec = importlib.util.spec_from_file_location(\"" + settings['functionname'] + "\",\""+ settings['file']+"\")\n"
fun_body += " "+settings['functionname'] + "Module = importlib.util.module_from_spec(spec)\n"
fun_body += " spec.loader.exec_module(" + settings['functionname'] + "Module)\n"
fun_body += " except:\n"
fun_body += " pass\n"
if settings['error']:
fun_body += " outputMessage(\"Error while executing " + pluginname + "\")\n"
if settings['stopnotify']:
fun_body +=" outputMessage('finish " + pluginname + "')\n"
fun_body += " return True\n\n"
fun_body +="def " + settings['functionname'] + "T(script=None, inputEvent=None):\n"
fun_body +=" _thread.start_new_thread("+ settings['functionname'] + ",(script, inputEvent))\n\n"
return fun_body
def getFunctionName(settings):
fn = "_" + settings['pluginname'].replace("-", "_")
settings['functionname'] = fn
while settings['functionname'] == fn or settings['functionname'] + 'T' in globals() or settings['functionname'] in globals():
settings['functionname'] = id_generator() + fn
return settings
if not loaded:
pluginlist = glob.glob(pluginrepo+'*')
for currplugin in pluginlist:
settings = initSettings()
settings = getPluginSettings(currplugin, settings)
if not settings['valid']:
continue
settings = getFunctionName(settings)
if settings['loadmodule']:
exec(buildPluginExec(settings)) # load as python module
else:
exec(buildPluginSubprocess(settings)) # run as subprocess
if settings['blockcall']:
settings['function'] = globals()[settings['functionname']] # non threaded
else:
settings['function'] = globals()[settings['functionname']+"T"] # T = Threaded
if settings['exec']: # exec on load if we want
settings['function']()
if not settings['key'] == '':
SetupShortcutAndHandle(settings)
pluginList.append(settings) # store in a list
loaded = True