Protocols are now found through entry points
This commit is contained in:
parent
1bc2b779eb
commit
e051877a5a
8
setup.py
8
setup.py
@ -28,4 +28,12 @@ setup(
|
|||||||
'Topic :: Internet :: WWW/HTTP :: WSGI',
|
'Topic :: Internet :: WWW/HTTP :: WSGI',
|
||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
'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',
|
||||||
|
]
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
@ -5,6 +5,8 @@ import logging
|
|||||||
import webob
|
import webob
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import pkg_resources
|
||||||
|
|
||||||
from wsme import exc
|
from wsme import exc
|
||||||
from wsme.types import register_type
|
from wsme.types import register_type
|
||||||
|
|
||||||
@ -121,10 +123,23 @@ class FunctionDefinition(object):
|
|||||||
|
|
||||||
|
|
||||||
def register_protocol(protocol):
|
def register_protocol(protocol):
|
||||||
global registered_protocols
|
|
||||||
registered_protocols[protocol.name] = protocol
|
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):
|
class expose(object):
|
||||||
"""
|
"""
|
||||||
Decorator that expose a function.
|
Decorator that expose a function.
|
||||||
@ -218,7 +233,7 @@ class WSRoot(object):
|
|||||||
of a protocol.
|
of a protocol.
|
||||||
"""
|
"""
|
||||||
if isinstance(protocol, str):
|
if isinstance(protocol, str):
|
||||||
protocol = registered_protocols[protocol]()
|
protocol = getprotocol(protocol)
|
||||||
self.protocols[protocol.name] = protocol
|
self.protocols[protocol.name] = protocol
|
||||||
protocol.root = weakref.proxy(self)
|
protocol.root = weakref.proxy(self)
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ import decimal
|
|||||||
from simplegeneric import generic
|
from simplegeneric import generic
|
||||||
|
|
||||||
from wsme.rest import RestProtocol
|
from wsme.rest import RestProtocol
|
||||||
from wsme.controller import register_protocol
|
|
||||||
import wsme.types
|
import wsme.types
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -138,7 +137,7 @@ class RestJsonProtocol(RestProtocol):
|
|||||||
.. autoattribute:: content_types
|
.. autoattribute:: content_types
|
||||||
"""
|
"""
|
||||||
|
|
||||||
name = 'REST+Json'
|
name = 'restjson'
|
||||||
dataformat = 'json'
|
dataformat = 'json'
|
||||||
content_types = [
|
content_types = [
|
||||||
'application/json',
|
'application/json',
|
||||||
@ -162,5 +161,3 @@ class RestJsonProtocol(RestProtocol):
|
|||||||
|
|
||||||
def encode_error(self, errordetail):
|
def encode_error(self, errordetail):
|
||||||
return json.dumps(errordetail, encoding='utf-8')
|
return json.dumps(errordetail, encoding='utf-8')
|
||||||
|
|
||||||
register_protocol(RestJsonProtocol)
|
|
||||||
|
@ -9,7 +9,6 @@ except ImportError:
|
|||||||
from simplegeneric import generic
|
from simplegeneric import generic
|
||||||
|
|
||||||
from wsme.rest import RestProtocol
|
from wsme.rest import RestProtocol
|
||||||
from wsme.controller import register_protocol
|
|
||||||
from wsme.exc import *
|
from wsme.exc import *
|
||||||
import wsme.types
|
import wsme.types
|
||||||
|
|
||||||
@ -173,7 +172,7 @@ class RestXmlProtocol(RestProtocol):
|
|||||||
.. autoattribute:: dataformat
|
.. autoattribute:: dataformat
|
||||||
.. autoattribute:: content_types
|
.. autoattribute:: content_types
|
||||||
"""
|
"""
|
||||||
name = 'REST+XML'
|
name = 'restxml'
|
||||||
dataformat = 'xml'
|
dataformat = 'xml'
|
||||||
content_types = ['text/xml']
|
content_types = ['text/xml']
|
||||||
|
|
||||||
@ -196,5 +195,3 @@ class RestXmlProtocol(RestProtocol):
|
|||||||
if 'debuginfo' in errordetail:
|
if 'debuginfo' in errordetail:
|
||||||
et.SubElement(el, 'debuginfo').text = errordetail['debuginfo']
|
et.SubElement(el, 'debuginfo').text = errordetail['debuginfo']
|
||||||
return et.tostring(el)
|
return et.tostring(el)
|
||||||
|
|
||||||
register_protocol(RestXmlProtocol)
|
|
||||||
|
@ -18,7 +18,7 @@ except ImportError:
|
|||||||
|
|
||||||
from genshi.builder import tag, Element, Namespace
|
from genshi.builder import tag, Element, Namespace
|
||||||
from genshi.template import MarkupTemplate
|
from genshi.template import MarkupTemplate
|
||||||
from wsme.controller import register_protocol, pexpose
|
from wsme.controller import pexpose
|
||||||
import wsme.types
|
import wsme.types
|
||||||
from wsme import exc
|
from wsme import exc
|
||||||
from wsme.utils import *
|
from wsme.utils import *
|
||||||
@ -215,7 +215,7 @@ class SoapProtocol(object):
|
|||||||
.. autoattribute:: name
|
.. autoattribute:: name
|
||||||
.. autoattribute:: content_types
|
.. autoattribute:: content_types
|
||||||
"""
|
"""
|
||||||
name = 'SOAP'
|
name = 'soap'
|
||||||
content_types = ['application/soap+xml']
|
content_types = ['application/soap+xml']
|
||||||
|
|
||||||
ns = {
|
ns = {
|
||||||
@ -333,5 +333,3 @@ class SoapProtocol(object):
|
|||||||
soap_type=soap_type,
|
soap_type=soap_type,
|
||||||
soap_fname=soap_fname,
|
soap_fname=soap_fname,
|
||||||
)
|
)
|
||||||
|
|
||||||
register_protocol(SoapProtocol)
|
|
||||||
|
@ -48,7 +48,7 @@ def prepare_result(value, datatype):
|
|||||||
|
|
||||||
|
|
||||||
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
|
||||||
protocol = 'REST+Json'
|
protocol = 'restjson'
|
||||||
|
|
||||||
def call(self, fpath, _rt=None, _accept=None,
|
def call(self, fpath, _rt=None, _accept=None,
|
||||||
_no_result_decode=False, **kw):
|
_no_result_decode=False, **kw):
|
||||||
|
@ -10,8 +10,6 @@ try:
|
|||||||
except:
|
except:
|
||||||
import cElementTree as et
|
import cElementTree as et
|
||||||
|
|
||||||
import wsme.protocols.restxml
|
|
||||||
|
|
||||||
|
|
||||||
def dumpxml(key, obj, datatype=None):
|
def dumpxml(key, obj, datatype=None):
|
||||||
el = et.Element(key)
|
el = et.Element(key)
|
||||||
@ -70,7 +68,7 @@ def loadxml(el, datatype):
|
|||||||
|
|
||||||
|
|
||||||
class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
|
||||||
protocol = 'REST+XML'
|
protocol = 'restxml'
|
||||||
|
|
||||||
def call(self, fpath, _rt=None, _accept=None,
|
def call(self, fpath, _rt=None, _accept=None,
|
||||||
_no_result_decode=False, **kw):
|
_no_result_decode=False, **kw):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user