Skip to content

Commit

Permalink
better way to turn on debug and pretty in server module
Browse files Browse the repository at this point in the history
  • Loading branch information
matee911 committed May 9, 2012
1 parent 5764fba commit 4a1ec0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
syntax: glob
*.pyc
lib/*
bin/*
include/*
36 changes: 23 additions & 13 deletions pysimplesoap/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
__author__ = "Mariano Reingart ([email protected])"
__copyright__ = "Copyright (C) 2010 Mariano Reingart"
__license__ = "LGPL 3.0"
__version__ = "1.02c"
__version__ = "1.03c"

import logging
from simplexml import SimpleXMLElement, TYPE_MAP, DateTime, Date, Decimal
import traceback
from simplexml import SimpleXMLElement, TYPE_MAP, Date, Decimal

log = logging.getLogger(__name__)

# Deprecated
DEBUG = False


Expand All @@ -31,7 +34,13 @@ def __init__(self, name, documentation='', action='', location='',
namespace=None, prefix=False,
soap_uri="http://schemas.xmlsoap.org/soap/envelope/",
soap_ns='soap',
pretty=True,
debug=False,
**kwargs):
"""
:param pretty: Prettifies generated xmls
:param debug: Use to add tracebacks in generated xmls.
"""
self.methods = {}
self.name = name
self.documentation = documentation
Expand All @@ -41,9 +50,11 @@ def __init__(self, name, documentation='', action='', location='',
self.prefix = prefix
self.soap_ns = soap_ns
self.soap_uri = soap_uri
self.pretty = pretty
self.debug = debug

def register_function(self, name, fn, returns=None, args=None, doc=None):
self.methods[name] = fn, returns, args, doc or getattr(fn,"__doc__","")
self.methods[name] = fn, returns, args, doc or getattr(fn, "__doc__", "")

def dispatch(self, xml, action=None):
"Receive and proccess SOAP call"
Expand All @@ -53,7 +64,7 @@ def dispatch(self, xml, action=None):
soap_ns, soap_uri = self.soap_ns, self.soap_uri
soap_fault_code = 'VersionMismatch'
name = None

try:
request = SimpleXMLElement(xml, namespace=self.namespace)

Expand All @@ -66,7 +77,7 @@ def dispatch(self, xml, action=None):

soap_fault_code = 'Client'

# parse request message and get local method
# parse request message and get local method
method = request('Body', ns=soap_uri).children()(0)
if action:
# method name = action
Expand All @@ -84,7 +95,7 @@ def dispatch(self, xml, action=None):
if args_types:
args = method.children().unmarshall(args_types)
elif args_types is None:
args = {'request':method} # send raw request
args = {'request': method} # send raw request
else:
args = {} # no parameters

Expand All @@ -93,11 +104,11 @@ def dispatch(self, xml, action=None):
ret = function(**args)
log.debug('%s', ret)

except Exception, e:
except Exception:
import sys
etype, evalue, etb = sys.exc_info()
if DEBUG:
import traceback
log.error(traceback.format_exc())
if self.debug:
detail = ''.join(traceback.format_exception(etype, evalue, etb))
detail += '\n\nXML REQUEST\n\n' + xml
else:
Expand All @@ -121,8 +132,9 @@ def dispatch(self, xml, action=None):

response['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
response['xmlns:xsd'] = "http://www.w3.org/2001/XMLSchema"

body = response.add_child("%s:Body" % soap_ns, ns=False)

if fault:
# generate a Soap Fault (with the python exception)
body.marshall("%s:Fault" % soap_ns, fault, ns=False)
Expand All @@ -143,7 +155,7 @@ def dispatch(self, xml, action=None):
# merge xmlelement returned
res.import_node(ret)

return response.as_xml()
return response.as_xml(pretty=self.pretty)

# Introspection functions:

Expand Down Expand Up @@ -411,5 +423,3 @@ def echo(request):
result = response.AddResult
print int(result.ab)
print str(result.dd)


0 comments on commit 4a1ec0e

Please sign in to comment.