Protocols are now found through entry points

This commit is contained in:
Christophe de Vienne 2011-10-12 23:18:27 +02:00
parent 1bc2b779eb
commit e051877a5a
7 changed files with 31 additions and 18 deletions

View File

@ -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',
]
},
)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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):