def qualify(ref, resolvers, defns=Namespace.default): """ Get a reference that is I{qualified} by namespace. @param ref: A referenced schema type name. @type ref: str @param resolvers: A list of objects to be used to resolve types. @type resolvers: [L{sax.element.Element},] @param defns: An optional target namespace used to qualify references when no prefix is specified. @type defns: A default namespace I{tuple: (prefix,uri)} used when ref not prefixed. @return: A qualified reference. @rtype: (name, namespace-uri) """ ns = None p, n = splitPrefix(ref) if p is not None: if not isinstance(resolvers, (list, tuple)): resolvers = (resolvers,) for r in resolvers: resolved = r.resolvePrefix(p) if resolved[1] is not None: ns = resolved break if ns is None: raise Exception('prefix (%s) not resolved' % p) else: ns = defns return (n, ns[1])
def rename(self, name): """ Rename the element. @param name: A new name for the element. @type name: basestring """ if name is None: raise Exception('name (%s) not-valid' % name) else: self.prefix, self.name = splitPrefix(name)
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 @type value: basestring """ self.parent = None self.prefix, self.name = splitPrefix(name) self.setValue(value)
def __childrenAtPath(self, parts): result = [] node = self last = len(parts) - 1 ancestors = parts[:last] leaf = parts[last] for name in ancestors: ns = None prefix, name = splitPrefix(name) if prefix is not None: ns = node.resolvePrefix(prefix) child = node.getChild(name, ns) if child is None: break else: node = child if child is not None: ns = None prefix, leaf = splitPrefix(leaf) if prefix is not None: ns = node.resolvePrefix(prefix) result = child.getChildren(leaf) return result
def refitValue(self, a): """ Refit (normalize) the attribute's value. @param a: An attribute. @type a: L{Attribute} """ 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)))
def __childrenAtPath(self, parts): result = [] node = self last = len(parts)-1 ancestors = parts[:last] leaf = parts[last] for name in ancestors: ns = None prefix, name = splitPrefix(name) if prefix is not None: ns = node.resolvePrefix(prefix) child = node.getChild(name, ns) if child is None: break else: node = child if child is not None: ns = None prefix, leaf = splitPrefix(leaf) if prefix is not None: ns = node.resolvePrefix(prefix) result = child.getChildren(leaf) return result
def leaf(self, parent, parts): """ Find the leaf. @param parts: A list of path parts. @type parts: [str,..] @param parent: The leaf's parent. @type parent: L{xsd.sxbase.SchemaObject} @return: The leaf. @rtype: L{xsd.sxbase.SchemaObject} """ name = splitPrefix(parts[-1])[1] if name.startswith('@'): result, path = parent.get_attribute(name[1:]) else: result, ancestry = parent.get_child(name) if result is None: raise PathResolver.BadPath(name) return result
def getChildren(self, name=None, ns=None): """ 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},...] """ 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)]
def get_reply(self, method, reply): """ Process the I{reply} for the specified I{method} by sax parsing the I{reply} and then unmarshalling into python object(s). @param method: The name of the invoked method. @type method: str @param reply: The reply XML received after invoking the specified method. @type reply: str @return: The unmarshalled reply. The returned value is an L{Object} for a I{list} depending on whether the service returns a single object or a collection. @rtype: tuple ( L{Element}, L{Object} ) """ reply = self.replyfilter(reply) sax = Parser() replyroot = sax.parse(string=reply) soapenv = replyroot.getChild('Envelope') soapenv.promotePrefixes() soapbody = soapenv.getChild('Body') soapbody = self.multiref.process(soapbody) nodes = self.replycontent(method, soapbody) rtypes = self.returned_types(method) if len(rtypes) > 1: result = self.replycomposite(rtypes, nodes) return (replyroot, result) if len(rtypes) == 1: if rtypes[0].unbounded(): result = self.replylist(rtypes[0], nodes) return (replyroot, result) if len(nodes): unmarshaller = self.unmarshaller() resolved = rtypes[0].resolve(nobuiltin=True) ns = nodes[0].resolvePrefix(splitPrefix(nodes[0].get("type"))[0])[1]; result = unmarshaller.process(nodes[0], resolved) # result.__ns = ns return (replyroot, result) return (replyroot, None)
def builtin(self, ref, context=None): """ Get whether the specified reference is an (xs) builtin. @param ref: A str or qref. @type ref: (str|qref) @return: True if builtin, else False. @rtype: bool """ w3 = 'http://www.w3.org' try: if isqref(ref): ns = ref[1] return ( ref[0] in Factory.tags and ns.startswith(w3) ) if context is None: context = self.root prefix = splitPrefix(ref)[0] prefixes = context.findPrefixes(w3, 'startswith') return ( prefix in prefixes and ref[0] in Factory.tags ) except: return False
def builtin(self, ref, context=None): """ Get whether the specified reference is an (xs) builtin. @param ref: A str or qref. @type ref: (str|qref) @return: True if builtin, else False. @rtype: bool """ w3 = 'http://www.w3.org' try: if isqref(ref): ns = ref[1] return (ref[0] in Factory.tags and ns.startswith(w3)) if context is None: context = self.root prefix = splitPrefix(ref)[0] prefixes = context.findPrefixes(w3, 'startswith') return (prefix in prefixes and ref[0] in Factory.tags) except: return False
def branch(self, root, parts): """ Traverse the path until a leaf is reached. @param parts: A list of path parts. @type parts: [str,..] @param root: The root. @type root: L{xsd.sxbase.SchemaObject} @return: The end of the branch. @rtype: L{xsd.sxbase.SchemaObject} """ result = root for part in parts[1:-1]: name = splitPrefix(part)[1] log.debug('searching parent (%s) for (%s)', Repr(result), name) result, ancestry = result.get_child(name) if result is None: log.error('(%s) not-found', name) raise PathResolver.BadPath(name) result = result.resolve(nobuiltin=True) log.debug('found (%s) as (%s)', name, Repr(result)) return result
def getChild(self, name, ns=None, default=None): """ 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} """ 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
def getAttribute(self, name, ns=None, default=None): """ 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} """ 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
def childAtPath(self, path): """ 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} """ 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: break else: node = result return result