diff --git a/wsme/cornice.py b/wsme/cornice.py index 36d653c..b353168 100644 --- a/wsme/cornice.py +++ b/wsme/cornice.py @@ -14,6 +14,7 @@ And use it:: def get_hello(who=u'World'): return Message(text='Hello %s' % who) """ +from __future__ import absolute_import import json import xml.etree.ElementTree as et @@ -76,15 +77,38 @@ def wsexpose(*args, **kwargs): request.override_renderer = 'wsmejson' elif 'text/xml' in request.headers['Accept']: request.override_renderer = 'wsmexml' + else: + request.override_renderer = 'wsmejson' return { 'datatype': funcdef.return_type, 'result': f(*args, **kwargs) } + callfunction.wsme_func = f return callfunction return decorate +def scan_api(root=None): + from cornice.service import get_services + for service in get_services(): + for method, func, options in service.definitions: + wsme_func = getattr(func, 'wsme_func') + basepath = service.path.split('/') + if basepath and not basepath[0]: + del basepath[0] + if wsme_func: + yield ( + basepath + [method.lower()], + wsme_func._wsme_definition + ) + + def includeme(config): + import pyramid.wsgi + wsroot = wsme.WSRoot(scan_api=scan_api, webpath='/ws') + wsroot.addprotocol('extdirect') config.add_renderer('wsmejson', WSMEJsonRenderer) config.add_renderer('wsmexml', WSMEXmlRenderer) + config.add_route('wsme', '/ws/*path') + config.add_view(pyramid.wsgi.wsgiapp(wsroot.wsgiapp()), route_name='wsme') diff --git a/wsme/root.py b/wsme/root.py index 07728d8..4043bda 100644 --- a/wsme/root.py +++ b/wsme/root.py @@ -78,10 +78,12 @@ class WSRoot(object): """ __registry__ = wsme.types.registry - def __init__(self, protocols=[], webpath='', transaction=None): + def __init__(self, protocols=[], webpath='', transaction=None, + scan_api=scan_api): self._debug = True self._webpath = webpath self.protocols = [] + self.scan_api = scan_api self._transaction = transaction if self._transaction is True: @@ -123,7 +125,7 @@ class WSRoot(object): :rtype: list of (path, :class:`FunctionDefinition`) """ if self._api is None: - self._api = [i for i in scan_api(self)] + self._api = [i for i in self.scan_api(self)] for path, fdef in self._api: fdef.resolve_types(self.__registry__) return self._api