Package suds :: Package sax :: Module attribute
[hide private]
[frames] | no frames]

Source Code for Module suds.sax.attribute

  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 XML I{attribute} classes. 
 19  """ 
 20   
 21  import suds.sax 
 22  from logging import getLogger 
 23  from suds import * 
 24  from suds.sax import * 
 25  from suds.sax.text import Text 
 26   
 27  log = getLogger(__name__) 
 28   
29 -class Attribute:
30 """ 31 An XML attribute object. 32 @ivar parent: The node containing this attribute 33 @type parent: L{element.Element} 34 @ivar prefix: The I{optional} namespace prefix. 35 @type prefix: basestring 36 @ivar name: The I{unqualified} name of the attribute 37 @type name: basestring 38 @ivar value: The attribute's value 39 @type value: basestring 40 """
41 - def __init__(self, name, value=None):
42 """ 43 @param name: The attribute's name with I{optional} namespace prefix. 44 @type name: basestring 45 @param value: The attribute's value 46 @type value: basestring 47 """ 48 self.parent = None 49 self.prefix, self.name = splitPrefix(name) 50 self.setValue(value)
51
52 - def clone(self, parent=None):
53 """ 54 Clone this object. 55 @param parent: The parent for the clone. 56 @type parent: L{element.Element} 57 @return: A copy of this object assigned to the new parent. 58 @rtype: L{Attribute} 59 """ 60 a = Attribute(self.qname(), self.value) 61 a.parent = parent 62 return a
63
64 - def qname(self):
65 """ 66 Get the B{fully} qualified name of this attribute 67 @return: The fully qualified name. 68 @rtype: basestring 69 """ 70 if self.prefix is None: 71 return self.name 72 else: 73 return ':'.join((self.prefix, self.name))
74
75 - def setValue(self, value):
76 """ 77 Set the attributes value 78 @param value: The new value (may be None) 79 @type value: basestring 80 @return: self 81 @rtype: L{Attribute} 82 """ 83 if isinstance(value, Text): 84 self.value = value 85 else: 86 self.value = Text(value) 87 return self
88
89 - def getValue(self, default=Text('')):
90 """ 91 Get the attributes value with optional default. 92 @param default: An optional value to be return when the 93 attribute's has not been set. 94 @type default: basestring 95 @return: The attribute's value, or I{default} 96 @rtype: L{Text} 97 """ 98 if self.hasText(): 99 return self.value 100 else: 101 return default
102
103 - def hasText(self):
104 """ 105 Get whether the attribute has I{text} and that it is not an empty 106 (zero length) string. 107 @return: True when has I{text}. 108 @rtype: boolean 109 """ 110 return ( self.value is not None and len(self.value) )
111
112 - def namespace(self):
113 """ 114 Get the attributes namespace. This may either be the namespace 115 defined by an optional prefix, or its parent's namespace. 116 @return: The attribute's namespace 117 @rtype: (I{prefix}, I{name}) 118 """ 119 if self.prefix is None: 120 return Namespace.default 121 else: 122 return self.resolvePrefix(self.prefix)
123
124 - def resolvePrefix(self, prefix):
125 """ 126 Resolve the specified prefix to a known namespace. 127 @param prefix: A declared prefix 128 @type prefix: basestring 129 @return: The namespace that has been mapped to I{prefix} 130 @rtype: (I{prefix}, I{name}) 131 """ 132 ns = Namespace.default 133 if self.parent is not None: 134 ns = self.parent.resolvePrefix(prefix) 135 return ns
136
137 - def match(self, name=None, ns=None):
138 """ 139 Match by (optional) name and/or (optional) namespace. 140 @param name: The optional attribute tag name. 141 @type name: str 142 @param ns: An optional namespace. 143 @type ns: (I{prefix}, I{name}) 144 @return: True if matched. 145 @rtype: boolean 146 """ 147 if name is None: 148 byname = True 149 else: 150 byname = ( self.name == name ) 151 if ns is None: 152 byns = True 153 else: 154 byns = ( self.namespace()[1] == ns[1] ) 155 return ( byname and byns )
156
157 - def __eq__(self, rhs):
158 """ equals operator """ 159 return rhs is not None and \ 160 isinstance(rhs, Attribute) and \ 161 self.prefix == rhs.name and \ 162 self.name == rhs.name
163
164 - def __repr__(self):
165 """ get a string representation """ 166 return \ 167 'attr (prefix=%s, name=%s, value=(%s))' %\ 168 (self.prefix, self.name, self.value)
169
170 - def __str__(self):
171 """ get an xml string representation """ 172 return unicode(self).encode('utf-8')
173
174 - def __unicode__(self):
175 """ get an xml string representation """ 176 n = self.qname() 177 if self.hasText(): 178 v = self.value.escape() 179 else: 180 v = self.value 181 return u'%s="%s"' % (n, v)
182