1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 """ WSGI stuffs (proof of concept for now)
24 This module offers an WSGI interface to OpenERP.
28 from wsgiref.simple_server import make_server
29 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
35 import openerp.tools.config as config
37 def wsgi_xmlrpc(environ, start_response):
38 if environ['REQUEST_METHOD'] == 'POST' and environ['PATH_INFO'].startswith('/xmlrpc/'):
39 length = int(environ['CONTENT_LENGTH'])
40 data = environ['wsgi.input'].read(length)
41 path = environ['PATH_INFO'][len('/xmlrpc/'):] # expected to be one of db, object, ... TODO should we expect a possible trailing '/' ?
43 # TODO see SimpleXMLRPCDispatcher._marshaled_dispatch() for some necessary handling.
44 # TODO see OpenERPDispatcher for some othe handling (in particular, auth things).
45 params, method = xmlrpclib.loads(data)
46 result = openerp.netsvc.ExportService.getService(path).dispatch(method, None, params)
47 response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None)
49 start_response("200 OK", [('Content-Type','text/xml'), ('Content-Length', str(len(response)))])
52 def wsgi_jsonrpc(environ, start_response):
55 def application(environ, start_response):
57 # Try all handlers until one returns some result (i.e. not None).
58 wsgi_handlers = [wsgi_xmlrpc, wsgi_jsonrpc]
59 for handler in wsgi_handlers:
60 result = handler(environ, start_response)
65 # We never returned from the loop.
67 response = 'No handler found.\n'
68 start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))])
71 # Serve XMLRPC via wsgiref's simple_server.
72 # Blocking, should probably be called in its own process.
74 httpd = make_server('localhost', config['xmlrpc_port'], application)
77 # Application setup be fore we can spawn any worker process.
78 # This is suitable for e.g. gunicorn's on_starting hook.
79 def on_starting(server):
80 config = openerp.tools.config
81 config['addons_path'] = '/home/openerp/repos/addons/trunk/' # need a config file
82 openerp.netsvc.init_logger()
83 openerp.osv.osv.start_object_proxy()
84 openerp.service.web_services.start_web_services()
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: