Change the way framework adapters works. Now the adapter modules have a simple adapt function that adapt a :class:wsme.WSRoot instance. This way a same root can be integrated in several framework.

This commit is contained in:
Christophe de Vienne 2011-10-11 22:51:21 +02:00
parent 07e8820e44
commit 1bc2b779eb
9 changed files with 52 additions and 37 deletions

View File

@ -9,11 +9,13 @@ Public API
.. module:: wsme .. module:: wsme
.. autoclass:: wsme.expose .. autoclass:: WSRoot
.. autoclass:: wsme.validate
.. autoclass:: wsme.wsproperty .. autoclass:: expose
.. autoclass:: wsme.wsattr .. autoclass:: validate
.. autoclass:: wsproperty
.. autoclass:: wsattr
Internals Internals
@ -29,7 +31,7 @@ Internals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: wsme.controller .. automodule:: wsme.controller
:members: scan_api, FunctionArgument, FunctionDefinition, WSRoot :members: scan_api, FunctionArgument, FunctionDefinition
:mod:`wsme.rest` -- REST protocol commons :mod:`wsme.rest` -- REST protocol commons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -1,6 +1,14 @@
Changes Changes
======= =======
next
----
* Change the way framework adapters works. Now the adapter modules
have a simple adapt function that adapt a :class:`wsme.WSRoot`
instance. This way a same root can be integrated in several
framework.
0.1.0a3 (2011-10-11) 0.1.0a3 (2011-10-11)
-------------------- --------------------

View File

@ -9,17 +9,17 @@ WSGI Application
.. module:: wsme.wsgi .. module:: wsme.wsgi
.. class:: WSRoot .. function:: adapt
A :class:`wsme.controller.WSRoot` that act as a WSGI application. Returns a wsgi application that serve a :class:`wsme.controller.WSRoot`.
Example Example
~~~~~~~ ~~~~~~~
.. code-block:: python .. code-block:: python
from wsme import expose, validate from wsme import *
from wsme.wsgi import WSRoot import wsme.wsgi
from wsme.protocols import restjson from wsme.protocols import restjson
class MyRoot(WSRoot): class MyRoot(WSRoot):
@ -27,7 +27,8 @@ Example
def helloworld(self): def helloworld(self):
return u"Hello World !" return u"Hello World !"
application = MyRoot(protocols=['REST+Json']) application = wsme.wsgi.adapt(
MyRoot(protocols=['REST+Json']))
Turbogears 1.x Turbogears 1.x
-------------- --------------
@ -37,10 +38,13 @@ Turbogears 1.x
.. module:: wsme.tg1 .. module:: wsme.tg1
.. class:: WSRoot .. class:: Controller(wsroot)
A :class:`wsme.controller.WSRoot` that can be inserted in a TG1 A TG1 Controller that publish a :class:`wsme.WSRoot`.
controller tree.
.. function:: adapt
Returns a :class:`Controller` that publish a :class:`wsme.WSRoot`.
Example Example
~~~~~~~ ~~~~~~~
@ -52,8 +56,8 @@ Create a new file, "wsmedemo/ws.py" :
.. code-block:: python .. code-block:: python
from wsme.tg1 import WSRoot import wsme.tg1
from wsme import expose, validate from wsme import expose, validate, WSRoot
class WSController(WSRoot): class WSController(WSRoot):
@expose(int) @expose(int)
@ -71,8 +75,10 @@ Insert the ws controller in the controller tree, (file controllers.py):
# make sure the wanted protocols are known # make sure the wanted protocols are known
import wsme.protocols.restjson import wsme.protocols.restjson
import wsme.tg1
class Root(controllers.RootController): class Root(controllers.RootController):
ws = WSController(webpath='/ws', protocols=['REST+Json']) ws = wsme.tg1.adapt(
WSController(webpath='/ws', protocols=['REST+Json']))
# ... # ...

View File

@ -1,2 +1,2 @@
from controller import expose, validate from controller import expose, validate, WSRoot
from types import wsattr, wsproperty from types import wsattr, wsproperty

