forked from apache/sdap-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginhandler.py
59 lines (49 loc) · 2.11 KB
/
pluginhandler.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
import os
import sys
import logging
import importlib
class PluginHandler(object):
def __init__(self, name, pluginPath, format=None):
self.name = name
self.pluginPath = pluginPath
self.format = format
def handleRequest(self, httpMethod, path, requestHandler):
paths = path.split('/')
fileName = paths.pop()
fileNames = fileName.split('.')
fileSuffix = fileNames.pop()
# Support plugin lookup for [fgdc|gmcd|iso].xml
if fileSuffix == 'xml':
fileSuffix = fileNames.pop()
if self.format is not None and len(self.format) > 0:
try:
fileSuffix = requestHandler.get_argument('format')
except:
fileSuffix = self.format[0]
#raise Exception("Format parameter required.")
if fileSuffix not in self.format:
raise Exception("Format %s not supported." % fileSuffix)
pluginName = self._getPluginName(self.pluginPath+'/'+self.name+'/'+fileSuffix)
if not pluginName:
raise Exception("Did not find plugin.")
modulePath = self.pluginPath+'.'+self.name+'.'+fileSuffix+'.'+pluginName
if modulePath in sys.modules:
currentModuleName = ''
for moduleName in modulePath.split('.'):
currentModuleName += moduleName
#print('reloading: '+currentModuleName)
importlib.reload(sys.modules[currentModuleName])
currentModuleName += '.'
#print('modulePath: '+modulePath)
module = __import__(modulePath, globals(), locals(), [pluginName])
plugin = getattr(module, pluginName)
pluginObject = plugin(self.pluginPath+'/'+self.name+'/'+fileSuffix+'/plugin.conf')
method = getattr(pluginObject, httpMethod)
method(requestHandler)
def _getPluginName(self, path):
name = None
for fileName in os.listdir(path):
if fileName != '__init__.py' and fileName.endswith('.py'):
name = fileName.split('.')[0]
break
return name