desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Get the I{day} component. @return: The day. @rtype: int'
def day(self):
return self.date.day
'Parse the string date. Supported formats: - YYYY-MM-DD - YYYY-MM-DD(z|Z) - YYYY-MM-DD+06:00 - YYYY-MM-DD-06:00 Although, the TZ is ignored because it\'s meaningless without the time, right? @param s: A date string. @type s: str @return: A date object. @rtype: I{date}'
def __parse(self, s):
try: (year, month, day) = s[:10].split('-', 2) year = int(year) month = int(month) day = int(day) return dt.date(year, month, day) except: log.debug(s, exec_info=True) raise ValueError, ('Invalid format "%s"' % s)
'@param time: The value of the object. @type time: (time|str) @param adjusted: Adjust for I{local} Timezone. @type adjusted: boolean @raise ValueError: When I{time} is invalid.'
def __init__(self, time, adjusted=True):
self.tz = Timezone() if isinstance(time, dt.time): self.time = time return if isinstance(time, basestring): self.time = self.__parse(time) if adjusted: self.__adjust() return raise ValueError, type(time)
'Get the I{hour} component. @return: The hour. @rtype: int'
def hour(self):
return self.time.hour
'Get the I{minute} component. @return: The minute. @rtype: int'
def minute(self):
return self.time.minute
'Get the I{seconds} component. @return: The seconds. @rtype: int'
def second(self):
return self.time.second
'Get the I{microsecond} component. @return: The microsecond. @rtype: int'
def microsecond(self):
return self.time.microsecond
'Adjust for TZ offset.'
def __adjust(self):
if hasattr(self, 'offset'): today = dt.date.today() delta = self.tz.adjustment(self.offset) d = dt.datetime.combine(today, self.time) d = (d + delta) self.time = d.time()
'Parse the string date. Patterns: - HH:MI:SS - HH:MI:SS(z|Z) - HH:MI:SS.ms - HH:MI:SS.ms(z|Z) - HH:MI:SS(+|-)06:00 - HH:MI:SS.ms(+|-)06:00 @param s: A time string. @type s: str @return: A time object. @rtype: B{datetime}.I{time}'
def __parse(self, s):
try: offset = None part = Timezone.split(s) (hour, minute, second) = part[0].split(':', 2) hour = int(hour) minute = int(minute) (second, ms) = self.__second(second) if (len(part) == 2): self.offset = self.__offset(part[1]) if (ms is None):...
'Parse the seconds and microseconds. The microseconds are truncated to 999999 due to a restriction in the python datetime.datetime object. @param s: A string representation of the seconds. @type s: str @return: Tuple of (sec,ms) @rtype: tuple.'
def __second(self, s):
part = s.split('.') if (len(part) > 1): return (int(part[0]), int(part[1][:6])) else: return (int(part[0]), None)
'Parse the TZ offset. @param s: A string representation of the TZ offset. @type s: str @return: The signed offset in hours. @rtype: str'
def __offset(self, s):
if (len(s) == len('-00:00')): return int(s[:3]) if (len(s) == 0): return self.tz.local if (len(s) == 1): return 0 raise Exception()
'@param date: The value of the object. @type date: (datetime|str) @raise ValueError: When I{tm} is invalid.'
def __init__(self, date):
if isinstance(date, dt.datetime): Date.__init__(self, date.date()) Time.__init__(self, date.time()) self.datetime = dt.datetime.combine(self.date, self.time) return if isinstance(date, basestring): part = date.split('T') Date.__init__(self, part[0]) Time._...
'Adjust for TZ offset.'
def __adjust(self):
if (not hasattr(self, 'offset')): return delta = self.tz.adjustment(self.offset) try: d = (self.datetime + delta) self.datetime = d self.date = d.date() self.time = d.time() except OverflowError: log.warn('"%s" caused overflow, not-adjusted', self...
'Split the TZ from string. @param s: A string containing a timezone @type s: basestring @return: The split parts. @rtype: tuple'
@classmethod def split(cls, s):
m = cls.pattern.search(s) if (m is None): return (s,) x = m.start(0) return (s[:x], s[x:])
'Get the adjustment to the I{local} TZ. @return: The delta between I{offset} and local TZ. @rtype: B{datetime}.I{timedelta}'
def adjustment(self, offset):
delta = (self.local - offset) return dt.timedelta(hours=delta)
'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}'
@classmethod def buildPath(self, parent, path):
for tag in path.split('/'): child = parent.getChild(tag) if (child is None): child = Element(tag, parent) parent = child return child
'@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})'
def __init__(self, name, parent=None, ns=None):
self.rename(name) self.expns = None self.nsprefixes = {} self.attributes = [] self.text = None if (parent is not None): if isinstance(parent, Element): self.parent = parent else: raise Exception('parent (%s) not-valid', parent.__class__.__name__) ...
'Rename the element. @param name: A new name for the element. @type name: basestring'
def rename(self, name):
if (name is None): raise Exception(('name (%s) not-valid' % name)) else: (self.prefix, self.name) = splitPrefix(name)
'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}'
def setPrefix(self, p, u=None):
self.prefix = p if ((p is not None) and (u is not None)): self.addPrefix(p, u) return self
'Get the B{fully} qualified name of this element @return: The fully qualified name. @rtype: basestring'
def qname(self):
if (self.prefix is None): return self.name else: return ('%s:%s' % (self.prefix, self.name))
'Get the root (top) node of the tree. @return: The I{top} node of this tree. @rtype: I{Element}'
def getRoot(self):
if (self.parent is None): return self else: return self.parent.getRoot()
'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}'
def clone(self, parent=None):
root = Element(self.qname(), parent, self.namespace()) for a in self.attributes: root.append(a.clone(self)) for c in self.children: root.append(c.clone(self)) for item in self.nsprefixes.items(): root.addPrefix(item[0], item[1]) return root
'Detach from parent. @return: This element removed from its parent\'s child list and I{parent}=I{None} @rtype: L{Element}'
def detach(self):
if (self.parent is not None): if (self in self.parent.children): self.parent.children.remove(self) self.parent = None return self
'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__()'
def set(self, name, value):
attr = self.getAttribute(name) if (attr is None): attr = Attribute(name, value) self.append(attr) else: attr.setValue(value)
'Unset (remove) an attribute. @param name: The attribute name. @type name: str @return: self @rtype: L{Element}'
def unset(self, name):
try: attr = self.getAttribute(name) self.attributes.remove(attr) except: pass return self
'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 @ret...
def get(self, name, ns=None, default=None):
attr = self.getAttribute(name, ns) if ((attr is None) or (attr.value is None)): return default else: return attr.getValue()
'Set the element\'s L{Text} content. @param value: The element\'s text value. @type value: basestring @return: self @rtype: I{Element}'
def setText(self, value):
if isinstance(value, Text): self.text = value else: self.text = Text(value) return self
'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}'
def getText(self, default=None):
if self.hasText(): return self.text else: return default
'Trim leading and trailing whitespace. @return: self @rtype: L{Element}'
def trim(self):
if self.hasText(): self.text = self.text.trim() return self
'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'
def hasText(self):
return ((self.text is not None) and len(self.text))
'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})'
def namespace(self):
if (self.prefix is None): return self.defaultNamespace() else: return self.resolvePrefix(self.prefix)
'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})'
def defaultNamespace(self):
p = self while (p is not None): if (p.expns is not None): return (None, p.expns) else: p = p.parent return Namespace.default
'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}'
def append(self, objects):
if (not isinstance(objects, (list, tuple))): objects = (objects,) for child in objects: if isinstance(child, Element): self.children.append(child) child.parent = self continue if isinstance(child, Attribute): self.attributes.append(child) ...
'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}'
def insert(self, objects, index=0):
objects = (objects,) for child in objects: if isinstance(child, Element): self.children.insert(index, child) child.parent = self else: raise Exception(('append %s not-valid' % child.__class__.__name__)) return self
'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'
def remove(self, child):
if isinstance(child, Element): return child.detach() if isinstance(child, Attribute): self.attributes.remove(child) return None
'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},]'
def replaceChild(self, child, content):
if (child not in self.children): raise Exception('child not-found') index = self.children.index(child) self.remove(child) if (not isinstance(content, (list, tuple))): content = (content,) for node in content: self.children.insert(index, node.detach()) node.parent =...
'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 o...
def getAttribute(self, name, ns=None, default=None):
if (ns is None): (prefix, name) = splitPrefix(name) if (prefix is None): ns = None else: ns = self.resolvePrefix(prefix) for a in self.attributes: if a.match(name, ns): return a return default
'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: Th...
def getChild(self, name, ns=None, default=None):
if (ns is None): (prefix, name) = splitPrefix(name) if (prefix is None): ns = None else: ns = self.resolvePrefix(prefix) for c in self.children: if c.match(name, ns): return c return default
'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}'
def childAtPath(self, path):
result = None node = self for name in [p for p in path.split('/') if (len(p) > 0)]: ns = None (prefix, name) = splitPrefix(name) if (prefix is not None): ns = node.resolvePrefix(prefix) result = node.getChild(name, ns) if (result is None): brea...
'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},...]'
def childrenAtPath(self, path):
parts = [p for p in path.split('/') if (len(p) > 0)] if (len(parts) == 1): result = self.getChildren(path) else: result = self.__childrenAtPath(parts) return result
'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},...]'
def getChildren(self, name=None, ns=None):
if (ns is None): if (name is None): return self.children (prefix, name) = splitPrefix(name) if (prefix is None): ns = None else: ns = self.resolvePrefix(prefix) return [c for c in self.children if c.match(name, ns)]
'Detach and return this element\'s children. @return: The element\'s children (detached). @rtype: [L{Element},...]'
def detachChildren(self):
detached = self.children self.children = [] for child in detached: child.parent = None return detached
'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 opt...
def resolvePrefix(self, prefix, default=Namespace.default):
n = self while (n is not None): if (prefix in n.nsprefixes): return (prefix, n.nsprefixes[prefix]) if (prefix in self.specialprefixes): return (prefix, self.specialprefixes[prefix]) n = n.parent return default
'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}'
def addPrefix(self, p, u):
self.nsprefixes[p] = u return self
'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!'
def updatePrefix(self, p, u):
if (p in self.nsprefixes): self.nsprefixes[p] = u for c in self.children: c.updatePrefix(p, u) return self
'Clear the specified prefix from the prefix mappings. @param prefix: A prefix to clear. @type prefix: basestring @return: self @rtype: L{Element}'
def clearPrefix(self, prefix):
if (prefix in self.nsprefixes): del self.nsprefixes[prefix] return self
'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. @rty...
def findPrefix(self, uri, default=None):
for item in self.nsprefixes.items(): if (item[1] == uri): prefix = item[0] return prefix for item in self.specialprefixes.items(): if (item[1] == uri): prefix = item[0] return prefix if (self.parent is not None): return self.parent.find...
'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...
def findPrefixes(self, uri, match='eq'):
result = [] for item in self.nsprefixes.items(): if self.matcher[match](item[1], uri): prefix = item[0] result.append(prefix) for item in self.specialprefixes.items(): if self.matcher[match](item[1], uri): prefix = item[0] result.append(prefix)...
'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}'
def promotePrefixes(self):
for c in self.children: c.promotePrefixes() if (self.parent is None): return for (p, u) in self.nsprefixes.items(): if (p in self.parent.nsprefixes): pu = self.parent.nsprefixes[p] if (pu == u): del self.nsprefixes[p] continue ...
'Refit namespace qualification by replacing prefixes with explicit namespaces. Also purges prefix mapping table. @return: self @rtype: L{Element}'
def refitPrefixes(self):
for c in self.children: c.refitPrefixes() if (self.prefix is not None): ns = self.resolvePrefix(self.prefix) if (ns[1] is not None): self.expns = ns[1] self.prefix = None self.nsprefixes = {} return self
'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}'
def normalizePrefixes(self):
PrefixNormalizer.apply(self) return self
'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'
def isempty(self, content=True):
noattrs = (not len(self.attributes)) nochildren = (not len(self.children)) notext = (self.text is None) nocontent = (nochildren and notext) if content: return nocontent else: return (nocontent and noattrs)
'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'
def isnil(self):
nilattr = self.getAttribute('nil', ns=Namespace.xsins) if (nilattr is None): return False else: return (nilattr.getValue().lower() == 'true')
'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}'
def setnil(self, flag=True):
(p, u) = Namespace.xsins name = ':'.join((p, 'nil')) self.set(name, str(flag).lower()) self.addPrefix(p, u) if flag: self.text = None return self
'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})'
def applyns(self, ns):
if (ns is None): return if (not isinstance(ns, (tuple, list))): raise Exception('namespace must be tuple') if (ns[0] is None): self.expns = ns[1] else: self.prefix = ns[0] self.nsprefixes[ns[0]] = ns[1]
'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'
def str(self, indent=0):
tab = ('%*s' % ((indent * 3), '')) result = [] result.append(('%s<%s' % (tab, self.qname()))) result.append(self.nsdeclarations()) for a in [unicode(a) for a in self.attributes]: result.append((' %s' % a)) if self.isempty(): result.append('/>') return ''.join(result) ...
'Get a string representation of this XML fragment. @return: A I{plain} string. @rtype: basestring'
def plain(self):
result = [] result.append(('<%s' % self.qname())) result.append(self.nsdeclarations()) for a in [unicode(a) for a in self.attributes]: result.append((' %s' % a)) if self.isempty(): result.append('/>') return ''.join(result) result.append('>') if self.hasText(): ...
'Get a string representation for all namespace declarations as xmlns="" and xmlns:p="". @return: A separated list of declarations. @rtype: basestring'
def nsdeclarations(self):
s = [] myns = (None, self.expns) if (self.parent is None): pns = Namespace.default else: pns = (None, self.parent.expns) if (myns[1] != pns[1]): if (self.expns is not None): d = (' xmlns="%s"' % self.expns) s.append(d) for item in self.nsprefixe...
'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'
def match(self, name=None, ns=None):
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)
'Get a flattened representation of the branch. @return: A flat list of nodes. @rtype: [L{Element},..]'
def branch(self):
branch = [self] for c in self.children: branch += c.branch() return branch
'Get a list of ancestors. @return: A list of ancestors. @rtype: [L{Element},..]'
def ancestors(self):
ancestors = [] p = self.parent while (p is not None): ancestors.append(p) p = p.parent return ancestors
'Walk the branch and call the visitor function on each node. @param visitor: A function. @return: self @rtype: L{Element}'
def walk(self, visitor):
visitor(self) for c in self.children: c.walk(visitor) return self
'Prune the branch of empty nodes.'
def prune(self):
pruned = [] for c in self.children: c.prune() if c.isempty(False): pruned.append(c) for p in pruned: self.children.remove(p)
'@param parent: An element to iterate. @type parent: L{Element}'
def __init__(self, parent):
self.pos = 0 self.children = parent.children
'Get the next child. @return: The next child. @rtype: L{Element} @raise StopIterator: At the end.'
def next(self):
try: child = self.children[self.pos] self.pos += 1 return child except: raise StopIteration()
'Normalize the specified node. @param node: A node to normalize. @type node: L{Element} @return: The normalized node. @rtype: L{Element}'
@classmethod def apply(cls, node):
pn = PrefixNormalizer(node) return pn.refit()
'@param node: A node to normalize. @type node: L{Element}'
def __init__(self, node):
self.node = node self.branch = node.branch() self.namespaces = self.getNamespaces() self.prefixes = self.genPrefixes()
'Get the I{unique} set of namespaces referenced in the branch. @return: A set of namespaces. @rtype: set'
def getNamespaces(self):
s = set() for n in (self.branch + self.node.ancestors()): if self.permit(n.expns): s.add(n.expns) s = s.union(self.pset(n)) return s
'Convert the nodes nsprefixes into a set. @param n: A node. @type n: L{Element} @return: A set of namespaces. @rtype: set'
def pset(self, n):
s = set() for ns in n.nsprefixes.items(): if self.permit(ns): s.add(ns[1]) return s
'Generate a I{reverse} mapping of unique prefixes for all namespaces. @return: A referse dict of prefixes. @rtype: {u, p}'
def genPrefixes(self):
prefixes = {} n = 0 for u in self.namespaces: p = ('ns%d' % n) prefixes[u] = p n += 1 return prefixes
'Refit (normalize) the prefixes in the node.'
def refit(self):
self.refitNodes() self.refitMappings()
'Refit (normalize) all of the nodes in the branch.'
def refitNodes(self):
for n in self.branch: if (n.prefix is not None): ns = n.namespace() if self.permit(ns): n.prefix = self.prefixes[ns[1]] self.refitAttrs(n)
'Refit (normalize) all of the attributes in the node. @param n: A node. @type n: L{Element}'
def refitAttrs(self, n):
for a in n.attributes: self.refitAddr(a)
'Refit (normalize) the attribute. @param a: An attribute. @type a: L{Attribute}'
def refitAddr(self, a):
if (a.prefix is not None): ns = a.namespace() if self.permit(ns): a.prefix = self.prefixes[ns[1]] self.refitValue(a)
'Refit (normalize) the attribute\'s value. @param a: An attribute. @type a: L{Attribute}'
def refitValue(self, a):
(p, name) = splitPrefix(a.getValue()) if (p is None): return ns = a.resolvePrefix(p) if self.permit(ns): u = ns[1] p = self.prefixes[u] a.setValue(':'.join((p, name)))
'Refit (normalize) all of the nsprefix mappings.'
def refitMappings(self):
for n in self.branch: n.nsprefixes = {} n = self.node for (u, p) in self.prefixes.items(): n.addPrefix(p, u)
'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'
def permit(self, ns):
return (not self.skip(ns))
'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'
def skip(self, ns):
return ((ns is None) or (ns == Namespace.default) or (ns == Namespace.xsdns) or (ns == Namespace.xsins) or (ns == Namespace.xmlns))
'@param name: The attribute\'s name with I{optional} namespace prefix. @type name: basestring @param value: The attribute\'s value @type value: basestring'
def __init__(self, name, value=None):
self.parent = None (self.prefix, self.name) = splitPrefix(name) self.setValue(value)
'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}'
def clone(self, parent=None):
a = Attribute(self.qname(), self.value) a.parent = parent return a
'Get the B{fully} qualified name of this attribute @return: The fully qualified name. @rtype: basestring'
def qname(self):
if (self.prefix is None): return self.name else: return ':'.join((self.prefix, self.name))
'Set the attributes value @param value: The new value (may be None) @type value: basestring @return: self @rtype: L{Attribute}'
def setValue(self, value):
if isinstance(value, Text): self.value = value else: self.value = Text(value) return self
'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}'
def getValue(self, default=Text('')):
if self.hasText(): return self.value else: return default
'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'
def hasText(self):
return ((self.value is not None) and len(self.value))
'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})'
def namespace(self):
if (self.prefix is None): return Namespace.default else: return self.resolvePrefix(self.prefix)
'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})'
def resolvePrefix(self, prefix):
ns = Namespace.default if (self.parent is not None): ns = self.parent.resolvePrefix(prefix) return ns
'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'
def match(self, name=None, ns=None):
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)
'equals operator'
def __eq__(self, rhs):
return ((rhs is not None) and isinstance(rhs, Attribute) and (self.prefix == rhs.name) and (self.name == rhs.name))
'get a string representation'
def __repr__(self):
return ('attr (prefix=%s, name=%s, value=(%s))' % (self.prefix, self.name, self.value))
'get an xml string representation'
def __str__(self):
return unicode(self).encode('utf-8')
'get an xml string representation'
def __unicode__(self):
n = self.qname() if self.hasText(): v = self.value.escape() else: v = self.value return (u'%s="%s"' % (n, v))
'@param aty: Array type information. @type aty: The value of wsdl:arrayType.'
def __init__(self, schema, root, aty):
SXAttribute.__init__(self, schema, root) if aty.endswith('[]'): self.aty = aty[:(-2)] else: self.aty = aty
'@param root: An XML root element. @type root: L{Element} @param definitions: A definitions object. @type definitions: L{Definitions}'
def __init__(self, root, definitions=None):
Object.__init__(self) self.root = root pmd = Metadata() pmd.excludes = ['root'] pmd.wrappers = dict(qname=repr) self.__metadata__.__print__ = pmd
'Resolve named references to other WSDL objects. @param definitions: A definitions object. @type definitions: L{Definitions}'
def resolve(self, definitions):
pass
'@param root: An XML root element. @type root: L{Element} @param definitions: A definitions object. @type definitions: L{Definitions}'
def __init__(self, root, definitions):
WObject.__init__(self, root, definitions) self.name = root.get('name') self.qname = (self.name, definitions.tns[1]) pmd = self.__metadata__.__print__ pmd.wrappers['qname'] = repr
'@param url: A URL to the WSDL. @type url: str @param options: An options dictionary. @type options: L{options.Options}'
def __init__(self, url, options):
log.debug('reading wsdl at: %s ...', url) reader = DocumentReader(options) d = reader.open(url) root = d.root() WObject.__init__(self, root) self.id = objid(self) self.options = options self.url = url self.tns = self.mktns(root) self.types = [] self.schema = None ...
'Get/create the target namespace'
def mktns(self, root):
tns = root.get('targetNamespace') prefix = root.findPrefix(tns) if (prefix is None): log.debug('warning: tns (%s), not mapped to prefix', tns) prefix = 'tns' return (prefix, tns)
'Add child objects using the factory'
def add_children(self, root):
for c in root.getChildren(ns=wsdlns): child = Factory.create(c, self) if (child is None): continue self.children.append(child) if isinstance(child, Import): self.imports.append(child) continue if isinstance(child, Types): self.t...
'Import the I{imported} WSDLs.'
def open_imports(self):
for imp in self.imports: imp.load(self)
'Tell all children to resolve themselves'
def resolve(self):
for c in self.children: c.resolve(self)