diff --git a/wsme/controller.py b/wsme/controller.py index 420d89c..7de3be2 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -34,9 +34,11 @@ def scan_api(controller, path=[]): if name.startswith('_'): continue a = getattr(controller, name) - if hasattr(a, '_wsme_definition'): - yield path, a._wsme_definition + if inspect.ismethod(a): + if hasattr(a, '_wsme_definition'): + yield path, a._wsme_definition else: + if len(path) > 10: raise ValueError(str(path)) for i in scan_api(a, path + [name]): yield i @@ -52,6 +54,7 @@ class FunctionArgument(object): class FunctionDefinition(object): def __init__(self, func): self.name = func.__name__ + self.doc = func.__doc__ self.return_type = None self.arguments = [] self.protocol_specific = False @@ -154,6 +157,9 @@ class WSRoot(object): kw = protocol.read_arguments(request, funcdef and funcdef.arguments or None) + if funcdef.protocol_specific: + kw['root'] = self + result = func(**kw) res.status = 200 diff --git a/wsme/soap.py b/wsme/soap.py index 48dc7c2..fc5ab91 100644 --- a/wsme/soap.py +++ b/wsme/soap.py @@ -2,7 +2,13 @@ import pkg_resources from xml.etree import ElementTree as et from genshi.template import MarkupTemplate -from wsme.controller import register_protocol, pexpose +from wsme.controller import register_protocol, pexpose, scan_api +import wsme.types + +nativetypes = { + str: 'xsd:string', + int: 'xsd:int', +} class SoapProtocol(object): name = 'SOAP' @@ -12,8 +18,13 @@ class SoapProtocol(object): "soap": "http://www.w3.org/2001/12/soap-envelope" } - def __init__(self): - pass + def __init__(self, tns=None, + typenamespace=None, + baseURL=None + ): + self.tns = tns + self.typenamespace = typenamespace + self.servicename = 'MyApp' def accept(self, root, req): if req.path.endswith('.wsdl'): @@ -56,20 +67,31 @@ class SoapProtocol(object): return et.tostring(env) @pexpose(contenttype="text/xml") - def api_wsdl(self): + def api_wsdl(self, root, service=None): + if service is None: + servicename = self.servicename + else: + servicename = self.servicename + service.capitalize() tmpl = MarkupTemplate( pkg_resources.resource_string(__name__, 'templates/wsdl.html')) stream = tmpl.generate( - tns = 'test', - typenamespace = 'tset', - soapenc = 'enc', - service_name = 'sn', - complex_types = [], - funclist = [], + tns = self.tns, + typenamespace = self.typenamespace, + soapenc = 'http://schemas.xmlsoap.org/soap/encoding/', + service_name = servicename, + complex_types = (t() for t in wsme.types.complex_types), + funclist = [i for i in scan_api(root)], arrays = [], - baseURL = '', + list_attributes = wsme.types.list_attributes, + baseURL = service, + soap_type = self.soap_type, ) return stream.render('xml') + def soap_type(self, datatype): + if datatype in nativetypes: + return nativetypes[datatype] + if wsme.types.iscomplex(datatype): + return "types:%s" % datatype.__name__ register_protocol(SoapProtocol) diff --git a/wsme/templates/wsdl.html b/wsme/templates/wsdl.html index d7f1c0d..8bbd957 100644 --- a/wsme/templates/wsdl.html +++ b/wsme/templates/wsdl.html @@ -14,26 +14,30 @@ - + - - + + - + - + + type="${soap_type(funcdef.return_type)}"/> @@ -50,23 +54,23 @@ - - - + + + - - + + - - ${funcdoc} - - + + ${funcdef.doc} + + @@ -75,8 +79,8 @@ - - + + @@ -91,7 +95,7 @@ WSDL File for ${service_name} + location="${baseURL}/"/> diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index 2f545f6..7c9d8f9 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -176,11 +176,11 @@ class WSTestRoot(WSRoot): witherrors = WithErrors() def reset(self): - self.touched = False + self._touched = False @expose() def touch(self): - self.touched = True + self._touched = True class ProtocolTestCase(unittest.TestCase): diff --git a/wsme/tests/test_soap.py b/wsme/tests/test_soap.py index ff4d963..a4ebc43 100644 --- a/wsme/tests/test_soap.py +++ b/wsme/tests/test_soap.py @@ -64,3 +64,4 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase): res = self.app.get('/api.wsdl') print res.body assert 'returntypes' in res.body + assert False diff --git a/wsme/types.py b/wsme/types.py index 9345fe8..fae9b49 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -10,10 +10,10 @@ dt_types = [datetime.date, datetime.time, datetime.datetime] extra_types = [binary, decimal.Decimal] native_types = pod_types + dt_types + extra_types -structured_types = [] +complex_types = [] -def isstructured(datatype): +def iscomplex(datatype): return hasattr(datatype, '_wsme_attributes') @@ -106,7 +106,7 @@ def register_type(class_): class_._wsme_attributes = inspect_class(class_) - structured_types.append(weakref.ref(class_)) + complex_types.append(weakref.ref(class_)) def list_attributes(class_):