tidy up the suds.sax.attribute module (stylistic)

- PEP-8ified comments & made their style consistent
- used double quotes consistently
- removed unnecessary imports
- replaced star imports with explicit ones
This commit is contained in:
Jurko Gospodnetić 2014-07-02 22:06:43 +02:00
parent 2e9c22daf1
commit 34ea1f4dd0
1 changed files with 68 additions and 69 deletions

View File

@ -1,46 +1,50 @@
# This program is free software; you can redistribute it and/or modify
# it under the terms of the (LGPL) GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
# This program is free software; you can redistribute it and/or modify it under
# the terms of the (LGPL) GNU Lesser General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library Lesser General Public License for more details at
# ( http://www.gnu.org/licenses/lgpl.html ).
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License
# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )
"""
Provides XML I{attribute} classes.
"""
from suds import *
from suds.sax import *
from suds import UnicodeMixin
from suds.sax import splitPrefix, Namespace
from suds.sax.text import Text
class Attribute(UnicodeMixin):
"""
An XML attribute object.
@ivar parent: The node containing this attribute
@ivar parent: The node containing this attribute.
@type parent: L{element.Element}
@ivar prefix: The I{optional} namespace prefix.
@type prefix: basestring
@ivar name: The I{unqualified} name of the attribute
@ivar name: The I{unqualified} attribute name.
@type name: basestring
@ivar value: The attribute's value
@ivar value: The attribute's value.
@type value: basestring
"""
def __init__(self, name, value=None):
"""
@param name: The attribute's name with I{optional} namespace prefix.
@type name: basestring
@param value: The attribute's value
@param value: The attribute's value.
@type value: basestring
"""
self.parent = None
self.prefix, self.name = splitPrefix(name)
@ -49,10 +53,12 @@ class Attribute(UnicodeMixin):
def clone(self, parent=None):
"""
Clone this object.
@param parent: The parent for the clone.
@type parent: L{element.Element}
@return: A copy of this object assigned to the new parent.
@rtype: L{Attribute}
"""
a = Attribute(self.qname(), self.value)
a.parent = parent
@ -60,22 +66,25 @@ class Attribute(UnicodeMixin):
def qname(self):
"""
Get the B{fully} qualified name of this attribute
Get this attribute's B{fully} qualified name.
@return: The fully qualified name.
@rtype: basestring
"""
if self.prefix is None:
return self.name
else:
return ':'.join((self.prefix, self.name))
return ":".join((self.prefix, self.name))
def setValue(self, value):
"""
Set the attributes value
@param value: The new value (may be None)
Set the attribute's value.
@param value: The new value (may be None).
@type value: basestring
@return: self
@rtype: L{Attribute}
"""
if isinstance(value, Text):
self.value = value
@ -83,92 +92,82 @@ class Attribute(UnicodeMixin):
self.value = Text(value)
return self
def getValue(self, default=Text('')):
def getValue(self, default=Text("")):
"""
Get the attributes value with optional default.
@param default: An optional value to be return when the
attribute's has not been set.
@param default: An optional value to return when the attribute's value
has not been set.
@type default: basestring
@return: The attribute's value, or I{default}
@return: The attribute's value, or I{default}.
@rtype: L{Text}
"""
if self.hasText():
return self.value
else:
return default
return self.value or default
def hasText(self):
"""
Get whether the attribute has I{text} and that it is not an empty
(zero length) string.
Get whether the attribute has a non-empty I{text} string value.
@return: True when has I{text}.
@rtype: boolean
"""
return ( self.value is not None and len(self.value) )
return bool(self.value)
def namespace(self):
"""
Get the attributes namespace. This may either be the namespace
defined by an optional prefix, or its parent's namespace.
@return: The attribute's namespace
Get the attribute's namespace. This may either be the namespace defined
by an optional prefix, or the default namespace.
@return: The attribute's namespace.
@rtype: (I{prefix}, I{name})
"""
if self.prefix is None:
return Namespace.default
else:
return self.resolvePrefix(self.prefix)
return self.resolvePrefix(self.prefix)
def resolvePrefix(self, prefix):
"""
Resolve the specified prefix to a known namespace.
@param prefix: A declared prefix
@param prefix: A declared prefix.
@type prefix: basestring
@return: The namespace that has been mapped to I{prefix}
@return: The namespace mapped to I{prefix}.
@rtype: (I{prefix}, I{name})
"""
ns = Namespace.default
if self.parent is not None:
ns = self.parent.resolvePrefix(prefix)
return ns
if self.parent is None:
return Namespace.default
return self.parent.resolvePrefix(prefix)
def match(self, name=None, ns=None):
"""
Match by (optional) name and/or (optional) namespace.
@param name: The optional attribute tag name.
@type name: str
@param ns: An optional namespace.
@type ns: (I{prefix}, I{name})
@return: True if matched.
@rtype: boolean
"""
if name is None:
byname = True
else:
byname = ( self.name == name )
if ns is None:
byns = True
else:
byns = ( self.namespace()[1] == ns[1] )
return ( byname and byns )
byname = name is None or (self.name == name)
byns = ns is None or (self.namespace()[1] == ns[1])
return byname and byns
def __eq__(self, rhs):
""" equals operator """
return rhs is not None and \
isinstance(rhs, Attribute) and \
self.prefix == rhs.name and \
self.name == rhs.name
"""Equals operator."""
return (isinstance(rhs, Attribute) and self.prefix == rhs.name and
self.name == rhs.name)
def __repr__(self):
""" get a string representation """
return \
'attr (prefix=%s, name=%s, value=(%s))' %\
(self.prefix, self.name, self.value)
"""Programmer friendly string representation."""
return "attr (prefix=%s, name=%s, value=(%s))" % (self.prefix,
self.name, self.value)
def __unicode__(self):
""" get an xml string representation """
n = self.qname()
if self.hasText():
v = self.value.escape()
else:
v = self.value
return u'%s="%s"' % (n, v)
"""XML string representation."""
return u'%s="%s"' % (self.qname(), self.value and self.value.escape())