From 8811b307e4226c3109c0d66b4d3a758e05f7f25b Mon Sep 17 00:00:00 2001 From: dawryn Date: Thu, 29 May 2014 15:49:35 +0200 Subject: [PATCH 1/2] Using configuration properties of objects --- src/SOAPpy/Client.py | 2 +- src/SOAPpy/SOAPBuilder.py | 28 ++++++++++++++-------------- src/SOAPpy/Server.py | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/SOAPpy/Client.py b/src/SOAPpy/Client.py index 26235f6..3930e72 100644 --- a/src/SOAPpy/Client.py +++ b/src/SOAPpy/Client.py @@ -477,7 +477,7 @@ class SOAPProxy: throw_struct = 0 if throw_struct: - if Config.debug: + if self.config.debug: print p raise p diff --git a/src/SOAPpy/SOAPBuilder.py b/src/SOAPpy/SOAPBuilder.py index 89fb80a..cfbd5aa 100644 --- a/src/SOAPpy/SOAPBuilder.py +++ b/src/SOAPpy/SOAPBuilder.py @@ -100,7 +100,7 @@ class SOAPBuilder: self.noroot = noroot def build(self): - if Config.debug: print "In build." + if self.config.debug: print "In build." ns_map = {} # Cache whether typing is on or not @@ -197,7 +197,7 @@ class SOAPBuilder: return ''.join(self.out) def gentag(self): - if Config.debug: print "In gentag." + if self.config.debug: print "In gentag." self.tcounter += 1 return "v%d" % self.tcounter @@ -279,7 +279,7 @@ class SOAPBuilder: # dumpers def dump(self, obj, tag = None, typed = 1, ns_map = {}): - if Config.debug: print "In dump.", "obj=", obj + if self.config.debug: print "In dump.", "obj=", obj ns_map = ns_map.copy() self.depth += 1 @@ -293,7 +293,7 @@ class SOAPBuilder: def dumper(self, nsURI, obj_type, obj, tag, typed = 1, ns_map = {}, rootattr = '', id = '', xml = '<%(tag)s%(type)s%(id)s%(attrs)s%(root)s>%(data)s\n'): - if Config.debug: print "In dumper." + if self.config.debug: print "In dumper." if nsURI == None: nsURI = self.config.typesNamespaceURI @@ -324,12 +324,12 @@ class SOAPBuilder: "id": id, "attrs": a} def dump_float(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_float." + if self.config.debug: print "In dump_float." tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding - if Config.strict_range: + if self.config.strict_range: doubleType(obj) if PosInf == obj: @@ -346,7 +346,7 @@ class SOAPBuilder: None, "double", obj, tag, typed, ns_map, self.genroot(ns_map))) def dump_int(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_int." + if self.config.debug: print "In dump_int." # fix error "Bad types (class java.math.BigInteger -> class java.lang.Integer)" if isinstance(obj, LongType): @@ -358,12 +358,12 @@ class SOAPBuilder: ns_map, self.genroot(ns_map))) def dump_bool(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_bool." + if self.config.debug: print "In dump_bool." self.out.append(self.dumper(None, 'boolean', obj, tag, typed, ns_map, self.genroot(ns_map))) def dump_string(self, obj, tag, typed = 0, ns_map = {}): - if Config.debug: print "In dump_string." + if self.config.debug: print "In dump_string." tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding @@ -381,7 +381,7 @@ class SOAPBuilder: dump_unicode = dump_string def dump_None(self, obj, tag, typed = 0, ns_map = {}): - if Config.debug: print "In dump_None." + if self.config.debug: print "In dump_None." tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding ns = self.genns(ns_map, self.config.schemaNamespaceURI)[0] @@ -392,7 +392,7 @@ class SOAPBuilder: dump_NoneType = dump_None # For Python 2.2+ def dump_list(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_list.", "obj=", obj + if self.config.debug: print "In dump_list.", "obj=", obj tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding @@ -512,7 +512,7 @@ class SOAPBuilder: dump_tuple = dump_list def dump_map(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_map.", "obj=", obj + if self.config.debug: print "In dump_map.", "obj=", obj tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding @@ -566,7 +566,7 @@ class SOAPBuilder: self.out.append("\n" % vns) def dump_dictionary(self, obj, tag, typed = 1, ns_map = {}): - if Config.debug: print "In dump_dictionary." + if self.config.debug: print "In dump_dictionary." tag = tag or self.gentag() tag = toXMLname(tag) # convert from SOAP 1.2 XML name encoding @@ -596,7 +596,7 @@ class SOAPBuilder: else: tag = self.gentag() - # watch out for order! + # watch out for order! dumpmap = ( (Exception, self.dump_exception), (mapType, self.dump_map), diff --git a/src/SOAPpy/Server.py b/src/SOAPpy/Server.py index b2c2c2c..e990c4f 100644 --- a/src/SOAPpy/Server.py +++ b/src/SOAPpy/Server.py @@ -233,7 +233,7 @@ class SOAPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): args = r._aslist() kw = r._asdict() - if Config.simplify_objects: + if self.server.config.simplify_objects: args = simplify(args) kw = simplify(kw) @@ -253,7 +253,7 @@ class SOAPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): ordered_args = {} named_args = {} - if Config.specialArgs: + if self.server.config.specialArgs: for (k,v) in kw.items(): @@ -383,7 +383,7 @@ class SOAPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): if f.context: # retrieve context object c = _contexts[thread_id] - if Config.specialArgs: + if self.server.config.specialArgs: if c: named_args["_SOAPContext"] = c fr = apply(f, ordered_args, named_args) @@ -404,7 +404,7 @@ class SOAPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): fr = apply(f, args, {}) else: - if Config.specialArgs: + if self.server.config.specialArgs: fr = apply(f, ordered_args, named_args) else: fr = apply(f, args, {}) From 0694b273bcd89eb844641f27f5e92bfc4b167dba Mon Sep 17 00:00:00 2001 From: dawryn Date: Fri, 20 Jun 2014 11:05:16 +0200 Subject: [PATCH 2/2] Dump user defined types with handler functions (can be used to override dump of built-in types) --- CHANGES.txt | 4 +++- src/SOAPpy/Config.py | 13 +++++++++++++ src/SOAPpy/SOAPBuilder.py | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 68a2561..0d690d7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,7 +4,9 @@ CHANGELOG 0.12.22 (unreleased) -------------------- -- Nothing changed yet. +- proper usage of config property inside objects. [Davorin Kunstelj] +- dump user defined types with handler functions (can be used to override dump + of built-in types). [Davorin Kunstelj] 0.12.21 (2014-05-27) diff --git a/src/SOAPpy/Config.py b/src/SOAPpy/Config.py index 24440a8..9f5e043 100644 --- a/src/SOAPpy/Config.py +++ b/src/SOAPpy/Config.py @@ -116,6 +116,13 @@ class SOAPConfig: # authorization error. self.authMethod = None + # The tuple of type and dump handler function pairs for + # SOAPBuilder dump dispatch. Used for handling additional types + # and overriding built-in types. Functions are expected to have + # the same parameters as SOAPBuilder dump_ methods + # (including self; possibility to call any SOAPBuilder dump method) + self.dumpmap = tuple() + # Globus Support if pyGlobus.io available try: from pyGlobus import io; @@ -144,6 +151,12 @@ class SOAPConfig: if d['SSLclient'] or d['SSLserver']: d['SSL'] = self.SSLconfig() + dumpmap = kw.pop("dumpmap", None) + if dumpmap: + if not isinstance(dumpmap, tuple): + raise TypeError("Config dumpmap parameter must be a tuple") + self.dumpmap = dumpmap + self.dumpmap + for k, v in kw.items(): if k[0] != '_': setattr(self, k, v) diff --git a/src/SOAPpy/SOAPBuilder.py b/src/SOAPpy/SOAPBuilder.py index cfbd5aa..0ce204b 100644 --- a/src/SOAPpy/SOAPBuilder.py +++ b/src/SOAPpy/SOAPBuilder.py @@ -596,6 +596,12 @@ class SOAPBuilder: else: tag = self.gentag() + # Apply additional types, override built-in types + for dtype, func in self.config.dumpmap: + if isinstance(obj, dtype): + func(self, obj, tag, typed, ns_map) + return + # watch out for order! dumpmap = ( (Exception, self.dump_exception),