From e051877a5a848d432a2e2238696cabfe51c845fd Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 12 Oct 2011 23:18:27 +0200 Subject: [PATCH] Protocols are now found through entry points --- setup.py | 8 ++++++++ wsme/controller.py | 19 +++++++++++++++++-- wsme/protocols/restjson.py | 5 +---- wsme/protocols/restxml.py | 5 +---- wsme/protocols/soap.py | 6 ++---- wsme/tests/test_restjson.py | 2 +- wsme/tests/test_restxml.py | 4 +--- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index 61f154a..63dca4c 100644 --- a/setup.py +++ b/setup.py @@ -28,4 +28,12 @@ setup( 'Topic :: Internet :: WWW/HTTP :: WSGI', 'Topic :: Software Development :: Libraries :: Python Modules', ], + + entry_points={ + 'wsme.protocols': [ + 'restjson = wsme.protocols.restjson:RestJsonProtocol', + 'restxml = wsme.protocols.restxml:RestXmlProtocol', + 'soap = wsme.protocols.soap:SoapProtocol', + ] + }, ) diff --git a/wsme/controller.py b/wsme/controller.py index 388a889..592c50a 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -5,6 +5,8 @@ import logging import webob import sys +import pkg_resources + from wsme import exc from wsme.types import register_type @@ -121,10 +123,23 @@ class FunctionDefinition(object): def register_protocol(protocol): - global registered_protocols registered_protocols[protocol.name] = protocol +def getprotocol(name, options={}): + protocol_class = registered_protocols.get(name) + if protocol_class is None: + for entry_point in pkg_resources.iter_entry_points( + 'wsme.protocols'): + print entry_point + if entry_point.name == name: + protocol_class = entry_point.load() + if protocol_class is None: + raise ValueError("Cannot find protocol '%s'" % name) + registered_protocols[name] = protocol_class + return protocol_class(**options) + + class expose(object): """ Decorator that expose a function. @@ -218,7 +233,7 @@ class WSRoot(object): of a protocol. """ if isinstance(protocol, str): - protocol = registered_protocols[protocol]() + protocol = getprotocol(protocol) self.protocols[protocol.name] = protocol protocol.root = weakref.proxy(self) diff --git a/wsme/protocols/restjson.py b/wsme/protocols/restjson.py index 50cb310..5027f70 100644 --- a/wsme/protocols/restjson.py +++ b/wsme/protocols/restjson.py @@ -8,7 +8,6 @@ import decimal from simplegeneric import generic from wsme.rest import RestProtocol -from wsme.controller import register_protocol import wsme.types try: @@ -138,7 +137,7 @@ class RestJsonProtocol(RestProtocol): .. autoattribute:: content_types """ - name = 'REST+Json' + name = 'restjson' dataformat = 'json' content_types = [ 'application/json', @@ -162,5 +161,3 @@ class RestJsonProtocol(RestProtocol): def encode_error(self, errordetail): return json.dumps(errordetail, encoding='utf-8') - -register_protocol(RestJsonProtocol) diff --git a/wsme/protocols/restxml.py b/wsme/protocols/restxml.py index b1aa924..0b0a527 100644 --- a/wsme/protocols/restxml.py +++ b/wsme/protocols/restxml.py @@ -9,7 +9,6 @@ except ImportError: from simplegeneric import generic from wsme.rest import RestProtocol -from wsme.controller import register_protocol from wsme.exc import * import wsme.types @@ -173,7 +172,7 @@ class RestXmlProtocol(RestProtocol): .. autoattribute:: dataformat .. autoattribute:: content_types """ - name = 'REST+XML' + name = 'restxml' dataformat = 'xml' content_types = ['text/xml'] @@ -196,5 +195,3 @@ class RestXmlProtocol(RestProtocol): if 'debuginfo' in errordetail: et.SubElement(el, 'debuginfo').text = errordetail['debuginfo'] return et.tostring(el) - -register_protocol(RestXmlProtocol) diff --git a/wsme/protocols/soap.py b/wsme/protocols/soap.py index a6053ca..527657a 100644 --- a/wsme/protocols/soap.py +++ b/wsme/protocols/soap.py @@ -18,7 +18,7 @@ except ImportError: from genshi.builder import tag, Element, Namespace from genshi.template import MarkupTemplate -from wsme.controller import register_protocol, pexpose +from wsme.controller import pexpose import wsme.types from wsme import exc from wsme.utils import * @@ -215,7 +215,7 @@ class SoapProtocol(object): .. autoattribute:: name .. autoattribute:: content_types """ - name = 'SOAP' + name = 'soap' content_types = ['application/soap+xml'] ns = { @@ -333,5 +333,3 @@ class SoapProtocol(object): soap_type=soap_type, soap_fname=soap_fname, ) - -register_protocol(SoapProtocol) diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 7580474..214f620 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -48,7 +48,7 @@ def prepare_result(value, datatype): class TestRestJson(wsme.tests.protocol.ProtocolTestCase): - protocol = 'REST+Json' + protocol = 'restjson' def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False, **kw): diff --git a/wsme/tests/test_restxml.py b/wsme/tests/test_restxml.py index 67b91af..eca5d8b 100644 --- a/wsme/tests/test_restxml.py +++ b/wsme/tests/test_restxml.py @@ -10,8 +10,6 @@ try: except: import cElementTree as et -import wsme.protocols.restxml - def dumpxml(key, obj, datatype=None): el = et.Element(key) @@ -70,7 +68,7 @@ def loadxml(el, datatype): class TestRestXML(wsme.tests.protocol.ProtocolTestCase): - protocol = 'REST+XML' + protocol = 'restxml' def call(self, fpath, _rt=None, _accept=None, _no_result_decode=False, **kw):