diff --git a/doc/index.rst b/doc/index.rst index ea32d5d..51a1dd7 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -26,6 +26,7 @@ Contents gettingstarted api + integrate changes diff --git a/doc/integrate.rst b/doc/integrate.rst new file mode 100644 index 0000000..e863078 --- /dev/null +++ b/doc/integrate.rst @@ -0,0 +1,42 @@ +Integrating with a Framework +============================ + +Turbogears 1.x +-------------- + +.. module:: wsme.tg1 + +wsme.tg1 provides a WSRoot controller that can be part of your +controller tree. + +In a freshly quickstarted tg1 application (let's say, wsmedemo), +the prefered way is the following : + +Create a new file, "wsmedemo/ws.py" : + +.. code-block:: python + + from wsme.tg1 import WSRoot + from wsme import expose, validate + + class WSController(WSRoot): + @expose(int) + @validate(int, int) + def multiply(self, a, b): + return a * b + +Insert the ws controller in the controller tree, (file controllers.py): + +.. code-block:: python + + # ... + + from wsmedemo.ws import WSController + + # make sure the wanted protocols are known + import wsme.protocols.restjson + + class Root(controllers.RootController): + ws = WSController(webpath='ws', protocols=['REST+Json']) + + # ... diff --git a/wsme/__init__.py b/wsme/__init__.py index ee4a877..f8eef28 100644 --- a/wsme/__init__.py +++ b/wsme/__init__.py @@ -1,2 +1,2 @@ -from controller import expose, validate, WSRoot +from controller import expose, validate from types import wsattr, wsproperty diff --git a/wsme/controller.py b/wsme/controller.py index dcd9a6e..06a8b7e 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -8,7 +8,7 @@ import sys from wsme import exc from wsme.types import register_type -__all__ = ['expose', 'validate', 'WSRoot'] +__all__ = ['expose', 'validate'] log = logging.getLogger(__name__) @@ -128,10 +128,9 @@ class validate(object): class WSRoot(object): - def __init__(self, protocols=[]): + def __init__(self, protocols=[], webpath=''): self._debug = True - if protocols is None: - protocols = registered_protocols.keys() + self._webpath = webpath self.protocols = {} for protocol in protocols: self.addprotocol(protocol) diff --git a/wsme/rest.py b/wsme/rest.py index f86c9d5..4f0907c 100644 --- a/wsme/rest.py +++ b/wsme/rest.py @@ -52,7 +52,10 @@ class RestProtocol(object): return kw def extract_path(self, request): - path = request.path.strip('/').split('/') + path = request.path + assert path.startswith(self.root._webpath) + path = path[len(self.root._webpath):] + path = path.strip('/').split('/') if path[-1].endswith('.' + self.dataformat): path[-1] = path[-1][:-len(self.dataformat) - 1] diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index 284b28d..8284ed9 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -10,6 +10,7 @@ from webob.dec import wsgify from webtest import TestApp from wsme import * +from wsme.controller import WSRoot import wsme.types warnings.filterwarnings('ignore', module='webob.dec') diff --git a/wsme/tests/test_controller.py b/wsme/tests/test_controller.py index 6b6beb8..8985b6f 100644 --- a/wsme/tests/test_controller.py +++ b/wsme/tests/test_controller.py @@ -4,7 +4,7 @@ from webob.dec import wsgify import webtest from wsme import * -from wsme.controller import scan_api +from wsme.controller import scan_api, WSRoot class DummyProtocol(object): diff --git a/wsme/tg1.py b/wsme/tg1.py new file mode 100644 index 0000000..e09357c --- /dev/null +++ b/wsme/tg1.py @@ -0,0 +1,13 @@ +import wsme +import cherrypy +import webob +from turbogears import expose + +class WSRoot(wsme.WSRoot): + @expose() + def default(self, *args, **kw): + req = webob.Request(cherrypy.request.wsgi_environ) + res = self._handle_request(req) + cherrypy.response.header_list = res.headerlist + cherrypy.status = res.status + return res.body