summaryrefslogtreecommitdiffstats
path: root/silpa/jsonrpc/modpywrapper.py
blob: 3e61017833e1141f19305af1c74e9f030cee7e50 (plain)
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
import sys, os
from jsonrpc import ServiceHandler, ServiceException


class ServiceImplementaionNotFound(ServiceException):
    pass


class ModPyServiceHandler(ServiceHandler):
    def __init__(self, req):
        self.req = req
        ServiceHandler.__init__(self, None)


    def findServiceEndpoint(self, name):
        req = self.req

        (modulePath, fileName) = os.path.split(req.filename)
        (moduleName, ext) = os.path.splitext(fileName)
        
        if not os.path.exists(os.path.join(modulePath, moduleName + ".py")):
            raise ServiceImplementaionNotFound()
        else:
            if not modulePath in sys.path:
                sys.path.insert(0, modulePath)

            from mod_python import apache
            module = apache.import_module(moduleName, log=1)
            
            if hasattr(module, "service"):
                self.service = module.service
            elif hasattr(module, "Service"):
                self.service = module.Service()
            else:
                self.service = module

        return ServiceHandler.findServiceEndpoint(self, name)
            
    
    def handleRequest(self, data):
        self.req.content_type = "text/plain"
        data = self.req.read()
        resultData = ServiceHandler.handleRequest(self, data)
        self.req.write(resultData)
        self.req.flush()

def handler(req):
    from mod_python import apache
    ModPyServiceHandler(req).handleRequest(req)
    return apache.OK