implement plugin autoload

This commit is contained in:
chrys 2016-07-09 02:48:22 +02:00
parent 5478900288
commit 70ba9a7a66
3 changed files with 22 additions and 1 deletions

View File

@ -1,10 +1,30 @@
#!/bin/python #!/bin/python
import importlib.util
import glob
import os
class commandManager(): class commandManager():
def __init__(self): def __init__(self):
pass pass
def loadCommands(self, environment): def loadCommands(self, environment):
commandFolder = "commands/"
commandList = glob.glob(commandFolder+'*')
for currCommand in commandList:
try:
fileName, fileExtension = os.path.splitext(currCommand)
fileName = fileName.split('/')[-1]
if fileName in ['__init__','__pycache__']:
continue
print(fileName)
if fileExtension.lower() == 'py':
spec = importlib.util.spec_from_file_location(fileName, currCommand)
command_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(command_mod)
environment['commands']['fileName'] = command_mod()
except:
continue
return environment return environment
def executeCommand(self, environment): def executeCommand(self, environment):
print(environment['commandInfo']['currCommand']) print(environment['commandInfo']['currCommand'])
if self.isCommandDefined(environment): if self.isCommandDefined(environment):

View File

@ -8,7 +8,7 @@ class inputManager():
def __init__(self): def __init__(self):
self.devices = map(evdev.InputDevice, (evdev.list_devices())) self.devices = map(evdev.InputDevice, (evdev.list_devices()))
self.devices = {dev.fd: dev for dev in self.devices} self.devices = {dev.fd: dev for dev in self.devices}
for dev in self.devices.values(): print(dev) #for dev in self.devices.values(): print(dev)
def getKeyPressed(self, environment): def getKeyPressed(self, environment):
r, w, x = select(self.devices, [], []) r, w, x = select(self.devices, [], [])

View File

@ -27,6 +27,7 @@ class fenrir():
self.environment = environment.environment self.environment = environment.environment
self.environment['runtime']['inputManager'] = inputManager.inputManager() self.environment['runtime']['inputManager'] = inputManager.inputManager()
self.environment['runtime']['commandManager'] = commandManager.commandManager() self.environment['runtime']['commandManager'] = commandManager.commandManager()
self.environment = self.environment['runtime']['commandManager'].loadCommands(self.environment)
self.environment['runtime']['debug'] = debug.debug() self.environment['runtime']['debug'] = debug.debug()
signal.signal(signal.SIGINT, self.captureSignal) signal.signal(signal.SIGINT, self.captureSignal)