Package suds :: Package bindings :: Module multiref
[hide private]
[frames] | no frames]

Source Code for Module suds.bindings.multiref

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 16   
 17  """ 
 18  Provides classes for handling soap multirefs. 
 19  """ 
 20   
 21  from logging import getLogger 
 22  from suds import * 
 23  from suds.sax.element import Element 
 24   
 25  log = getLogger(__name__) 
 26   
 27  soapenc = (None, 'http://schemas.xmlsoap.org/soap/encoding/') 
 28   
29 -class MultiRef:
30 """ 31 Resolves and replaces multirefs. 32 @ivar nodes: A list of non-multiref nodes. 33 @type nodes: list 34 @ivar catalog: A dictionary of multiref nodes by id. 35 @type catalog: dict 36 """ 37
38 - def __init__(self):
39 self.nodes = [] 40 self.catalog = {}
41
42 - def process(self, body):
43 """ 44 Process the specified soap envelope body and replace I{multiref} node 45 references with the contents of the referenced node. 46 @param body: A soap envelope body node. 47 @type body: L{Element} 48 @return: The processed I{body} 49 @rtype: L{Element} 50 """ 51 self.nodes = [] 52 self.catalog = {} 53 self.build_catalog(body) 54 self.update(body) 55 body.children = self.nodes 56 return body
57
58 - def update(self, node):
59 """ 60 Update the specified I{node} by replacing the I{multiref} references with 61 the contents of the referenced nodes and remove the I{href} attribute. 62 @param node: A node to update. 63 @type node: L{Element} 64 @return: The updated node 65 @rtype: L{Element} 66 """ 67 self.replace_references(node) 68 for c in node.children: 69 self.update(c) 70 return node
71
72 - def replace_references(self, node):
73 """ 74 Replacing the I{multiref} references with the contents of the 75 referenced nodes and remove the I{href} attribute. Warning: since 76 the I{ref} is not cloned, 77 @param node: A node to update. 78 @type node: L{Element} 79 """ 80 href = node.getAttribute('href') 81 if href is None: 82 return 83 id = href.getValue() 84 ref = self.catalog.get(id) 85 if ref is None: 86 log.error('soap multiref: %s, not-resolved', id) 87 return 88 node.append(ref.children) 89 node.setText(ref.getText()) 90 for a in ref.attributes: 91 if a.name != 'id': 92 node.append(a) 93 node.remove(href)
94
95 - def build_catalog(self, body):
96 """ 97 Create the I{catalog} of multiref nodes by id and the list of 98 non-multiref nodes. 99 @param body: A soap envelope body node. 100 @type body: L{Element} 101 """ 102 for child in body.children: 103 if self.soaproot(child): 104 self.nodes.append(child) 105 id = child.get('id') 106 if id is None: continue 107 key = '#%s' % id 108 self.catalog[key] = child
109
110 - def soaproot(self, node):
111 """ 112 Get whether the specified I{node} is a soap encoded root. 113 This is determined by examining @soapenc:root='1'. 114 The node is considered to be a root when the attribute 115 is not specified. 116 @param node: A node to evaluate. 117 @type node: L{Element} 118 @return: True if a soap encoded root. 119 @rtype: bool 120 """ 121 root = node.getAttribute('root', ns=soapenc) 122 if root is None: 123 return True 124 else: 125 return ( root.value == '1' )
126