signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def __init__(self, offset):
self.__offset = offset<EOL>
Constructor. @ivar offset: The fixed offset of the timezone. @type offset: B{datetime}.I{timedelta}
f9705:c3:m0
def utcoffset(self, dt):
return self.__offset<EOL>
http://docs.python.org/library/datetime.html#datetime.tzinfo.utcoffset
f9705:c3:m1
def tzname(self, dt):
sign = '<STR_LIT:+>'<EOL>if self.__offset < datetime.timedelta():<EOL><INDENT>sign = '<STR_LIT:->'<EOL><DEDENT>if hasattr(self.__offset, '<STR_LIT>'):<EOL><INDENT>total_seconds = self.__offset.total_seconds()<EOL><DEDENT>else:<EOL><INDENT>total_seconds = (self.__offset.days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT>) +(self.__offset.seconds) +(self.__offset.microseconds / <NUM_LIT>)<EOL><DEDENT>hours = total_seconds // (<NUM_LIT> * <NUM_LIT>)<EOL>total_seconds -= hours * <NUM_LIT> * <NUM_LIT><EOL>minutes = total_seconds // <NUM_LIT><EOL>total_seconds -= minutes * <NUM_LIT><EOL>seconds = total_seconds // <NUM_LIT:1><EOL>total_seconds -= seconds<EOL>if seconds:<EOL><INDENT>return '<STR_LIT>' % (sign, hours, minutes, seconds)<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>' % (sign, hours, minutes)<EOL><DEDENT>
http://docs.python.org/library/datetime.html#datetime.tzinfo.tzname
f9705:c3:m2
def dst(self, dt):
return datetime.timedelta(<NUM_LIT:0>)<EOL>
http://docs.python.org/library/datetime.html#datetime.tzinfo.dst
f9705:c3:m3
def __init__(self):
FixedOffsetTimezone.__init__(self, datetime.timedelta(<NUM_LIT:0>))<EOL>
Constructor.
f9705:c4:m0
def tzname(self, dt):
return '<STR_LIT>'<EOL>
http://docs.python.org/library/datetime.html#datetime.tzinfo.tzname
f9705:c4:m1
def __init__(self):
self.__offset = datetime.timedelta(seconds=-time.timezone)<EOL>if time.daylight:<EOL><INDENT>self.__dst_offset = datetime.timedelta(seconds=-time.altzone)<EOL><DEDENT>else:<EOL><INDENT>self.__dst_offset = None<EOL><DEDENT>
Constructor.
f9705:c5:m0
def utcoffset(self, dt):
if self.__is_daylight_time(dt):<EOL><INDENT>return self.__dst_offset<EOL><DEDENT>else:<EOL><INDENT>return self.__offset<EOL><DEDENT>
http://docs.python.org/library/datetime.html#datetime.tzinfo.utcoffset
f9705:c5:m1
def dst(self, dt):
if self.__is_daylight_time(dt):<EOL><INDENT>return self.__dst_offset - self.__offset<EOL><DEDENT>else:<EOL><INDENT>return datetime.timedelta(<NUM_LIT:0>)<EOL><DEDENT>
http://docs.python.org/library/datetime.html#datetime.tzinfo.dst
f9705:c5:m2
def tzname(self, dt):
if self.__is_daylight_time(dt):<EOL><INDENT>return time.tzname[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>return time.tzname[<NUM_LIT:0>]<EOL><DEDENT>
http://docs.python.org/library/datetime.html#datetime.tzinfo.tzname
f9705:c5:m3
def escape(self):
if not self.escaped:<EOL><INDENT>post = sax.encoder.encode(self)<EOL>escaped = post != self<EOL>return Text(post, lang=self.lang, escaped=escaped)<EOL><DEDENT>return self<EOL>
Encode (escape) special XML characters. @return: The text with XML special characters escaped. @rtype: L{Text}
f9706:c0:m2
def unescape(self):
if self.escaped:<EOL><INDENT>post = sax.encoder.decode(self)<EOL>return Text(post, lang=self.lang)<EOL><DEDENT>return self<EOL>
Decode (unescape) special XML characters. @return: The text with escaped XML special characters decoded. @rtype: L{Text}
f9706:c0:m3
def splitPrefix(name):
if isinstance(name, str) and '<STR_LIT::>' in name:<EOL><INDENT>return tuple(name.split('<STR_LIT::>', <NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>return (None, name)<EOL><DEDENT>
Split the name into a tuple (I{prefix}, I{name}). The first element in the tuple is I{None} when the name does't have a prefix. @param name: A node name containing an optional prefix. @type name: basestring @return: A tuple containing the (2) parts of I{name} @rtype: (I{prefix}, I{name})
f9707:m0
def needsEncoding(self, s):
if isinstance(s, str):<EOL><INDENT>for c in self.special:<EOL><INDENT>if c in s:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>
Get whether string I{s} contains special characters. @param s: A string to check. @type s: str @return: True if needs encoding. @rtype: boolean
f9708:c0:m0
def encode(self, s):
if isinstance(s, str) and self.needsEncoding(s):<EOL><INDENT>for x in self.encodings:<EOL><INDENT>s = re.sub(x[<NUM_LIT:0>], x[<NUM_LIT:1>], s)<EOL><DEDENT><DEDENT>return s<EOL>
Encode special characters found in string I{s}. @param s: A string to encode. @type s: str @return: The encoded string. @rtype: str
f9708:c0:m1
def decode(self, s):
if isinstance(s, str) and '<STR_LIT:&>' in s:<EOL><INDENT>for x in self.decodings:<EOL><INDENT>s = s.replace(x[<NUM_LIT:0>], x[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>return s<EOL>
Decode special characters encodings found in string I{s}. @param s: A string to decode. @type s: str @return: The decoded string. @rtype: str
f9708:c0:m2
def __init__(self, name, value=None):
self.parent = None<EOL>self.prefix, self.name = splitPrefix(name)<EOL>self.setValue(value)<EOL>
@param name: The attribute's name with I{optional} namespace prefix. @type name: basestring @param value: The attribute's value @type value: basestring
f9709:c0:m0
def clone(self, parent=None):
a = Attribute(self.qname(), self.value)<EOL>a.parent = parent<EOL>return a<EOL>
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}
f9709:c0:m1
def qname(self):
if self.prefix is None:<EOL><INDENT>return self.name<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT::>'.join((self.prefix, self.name))<EOL><DEDENT>
Get the B{fully} qualified name of this attribute @return: The fully qualified name. @rtype: basestring
f9709:c0:m2
def setValue(self, value):
if isinstance(value, Text):<EOL><INDENT>self.value = value<EOL><DEDENT>else:<EOL><INDENT>self.value = Text(value)<EOL><DEDENT>return self<EOL>
Set the attributes value @param value: The new value (may be None) @type value: basestring @return: self @rtype: L{Attribute}
f9709:c0:m3
def getValue(self, default=Text('<STR_LIT>')):
if self.hasText():<EOL><INDENT>return self.value<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT>
Get the attributes value with optional default. @param default: An optional value to be return when the attribute's has not been set. @type default: basestring @return: The attribute's value, or I{default} @rtype: L{Text}
f9709:c0:m4
def hasText(self):
return self.value is not None and len(self.value)<EOL>
Get whether the attribute has I{text} and that it is not an empty (zero length) string. @return: True when has I{text}. @rtype: boolean
f9709:c0:m5
def namespace(self):
if self.prefix is None:<EOL><INDENT>return Namespace.default<EOL><DEDENT>else:<EOL><INDENT>return self.resolvePrefix(self.prefix)<EOL><DEDENT>
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 @rtype: (I{prefix}, I{name})
f9709:c0:m6
def resolvePrefix(self, prefix):
ns = Namespace.default<EOL>if self.parent is not None:<EOL><INDENT>ns = self.parent.resolvePrefix(prefix)<EOL><DEDENT>return ns<EOL>
Resolve the specified prefix to a known namespace. @param prefix: A declared prefix @type prefix: basestring @return: The namespace that has been mapped to I{prefix} @rtype: (I{prefix}, I{name})
f9709:c0:m7
def match(self, name=None, ns=None):
if name is None:<EOL><INDENT>byname = True<EOL><DEDENT>else:<EOL><INDENT>byname = self.name == name<EOL><DEDENT>if ns is None:<EOL><INDENT>byns = True<EOL><DEDENT>else:<EOL><INDENT>byns = self.namespace()[<NUM_LIT:1>] == ns[<NUM_LIT:1>]<EOL><DEDENT>return byname and byns<EOL>
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
f9709:c0:m8
def __eq__(self, rhs):
return rhs is not None andisinstance(rhs, Attribute) andself.prefix == rhs.name andself.name == rhs.name<EOL>
equals operator
f9709:c0:m9
def __repr__(self):
return '<STR_LIT>' % (<EOL>self.prefix,<EOL>self.name,<EOL>self.value)<EOL>
get a string representation
f9709:c0:m10
def __str__(self):
n = self.qname()<EOL>if self.hasText():<EOL><INDENT>v = self.value.escape()<EOL><DEDENT>else:<EOL><INDENT>v = self.value<EOL><DEDENT>return u'<STR_LIT>' % (n, v)<EOL>
get an xml string representation
f9709:c0:m11
def __unicode__(self):
n = self.qname()<EOL>if self.hasText():<EOL><INDENT>v = self.value.escape()<EOL><DEDENT>else:<EOL><INDENT>v = self.value<EOL><DEDENT>return u'<STR_LIT>' % (n, v)<EOL>
get an xml string representation
f9709:c0:m12
@classmethod<EOL><INDENT>def buildPath(self, parent, path):<DEDENT>
for tag in path.split('<STR_LIT:/>'):<EOL><INDENT>child = parent.getChild(tag)<EOL>if child is None:<EOL><INDENT>child = Element(tag, parent)<EOL><DEDENT>parent = child<EOL><DEDENT>return child<EOL>
Build the specifed pat as a/b/c where missing intermediate nodes are built automatically. @param parent: A parent element on which the path is built. @type parent: I{Element} @param path: A simple path separated by (/). @type path: basestring @return: The leaf node of I{path}. @rtype: L{Element}
f9710:c0:m0
def __init__(self, name, parent=None, ns=None):
self.rename(name)<EOL>self.expns = None<EOL>self.nsprefixes = {}<EOL>self.attributes = []<EOL>self.text = None<EOL>if parent is not None:<EOL><INDENT>if isinstance(parent, Element):<EOL><INDENT>self.parent = parent<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>', parent.__class__.__name__)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.parent = None<EOL><DEDENT>self.children = []<EOL>self.applyns(ns)<EOL>
@param name: The element's (tag) name. May cotain a prefix. @type name: basestring @param parent: An optional parent element. @type parent: I{Element} @param ns: An optional namespace @type ns: (I{prefix}, I{name})
f9710:c0:m1
def rename(self, name):
if name is None:<EOL><INDENT>raise Exception('<STR_LIT>' % name)<EOL><DEDENT>else:<EOL><INDENT>self.prefix, self.name = splitPrefix(name)<EOL><DEDENT>
Rename the element. @param name: A new name for the element. @type name: basestring
f9710:c0:m2
def setPrefix(self, p, u=None):
self.prefix = p<EOL>if p is not None and u is not None:<EOL><INDENT>self.addPrefix(p, u)<EOL><DEDENT>return self<EOL>
Set the element namespace prefix. @param p: A new prefix for the element. @type p: basestring @param u: A namespace URI to be mapped to the prefix. @type u: basestring @return: self @rtype: L{Element}
f9710:c0:m3
def qname(self):
if self.prefix is None:<EOL><INDENT>return self.name<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT>' % (self.prefix, self.name)<EOL><DEDENT>
Get the B{fully} qualified name of this element @return: The fully qualified name. @rtype: basestring
f9710:c0:m4
def getRoot(self):
if self.parent is None:<EOL><INDENT>return self<EOL><DEDENT>else:<EOL><INDENT>return self.parent.getRoot()<EOL><DEDENT>
Get the root (top) node of the tree. @return: The I{top} node of this tree. @rtype: I{Element}
f9710:c0:m5
def clone(self, parent=None):
root = Element(self.qname(), parent, self.namespace())<EOL>for a in self.attributes:<EOL><INDENT>root.append(a.clone(self))<EOL><DEDENT>for c in self.children:<EOL><INDENT>root.append(c.clone(self))<EOL><DEDENT>for item in self.nsprefixes.items():<EOL><INDENT>root.addPrefix(item[<NUM_LIT:0>], item[<NUM_LIT:1>])<EOL><DEDENT>return root<EOL>
Deep clone of this element and children. @param parent: An optional parent for the copied fragment. @type parent: I{Element} @return: A deep copy parented by I{parent} @rtype: I{Element}
f9710:c0:m6
def detach(self):
if self.parent is not None:<EOL><INDENT>if self in self.parent.children:<EOL><INDENT>self.parent.children.remove(self)<EOL><DEDENT>self.parent = None<EOL><DEDENT>return self<EOL>
Detach from parent. @return: This element removed from its parent's child list and I{parent}=I{None} @rtype: L{Element}
f9710:c0:m7
def set(self, name, value):
attr = self.getAttribute(name)<EOL>if attr is None:<EOL><INDENT>attr = Attribute(name, value)<EOL>self.append(attr)<EOL><DEDENT>else:<EOL><INDENT>attr.setValue(value)<EOL><DEDENT>
Set an attribute's value. @param name: The name of the attribute. @type name: basestring @param value: The attribute value. @type value: basestring @see: __setitem__()
f9710:c0:m8
def unset(self, name):
try:<EOL><INDENT>attr = self.getAttribute(name)<EOL>self.attributes.remove(attr)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>return self<EOL>
Unset (remove) an attribute. @param name: The attribute name. @type name: str @return: self @rtype: L{Element}
f9710:c0:m9
def get(self, name, ns=None, default=None):
attr = self.getAttribute(name, ns)<EOL>if attr is None or attr.value is None:<EOL><INDENT>return default<EOL><DEDENT>else:<EOL><INDENT>return attr.getValue()<EOL><DEDENT>
Get the value of an attribute by name. @param name: The name of the attribute. @type name: basestring @param ns: The optional attribute's namespace. @type ns: (I{prefix}, I{name}) @param default: An optional value to be returned when either the attribute does not exist of has not value. @type default: basestring @return: The attribute's value or I{default} @rtype: basestring @see: __getitem__()
f9710:c0:m10
def setText(self, value):
if isinstance(value, Text):<EOL><INDENT>self.text = value<EOL><DEDENT>else:<EOL><INDENT>self.text = Text(value)<EOL><DEDENT>return self<EOL>
Set the element's L{Text} content. @param value: The element's text value. @type value: basestring @return: self @rtype: I{Element}
f9710:c0:m11
def getText(self, default=None):
if self.hasText():<EOL><INDENT>return self.text<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT>
Get the element's L{Text} content with optional default @param default: A value to be returned when no text content exists. @type default: basestring @return: The text content, or I{default} @rtype: L{Text}
f9710:c0:m12
def trim(self):
if self.hasText():<EOL><INDENT>self.text = self.text.trim()<EOL><DEDENT>return self<EOL>
Trim leading and trailing whitespace. @return: self @rtype: L{Element}
f9710:c0:m13
def hasText(self):
return self.text is not None and len(self.text)<EOL>
Get whether the element has I{text} and that it is not an empty (zero length) string. @return: True when has I{text}. @rtype: boolean
f9710:c0:m14
def namespace(self):
if self.prefix is None:<EOL><INDENT>return self.defaultNamespace()<EOL><DEDENT>else:<EOL><INDENT>return self.resolvePrefix(self.prefix)<EOL><DEDENT>
Get the element's namespace. @return: The element's namespace by resolving the prefix, the explicit namespace or the inherited namespace. @rtype: (I{prefix}, I{name})
f9710:c0:m15
def defaultNamespace(self):
p = self<EOL>while p is not None:<EOL><INDENT>if p.expns is not None:<EOL><INDENT>return (None, p.expns)<EOL><DEDENT>else:<EOL><INDENT>p = p.parent<EOL><DEDENT><DEDENT>return Namespace.default<EOL>
Get the default (unqualified namespace). This is the expns of the first node (looking up the tree) that has it set. @return: The namespace of a node when not qualified. @rtype: (I{prefix}, I{name})
f9710:c0:m16
def append(self, objects):
if not isinstance(objects, (list, tuple)):<EOL><INDENT>objects = (objects,)<EOL><DEDENT>for child in objects:<EOL><INDENT>if isinstance(child, Element):<EOL><INDENT>self.children.append(child)<EOL>child.parent = self<EOL>continue<EOL><DEDENT>if isinstance(child, Attribute):<EOL><INDENT>self.attributes.append(child)<EOL>child.parent = self<EOL>continue<EOL><DEDENT>raise Exception('<STR_LIT>' % child.__class__.__name__)<EOL><DEDENT>return self<EOL>
Append the specified child based on whether it is an element or an attrbuite. @param objects: A (single|collection) of attribute(s) or element(s) to be added as children. @type objects: (L{Element}|L{Attribute}) @return: self @rtype: L{Element}
f9710:c0:m17
def insert(self, objects, index=<NUM_LIT:0>):
objects = (objects,)<EOL>for child in objects:<EOL><INDENT>if isinstance(child, Element):<EOL><INDENT>self.children.insert(index, child)<EOL>child.parent = self<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' % child.__class__.__name__)<EOL><DEDENT><DEDENT>return self<EOL>
Insert an L{Element} content at the specified index. @param objects: A (single|collection) of attribute(s) or element(s) to be added as children. @type objects: (L{Element}|L{Attribute}) @param index: The position in the list of children to insert. @type index: int @return: self @rtype: L{Element}
f9710:c0:m18
def remove(self, child):
if isinstance(child, Element):<EOL><INDENT>return child.detach()<EOL><DEDENT>if isinstance(child, Attribute):<EOL><INDENT>self.attributes.remove(child)<EOL><DEDENT>return None<EOL>
Remove the specified child element or attribute. @param child: A child to remove. @type child: L{Element}|L{Attribute} @return: The detached I{child} when I{child} is an element, else None. @rtype: L{Element}|None
f9710:c0:m19
def replaceChild(self, child, content):
if child not in self.children:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>index = self.children.index(child)<EOL>self.remove(child)<EOL>if not isinstance(content, (list, tuple)):<EOL><INDENT>content = (content,)<EOL><DEDENT>for node in content:<EOL><INDENT>self.children.insert(index, node.detach())<EOL>node.parent = self<EOL>index += <NUM_LIT:1><EOL><DEDENT>
Replace I{child} with the specified I{content}. @param child: A child element. @type child: L{Element} @param content: An element or collection of elements. @type content: L{Element} or [L{Element},]
f9710:c0:m20
def getAttribute(self, name, ns=None, default=None):
if ns is None:<EOL><INDENT>prefix, name = splitPrefix(name)<EOL>if prefix is None:<EOL><INDENT>ns = None<EOL><DEDENT>else:<EOL><INDENT>ns = self.resolvePrefix(prefix)<EOL><DEDENT><DEDENT>for a in self.attributes:<EOL><INDENT>if a.match(name, ns):<EOL><INDENT>return a<EOL><DEDENT><DEDENT>return default<EOL>
Get an attribute by name and (optional) namespace @param name: The name of a contained attribute (may contain prefix). @type name: basestring @param ns: An optional namespace @type ns: (I{prefix}, I{name}) @param default: Returned when attribute not-found. @type default: L{Attribute} @return: The requested attribute object. @rtype: L{Attribute}
f9710:c0:m21
def getChild(self, name, ns=None, default=None):
if ns is None:<EOL><INDENT>prefix, name = splitPrefix(name)<EOL>if prefix is None:<EOL><INDENT>ns = None<EOL><DEDENT>else:<EOL><INDENT>ns = self.resolvePrefix(prefix)<EOL><DEDENT><DEDENT>for c in self.children:<EOL><INDENT>if c.match(name, ns):<EOL><INDENT>return c<EOL><DEDENT><DEDENT>return default<EOL>
Get a child by (optional) name and/or (optional) namespace. @param name: The name of a child element (may contain prefix). @type name: basestring @param ns: An optional namespace used to match the child. @type ns: (I{prefix}, I{name}) @param default: Returned when child not-found. @type default: L{Element} @return: The requested child, or I{default} when not-found. @rtype: L{Element}
f9710:c0:m22
def childAtPath(self, path):
result = None<EOL>node = self<EOL>for name in [p for p in path.split('<STR_LIT:/>') if len(p) > <NUM_LIT:0>]:<EOL><INDENT>ns = None<EOL>prefix, name = splitPrefix(name)<EOL>if prefix is not None:<EOL><INDENT>ns = node.resolvePrefix(prefix)<EOL><DEDENT>result = node.getChild(name, ns)<EOL>if result is None:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>node = result<EOL><DEDENT><DEDENT>return result<EOL>
Get a child at I{path} where I{path} is a (/) separated list of element names that are expected to be children. @param path: A (/) separated list of element names. @type path: basestring @return: The leaf node at the end of I{path} @rtype: L{Element}
f9710:c0:m23
def childrenAtPath(self, path):
parts = [p for p in path.split('<STR_LIT:/>') if len(p) > <NUM_LIT:0>]<EOL>if len(parts) == <NUM_LIT:1>:<EOL><INDENT>result = self.getChildren(path)<EOL><DEDENT>else:<EOL><INDENT>result = self.__childrenAtPath(parts)<EOL><DEDENT>return result<EOL>
Get a list of children at I{path} where I{path} is a (/) separated list of element names that are expected to be children. @param path: A (/) separated list of element names. @type path: basestring @return: The collection leaf nodes at the end of I{path} @rtype: [L{Element},...]
f9710:c0:m24
def getChildren(self, name=None, ns=None):
if ns is None:<EOL><INDENT>if name is None:<EOL><INDENT>return self.children<EOL><DEDENT>prefix, name = splitPrefix(name)<EOL>if prefix is None:<EOL><INDENT>ns = None<EOL><DEDENT>else:<EOL><INDENT>ns = self.resolvePrefix(prefix)<EOL><DEDENT><DEDENT>return [c for c in self.children if c.match(name, ns)]<EOL>
Get a list of children by (optional) name and/or (optional) namespace. @param name: The name of a child element (may contain prefix). @type name: basestring @param ns: An optional namespace used to match the child. @type ns: (I{prefix}, I{name}) @return: The list of matching children. @rtype: [L{Element},...]
f9710:c0:m25
def detachChildren(self):
detached = self.children<EOL>self.children = []<EOL>for child in detached:<EOL><INDENT>child.parent = None<EOL><DEDENT>return detached<EOL>
Detach and return this element's children. @return: The element's children (detached). @rtype: [L{Element},...]
f9710:c0:m26
def resolvePrefix(self, prefix, default=Namespace.default):
n = self<EOL>while n is not None:<EOL><INDENT>if prefix in n.nsprefixes:<EOL><INDENT>return (prefix, n.nsprefixes[prefix])<EOL><DEDENT>if prefix in self.specialprefixes:<EOL><INDENT>return (prefix, self.specialprefixes[prefix])<EOL><DEDENT>n = n.parent<EOL><DEDENT>return default<EOL>
Resolve the specified prefix to a namespace. The I{nsprefixes} is searched. If not found, it walks up the tree until either resolved or the top of the tree is reached. Searching up the tree provides for inherited mappings. @param prefix: A namespace prefix to resolve. @type prefix: basestring @param default: An optional value to be returned when the prefix cannot be resolved. @type default: (I{prefix},I{URI}) @return: The namespace that is mapped to I{prefix} in this context. @rtype: (I{prefix},I{URI})
f9710:c0:m27
def addPrefix(self, p, u):
self.nsprefixes[p] = u<EOL>return self<EOL>
Add or update a prefix mapping. @param p: A prefix. @type p: basestring @param u: A namespace URI. @type u: basestring @return: self @rtype: L{Element}
f9710:c0:m28
def updatePrefix(self, p, u):
if p in self.nsprefixes:<EOL><INDENT>self.nsprefixes[p] = u<EOL><DEDENT>for c in self.children:<EOL><INDENT>c.updatePrefix(p, u)<EOL><DEDENT>return self<EOL>
Update (redefine) a prefix mapping for the branch. @param p: A prefix. @type p: basestring @param u: A namespace URI. @type u: basestring @return: self @rtype: L{Element} @note: This method traverses down the entire branch!
f9710:c0:m29
def clearPrefix(self, prefix):
if prefix in self.nsprefixes:<EOL><INDENT>del self.nsprefixes[prefix]<EOL><DEDENT>return self<EOL>
Clear the specified prefix from the prefix mappings. @param prefix: A prefix to clear. @type prefix: basestring @return: self @rtype: L{Element}
f9710:c0:m30
def findPrefix(self, uri, default=None):
for item in self.nsprefixes.items():<EOL><INDENT>if item[<NUM_LIT:1>] == uri:<EOL><INDENT>prefix = item[<NUM_LIT:0>]<EOL>return prefix<EOL><DEDENT><DEDENT>for item in self.specialprefixes.items():<EOL><INDENT>if item[<NUM_LIT:1>] == uri:<EOL><INDENT>prefix = item[<NUM_LIT:0>]<EOL>return prefix<EOL><DEDENT><DEDENT>if self.parent is not None:<EOL><INDENT>return self.parent.findPrefix(uri, default)<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT>
Find the first prefix that has been mapped to a namespace URI. The local mapping is searched, then it walks up the tree until it reaches the top or finds a match. @param uri: A namespace URI. @type uri: basestring @param default: A default prefix when not found. @type default: basestring @return: A mapped prefix. @rtype: basestring
f9710:c0:m31
def findPrefixes(self, uri, match='<STR_LIT>'):
result = []<EOL>for item in self.nsprefixes.items():<EOL><INDENT>if self.matcher[match](item[<NUM_LIT:1>], uri):<EOL><INDENT>prefix = item[<NUM_LIT:0>]<EOL>result.append(prefix)<EOL><DEDENT><DEDENT>for item in self.specialprefixes.items():<EOL><INDENT>if self.matcher[match](item[<NUM_LIT:1>], uri):<EOL><INDENT>prefix = item[<NUM_LIT:0>]<EOL>result.append(prefix)<EOL><DEDENT><DEDENT>if self.parent is not None:<EOL><INDENT>result += self.parent.findPrefixes(uri, match)<EOL><DEDENT>return result<EOL>
Find all prefixes that has been mapped to a namespace URI. The local mapping is searched, then it walks up the tree until it reaches the top collecting all matches. @param uri: A namespace URI. @type uri: basestring @param match: A matching function L{Element.matcher}. @type match: basestring @return: A list of mapped prefixes. @rtype: [basestring,...]
f9710:c0:m32
def promotePrefixes(self):
for c in self.children:<EOL><INDENT>c.promotePrefixes()<EOL><DEDENT>if self.parent is None:<EOL><INDENT>return<EOL><DEDENT>_pref = []<EOL>for p, u in self.nsprefixes.items():<EOL><INDENT>if p in self.parent.nsprefixes:<EOL><INDENT>pu = self.parent.nsprefixes[p]<EOL>if pu == u:<EOL><INDENT>_pref.append(p)<EOL><DEDENT>continue<EOL><DEDENT>if p != self.parent.prefix:<EOL><INDENT>self.parent.nsprefixes[p] = u<EOL>_pref.append(p)<EOL><DEDENT><DEDENT>for x in _pref:<EOL><INDENT>del self.nsprefixes[x]<EOL><DEDENT>return self<EOL>
Push prefix declarations up the tree as far as possible. Prefix mapping are pushed to its parent unless the parent has the prefix mapped to another URI or the parent has the prefix. This is propagated up the tree until the top is reached. @return: self @rtype: L{Element}
f9710:c0:m33
def refitPrefixes(self):
for c in self.children:<EOL><INDENT>c.refitPrefixes()<EOL><DEDENT>if self.prefix is not None:<EOL><INDENT>ns = self.resolvePrefix(self.prefix)<EOL>if ns[<NUM_LIT:1>] is not None:<EOL><INDENT>self.expns = ns[<NUM_LIT:1>]<EOL><DEDENT><DEDENT>self.prefix = None<EOL>self.nsprefixes = {}<EOL>return self<EOL>
Refit namespace qualification by replacing prefixes with explicit namespaces. Also purges prefix mapping table. @return: self @rtype: L{Element}
f9710:c0:m34
def normalizePrefixes(self):
PrefixNormalizer.apply(self)<EOL>return self<EOL>
Normalize the namespace prefixes. This generates unique prefixes for all namespaces. Then retrofits all prefixes and prefix mappings. Further, it will retrofix attribute values that have values containing (:). @return: self @rtype: L{Element}
f9710:c0:m35
def isempty(self, content=True):
noattrs = not len(self.attributes)<EOL>nochildren = not len(self.children)<EOL>notext = self.text is None<EOL>nocontent = nochildren and notext<EOL>if content:<EOL><INDENT>return nocontent<EOL><DEDENT>else:<EOL><INDENT>return nocontent and noattrs<EOL><DEDENT>
Get whether the element has no children. @param content: Test content (children & text) only. @type content: boolean @return: True when element has not children. @rtype: boolean
f9710:c0:m36
def isnil(self):
nilattr = self.getAttribute('<STR_LIT>', ns=Namespace.xsins)<EOL>if nilattr is None:<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return nilattr.getValue().lower() == '<STR_LIT:true>'<EOL><DEDENT>
Get whether the element is I{nil} as defined by having an attribute in the I{xsi:nil="true"} @return: True if I{nil}, else False @rtype: boolean
f9710:c0:m37
def setnil(self, flag=True):
p, u = Namespace.xsins<EOL>name = '<STR_LIT::>'.join((p, '<STR_LIT>'))<EOL>self.set(name, str(flag).lower())<EOL>self.addPrefix(p, u)<EOL>if flag:<EOL><INDENT>self.text = None<EOL><DEDENT>return self<EOL>
Set this node to I{nil} as defined by having an attribute I{xsi:nil}=I{flag}. @param flag: A flag inidcating how I{xsi:nil} will be set. @type flag: boolean @return: self @rtype: L{Element}
f9710:c0:m38
def applyns(self, ns):
if ns is None:<EOL><INDENT>return<EOL><DEDENT>if not isinstance(ns, (tuple, list)):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if ns[<NUM_LIT:0>] is None:<EOL><INDENT>self.expns = ns[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>self.prefix = ns[<NUM_LIT:0>]<EOL>self.nsprefixes[ns[<NUM_LIT:0>]] = ns[<NUM_LIT:1>]<EOL><DEDENT>
Apply the namespace to this node. If the prefix is I{None} then this element's explicit namespace I{expns} is set to the URI defined by I{ns}. Otherwise, the I{ns} is simply mapped. @param ns: A namespace. @type ns: (I{prefix},I{URI})
f9710:c0:m39
def str(self, indent=<NUM_LIT:0>):
tab = '<STR_LIT>' % (indent * <NUM_LIT:3>, '<STR_LIT>')<EOL>result = []<EOL>result.append('<STR_LIT>' % (tab, self.qname()))<EOL>result.append(self.nsdeclarations())<EOL>for a in [unicode(a) for a in self.attributes]:<EOL><INDENT>result.append('<STR_LIT>' % a)<EOL><DEDENT>if self.isempty():<EOL><INDENT>result.append('<STR_LIT>')<EOL>return '<STR_LIT>'.join(result)<EOL><DEDENT>result.append('<STR_LIT:>>')<EOL>if self.hasText():<EOL><INDENT>result.append(self.text.escape())<EOL><DEDENT>for c in self.children:<EOL><INDENT>result.append('<STR_LIT:\n>')<EOL>result.append(c.str(indent+<NUM_LIT:1>))<EOL><DEDENT>if len(self.children):<EOL><INDENT>result.append('<STR_LIT>' % tab)<EOL><DEDENT>result.append('<STR_LIT>' % self.qname())<EOL>result = '<STR_LIT>'.join(result)<EOL>return result<EOL>
Get a string representation of this XML fragment. @param indent: The indent to be used in formatting the output. @type indent: int @return: A I{pretty} string. @rtype: basestring
f9710:c0:m40
def plain(self):
result = []<EOL>result.append('<STR_LIT>' % self.qname())<EOL>result.append(self.nsdeclarations())<EOL>for a in [unicode(a) for a in self.attributes]:<EOL><INDENT>result.append('<STR_LIT>' % a)<EOL><DEDENT>if self.isempty():<EOL><INDENT>result.append('<STR_LIT>')<EOL>return '<STR_LIT>'.join(result)<EOL><DEDENT>result.append('<STR_LIT:>>')<EOL>if self.hasText():<EOL><INDENT>result.append(self.text.escape())<EOL><DEDENT>for c in self.children:<EOL><INDENT>result.append(c.plain())<EOL><DEDENT>result.append('<STR_LIT>' % self.qname())<EOL>result = '<STR_LIT>'.join(result)<EOL>return result<EOL>
Get a string representation of this XML fragment. @return: A I{plain} string. @rtype: basestring
f9710:c0:m41
def nsdeclarations(self):
s = []<EOL>myns = (None, self.expns)<EOL>if self.parent is None:<EOL><INDENT>pns = Namespace.default<EOL><DEDENT>else:<EOL><INDENT>pns = (None, self.parent.expns)<EOL><DEDENT>if myns[<NUM_LIT:1>] != pns[<NUM_LIT:1>]:<EOL><INDENT>if self.expns is not None:<EOL><INDENT>d = '<STR_LIT>' % self.expns<EOL>s.append(d)<EOL><DEDENT><DEDENT>for p, u in self.nsprefixes.items():<EOL><INDENT>if self.parent is not None:<EOL><INDENT>ns = self.parent.resolvePrefix(p)<EOL>if ns[<NUM_LIT:1>] == u:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>d = '<STR_LIT>' % (p, u)<EOL>s.append(d)<EOL><DEDENT>return '<STR_LIT>'.join(s)<EOL>
Get a string representation for all namespace declarations as xmlns="" and xmlns:p="". @return: A separated list of declarations. @rtype: basestring
f9710:c0:m42
def match(self, name=None, ns=None):
if name is None:<EOL><INDENT>byname = True<EOL><DEDENT>else:<EOL><INDENT>byname = self.name == name<EOL><DEDENT>if ns is None:<EOL><INDENT>byns = True<EOL><DEDENT>else:<EOL><INDENT>byns = self.namespace()[<NUM_LIT:1>] == ns[<NUM_LIT:1>]<EOL><DEDENT>return byname and byns<EOL>
Match by (optional) name and/or (optional) namespace. @param name: The optional element tag name. @type name: str @param ns: An optional namespace. @type ns: (I{prefix}, I{name}) @return: True if matched. @rtype: boolean
f9710:c0:m43
def branch(self):
branch = [self]<EOL>for c in self.children:<EOL><INDENT>branch += c.branch()<EOL><DEDENT>return branch<EOL>
Get a flattened representation of the branch. @return: A flat list of nodes. @rtype: [L{Element},..]
f9710:c0:m44
def ancestors(self):
ancestors = []<EOL>p = self.parent<EOL>while p is not None:<EOL><INDENT>ancestors.append(p)<EOL>p = p.parent<EOL><DEDENT>return ancestors<EOL>
Get a list of ancestors. @return: A list of ancestors. @rtype: [L{Element},..]
f9710:c0:m45
def walk(self, visitor):
visitor(self)<EOL>for c in self.children:<EOL><INDENT>c.walk(visitor)<EOL><DEDENT>return self<EOL>
Walk the branch and call the visitor function on each node. @param visitor: A function. @return: self @rtype: L{Element}
f9710:c0:m46
def prune(self):
pruned = []<EOL>for c in self.children:<EOL><INDENT>c.prune()<EOL>if c.isempty(False):<EOL><INDENT>pruned.append(c)<EOL><DEDENT><DEDENT>for p in pruned:<EOL><INDENT>self.children.remove(p)<EOL><DEDENT>
Prune the branch of empty nodes.
f9710:c0:m47
def __init__(self, parent):
self.pos = <NUM_LIT:0><EOL>self.children = parent.children<EOL>
@param parent: An element to iterate. @type parent: L{Element}
f9710:c1:m0
def next(self):
try:<EOL><INDENT>child = self.children[self.pos]<EOL>self.pos += <NUM_LIT:1><EOL>return child<EOL><DEDENT>except:<EOL><INDENT>raise StopIteration()<EOL><DEDENT>
Get the next child. @return: The next child. @rtype: L{Element} @raise StopIterator: At the end.
f9710:c1:m2
@classmethod<EOL><INDENT>def apply(cls, node):<DEDENT>
pn = PrefixNormalizer(node)<EOL>return pn.refit()<EOL>
Normalize the specified node. @param node: A node to normalize. @type node: L{Element} @return: The normalized node. @rtype: L{Element}
f9710:c2:m0
def __init__(self, node):
self.node = node<EOL>self.branch = node.branch()<EOL>self.namespaces = self.getNamespaces()<EOL>self.prefixes = self.genPrefixes()<EOL>
@param node: A node to normalize. @type node: L{Element}
f9710:c2:m1
def getNamespaces(self):
s = set()<EOL>for n in self.branch + self.node.ancestors():<EOL><INDENT>if self.permit(n.expns):<EOL><INDENT>s.add(n.expns)<EOL><DEDENT>s = s.union(self.pset(n))<EOL><DEDENT>return s<EOL>
Get the I{unique} set of namespaces referenced in the branch. @return: A set of namespaces. @rtype: set
f9710:c2:m2
def pset(self, n):
s = set()<EOL>for ns in n.nsprefixes.items():<EOL><INDENT>if self.permit(ns):<EOL><INDENT>s.add(ns[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>return s<EOL>
Convert the nodes nsprefixes into a set. @param n: A node. @type n: L{Element} @return: A set of namespaces. @rtype: set
f9710:c2:m3
def genPrefixes(self):
prefixes = {}<EOL>n = <NUM_LIT:0><EOL>for u in self.namespaces:<EOL><INDENT>p = '<STR_LIT>' % n<EOL>prefixes[u] = p<EOL>n += <NUM_LIT:1><EOL><DEDENT>return prefixes<EOL>
Generate a I{reverse} mapping of unique prefixes for all namespaces. @return: A referse dict of prefixes. @rtype: {u, p}
f9710:c2:m4
def refit(self):
self.refitNodes()<EOL>self.refitMappings()<EOL>
Refit (normalize) the prefixes in the node.
f9710:c2:m5
def refitNodes(self):
for n in self.branch:<EOL><INDENT>if n.prefix is not None:<EOL><INDENT>ns = n.namespace()<EOL>if self.permit(ns):<EOL><INDENT>n.prefix = self.prefixes[ns[<NUM_LIT:1>]]<EOL><DEDENT><DEDENT>self.refitAttrs(n)<EOL><DEDENT>
Refit (normalize) all of the nodes in the branch.
f9710:c2:m6
def refitAttrs(self, n):
for a in n.attributes:<EOL><INDENT>self.refitAddr(a)<EOL><DEDENT>
Refit (normalize) all of the attributes in the node. @param n: A node. @type n: L{Element}
f9710:c2:m7
def refitAddr(self, a):
if a.prefix is not None:<EOL><INDENT>ns = a.namespace()<EOL>if self.permit(ns):<EOL><INDENT>a.prefix = self.prefixes[ns[<NUM_LIT:1>]]<EOL><DEDENT><DEDENT>self.refitValue(a)<EOL>
Refit (normalize) the attribute. @param a: An attribute. @type a: L{Attribute}
f9710:c2:m8
def refitValue(self, a):
p, name = splitPrefix(a.getValue())<EOL>if p is None:<EOL><INDENT>return<EOL><DEDENT>ns = a.resolvePrefix(p)<EOL>if self.permit(ns):<EOL><INDENT>u = ns[<NUM_LIT:1>]<EOL>p = self.prefixes[u]<EOL>a.setValue('<STR_LIT::>'.join((p, name)))<EOL><DEDENT>
Refit (normalize) the attribute's value. @param a: An attribute. @type a: L{Attribute}
f9710:c2:m9
def refitMappings(self):
for n in self.branch:<EOL><INDENT>n.nsprefixes = {}<EOL><DEDENT>n = self.node<EOL>for u, p in self.prefixes.items():<EOL><INDENT>n.addPrefix(p, u)<EOL><DEDENT>
Refit (normalize) all of the nsprefix mappings.
f9710:c2:m10
def permit(self, ns):
return not self.skip(ns)<EOL>
Get whether the I{ns} is to be normalized. @param ns: A namespace. @type ns: (p,u) @return: True if to be included. @rtype: boolean
f9710:c2:m11
def skip(self, ns):
return ns in (None, Namespace.default, Namespace.xsdns, Namespace.xsins, Namespace.xmlns)<EOL>
Get whether the I{ns} is to B{not} be normalized. @param ns: A namespace. @type ns: (p,u) @return: True if to be skipped. @rtype: boolean
f9710:c2:m12
def smart_str(s, encoding='<STR_LIT:utf-8>', errors='<STR_LIT:strict>'):
if not isinstance(s, basestring):<EOL><INDENT>try:<EOL><INDENT>return str(s)<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>if isinstance(s, Exception):<EOL><INDENT>return '<STR_LIT:U+0020>'.join(smart_str(arg, encoding, errors) for arg in s)<EOL><DEDENT>return unicode(s).encode(encoding, errors)<EOL><DEDENT><DEDENT>elif isinstance(s, unicode):<EOL><INDENT>return s.encode(encoding, errors)<EOL><DEDENT>elif s and encoding != '<STR_LIT:utf-8>':<EOL><INDENT>return s.decode('<STR_LIT:utf-8>', errors).encode(encoding, errors)<EOL><DEDENT>else:<EOL><INDENT>return s<EOL><DEDENT>
Returns a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects. from django
f9711:m0
def tostr(object, encoding=None):
if isinstance(object, basestring):<EOL><INDENT>if encoding is None:<EOL><INDENT>return object<EOL><DEDENT>else:<EOL><INDENT>return object.encode(encoding)<EOL><DEDENT><DEDENT>if isinstance(object, tuple):<EOL><INDENT>s = ['<STR_LIT:(>']<EOL>for item in object:<EOL><INDENT>if isinstance(item, basestring):<EOL><INDENT>s.append(item)<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:)>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>if isinstance(object, list):<EOL><INDENT>s = ['<STR_LIT:[>']<EOL>for item in object:<EOL><INDENT>if isinstance(item, basestring):<EOL><INDENT>s.append(item)<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:]>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>if isinstance(object, dict):<EOL><INDENT>s = ['<STR_LIT:{>']<EOL>for item in object.items():<EOL><INDENT>if isinstance(item[<NUM_LIT:0>], basestring):<EOL><INDENT>s.append(item[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item[<NUM_LIT:0>]))<EOL><DEDENT>s.append('<STR_LIT>')<EOL>if isinstance(item[<NUM_LIT:1>], basestring):<EOL><INDENT>s.append(item[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>s.append(tostr(item[<NUM_LIT:1>]))<EOL><DEDENT>s.append('<STR_LIT:U+002CU+0020>')<EOL><DEDENT>s.append('<STR_LIT:}>')<EOL>return '<STR_LIT>'.join(s)<EOL><DEDENT>try:<EOL><INDENT>return unicode(object)<EOL><DEDENT>except:<EOL><INDENT>return str(object)<EOL><DEDENT>
get a unicode safe string representation of an object
f9711:m1
def setaty(self, content):
name = '<STR_LIT>'<EOL>ns = (None, '<STR_LIT>')<EOL>aty = content.node.get(name, ns)<EOL>if aty is not None:<EOL><INDENT>content.aty = aty<EOL>parts = aty.split('<STR_LIT:[>')<EOL>ref = parts[<NUM_LIT:0>]<EOL>if len(parts) == <NUM_LIT:2>:<EOL><INDENT>self.applyaty(content, ref)<EOL><DEDENT>else:<EOL><INDENT>pass <EOL><DEDENT><DEDENT>return self<EOL>
Grab the (aty) soap-enc:arrayType and attach it to the content for proper array processing later in end(). @param content: The current content being unmarshalled. @type content: L{Content} @return: self @rtype: L{Encoded}
f9712:c0:m3
def applyaty(self, content, xty):
name = '<STR_LIT:type>'<EOL>ns = Namespace.xsins<EOL>parent = content.node<EOL>for child in parent.getChildren():<EOL><INDENT>ref = child.get(name, ns)<EOL>if ref is None:<EOL><INDENT>parent.addPrefix(ns[<NUM_LIT:0>], ns[<NUM_LIT:1>])<EOL>attr = '<STR_LIT::>'.join((ns[<NUM_LIT:0>], name))<EOL>child.set(attr, xty)<EOL><DEDENT><DEDENT>return self<EOL>
Apply the type referenced in the I{arrayType} to the content (child nodes) of the array. Each element (node) in the array that does not have an explicit xsi:type attribute is given one based on the I{arrayType}. @param content: An array content. @type content: L{Content} @param xty: The XSI type reference. @type xty: str @return: self @rtype: L{Encoded}
f9712:c0:m4
def promote(self, content):
for n, v in content.data:<EOL><INDENT>if isinstance(v, list):<EOL><INDENT>content.data = v<EOL>return<EOL><DEDENT><DEDENT>content.data = []<EOL>
Promote (replace) the content.data with the first attribute of the current content.data that is a I{list}. Note: the content.data may be empty or contain only _x attributes. In either case, the content.data is assigned an empty list. @param content: An array content. @type content: L{Content}
f9712:c0:m5
def process(self, node):
content = Content(node)<EOL>return Core.process(self, content)<EOL>
Process an object graph representation of the xml I{node}. @param node: An XML tree. @type node: L{sax.element.Element} @return: A suds object. @rtype: L{Object}
f9713:c0:m0
def __init__(self, attributes):
self.raw = attributes<EOL>
@param attributes: A list of attributes @type attributes: list
f9714:c0:m0
def real(self):
for a in self.raw:<EOL><INDENT>if self.skip(a):<EOL><INDENT>continue<EOL><DEDENT>yield a<EOL><DEDENT>
Get list of I{real} attributes which exclude xs and xml attributes. @return: A list of I{real} attributes. @rtype: I{generator}
f9714:c0:m1