PageRenderTime 46ms CodeModel.GetById 38ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/jsonrpc/public/services/jsonrpc/apacheServiceHandler.py

http://pyjamas.googlecode.com/
Python | 62 lines | 31 code | 12 blank | 19 comment | 6 complexity | f76d03186395d52bb489074ee02796d0 MD5 | raw file
 1"""
 2  Copyright (c) 2006 Jan-Klaas Kollhof
 3
 4  This file is part of jsonrpc.
 5
 6  jsonrpc is free software; you can redistribute it and/or modify
 7  it under the terms of the GNU Lesser General Public License as published by
 8  the Free Software Foundation; either version 2.1 of the License, or
 9  (at your option) any later version.
10
11  This software is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with this software; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19"""
20from mod_python import apache
21from jsonrpc import SimpleServiceHandler
22
23
24class ModPyHandler(SimpleServiceHandler):
25    def send(self, data):
26        self.req.write(data)
27        self.req.flush()
28        
29    def handle(self, req):
30        self.req = req
31        req.content_type = "text/plain"
32        self.handlePartialData(req.read())
33        self.close()
34
35from mod_python import apache
36import os, sys
37
38def handler(req):    
39    (modulePath, fileName) = os.path.split(req.filename)
40    (moduleName, ext) = os.path.splitext(fileName)
41    
42    if not os.path.exists(os.path.join(modulePath, moduleName + ".py")):
43        return apache.HTTP_NOT_FOUND
44        
45    if not modulePath in sys.path:
46        sys.path.insert(0, modulePath)
47    
48    module = apache.import_module(moduleName, log=1)
49       
50    if hasattr(module, "getService"):
51        service = module.getService()
52    elif hasattr(module, "service"):
53        service = module.service
54    elif hasattr(module, "Service"):
55        service = module.Service()
56    else:
57        return apache.HTTP_SERVICE_UNAVAILABLE
58    
59    ModPyHandler(service, messageDelimiter="\n").handle(req)
60    
61    return apache.OK
62