View File

@ -213,7 +213,6 @@ class SoapProtocol(object):
REST+XML protocol. REST+XML protocol.
.. autoattribute:: name .. autoattribute:: name
.. autoattribute:: dataformat
.. autoattribute:: content_types .. autoattribute:: content_types
""" """
name = 'SOAP' name = 'SOAP'

View File

@ -10,7 +10,7 @@ from webob.dec import wsgify
from webtest import TestApp from webtest import TestApp
from wsme import * from wsme import *
from wsme.wsgi import WSRoot import wsme.wsgi
import wsme.types import wsme.types
warnings.filterwarnings('ignore', module='webob.dec') warnings.filterwarnings('ignore', module='webob.dec')
@ -221,7 +221,7 @@ class ProtocolTestCase(unittest.TestCase):
if self.__class__.__name__ != 'ProtocolTestCase': if self.__class__.__name__ != 'ProtocolTestCase':
self.root = WSTestRoot([self.protocol]) self.root = WSTestRoot([self.protocol])
self.app = TestApp(self.root) self.app = TestApp(wsme.wsgi.adapt(self.root))
def test_invalid_path(self): def test_invalid_path(self):
try: try:

View File

@ -5,7 +5,7 @@ import webtest
from wsme import * from wsme import *
from wsme.controller import scan_api from wsme.controller import scan_api
from wsme.wsgi import WSRoot import wsme.wsgi
class DummyProtocol(object): class DummyProtocol(object):
@ -123,7 +123,7 @@ class TestController(unittest.TestCase):
p = DummyProtocol() p = DummyProtocol()
r = MyRoot(protocols=[p]) r = MyRoot(protocols=[p])
app = webtest.TestApp(r) app = webtest.TestApp(wsme.wsgi.adapt(r))
res = app.get('/') res = app.get('/')
@ -138,7 +138,7 @@ class TestController(unittest.TestCase):
def test_no_available_protocol(self): def test_no_available_protocol(self):
r = WSRoot() r = WSRoot()
app = webtest.TestApp(r) app = webtest.TestApp(wsme.wsgi.adapt(r))
res = app.get('/', expect_errors=True) res = app.get('/', expect_errors=True)
assert res.status_int == 500 assert res.status_int == 500
@ -152,7 +152,7 @@ class TestController(unittest.TestCase):
r = WSRoot([DummierProto()]) r = WSRoot([DummierProto()])
app = webtest.TestApp(r) app = webtest.TestApp(wsme.wsgi.adapt(r))
res = app.get('/', expect_errors=True, headers={ res = app.get('/', expect_errors=True, headers={
'Accept': 'text/xml,q=0.8'}) 'Accept': 'text/xml,q=0.8'})

View File

@ -3,11 +3,20 @@ import cherrypy
import webob import webob
from turbogears import expose from turbogears import expose
class WSRoot(wsme.WSRoot):
class Controller(object):
def __init__(self, wsroot):
self._wsroot = wsroot
@expose() @expose()
def default(self, *args, **kw): def default(self, *args, **kw):
req = webob.Request(cherrypy.request.wsgi_environ) req = webob.Request(cherrypy.request.wsgi_environ)
res = self._handle_request(req) res = self._wsroot._handle_request(req)
cherrypy.response.header_list = res.headerlist cherrypy.response.header_list = res.headerlist
cherrypy.status = res.status cherrypy.status = res.status
return res.body return res.body
def adapt(wsroot):
return Controller(wsroot)

View File

@ -1,14 +1,5 @@
from webob.dec import wsgify from webob.dec import wsgify
from wsme import controller
class WSRoot(controller.WSRoot, wsgify): def adapt(root):
""" return wsgify(root._handle_request)
A WSRoot that is usable as a wsgi application.
"""
def __init__(self, *args, **kw):
super(WSRoot, self).__init__(*args, **kw)
wsgify.__init__(self, self._handle_request)
def clone(self, func=None, **kw):
return WSRoot(**kw)