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
.. autoclass:: wsme.expose
.. autoclass:: wsme.validate
.. autoclass:: WSRoot
.. autoclass:: wsme.wsproperty
.. autoclass:: wsme.wsattr
.. autoclass:: expose
.. autoclass:: validate
.. autoclass:: wsproperty
.. autoclass:: wsattr
Internals
@ -29,7 +31,7 @@ Internals
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: wsme.controller
:members: scan_api, FunctionArgument, FunctionDefinition, WSRoot
:members: scan_api, FunctionArgument, FunctionDefinition
:mod:`wsme.rest` -- REST protocol commons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -1,6 +1,14 @@
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)
--------------------

View File

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

View File

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

View File

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

View File

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

View File

@ -3,11 +3,20 @@ import cherrypy
import webob
from turbogears import expose
class WSRoot(wsme.WSRoot):
class Controller(object):
def __init__(self, wsroot):
self._wsroot = wsroot
@expose()
def default(self, *args, **kw):
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.status = res.status
return res.body
def adapt(wsroot):
return Controller(wsroot)

View File

@ -1,14 +1,5 @@
from webob.dec import wsgify
from wsme import controller
class WSRoot(controller.WSRoot, wsgify):
"""
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)
def adapt(root):
return wsgify(root._handle_request)