Blame python/libxml.py

Packit Service a31ea6
import libxml2mod
Packit Service a31ea6
import types
Packit Service a31ea6
import sys
Packit Service a31ea6
Packit Service a31ea6
# The root of all libxml2 errors.
Packit Service a31ea6
class libxmlError(Exception): pass
Packit Service a31ea6
Packit Service a31ea6
# Type of the wrapper class for the C objects wrappers
Packit Service a31ea6
def checkWrapper(obj):
Packit Service a31ea6
    try:
Packit Service a31ea6
        n = type(_obj).__name__
Packit Service a31ea6
        if n != 'PyCObject' and n != 'PyCapsule':
Packit Service a31ea6
            return 1
Packit Service a31ea6
    except:
Packit Service a31ea6
        return 0
Packit Service a31ea6
    return 0
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# id() is sometimes negative ...
Packit Service a31ea6
#
Packit Service a31ea6
def pos_id(o):
Packit Service a31ea6
    i = id(o)
Packit Service a31ea6
    if (i < 0):
Packit Service a31ea6
        return (sys.maxsize - i)
Packit Service a31ea6
    return i
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# Errors raised by the wrappers when some tree handling failed.
Packit Service a31ea6
#
Packit Service a31ea6
class treeError(libxmlError):
Packit Service a31ea6
    def __init__(self, msg):
Packit Service a31ea6
        self.msg = msg
Packit Service a31ea6
    def __str__(self):
Packit Service a31ea6
        return self.msg
Packit Service a31ea6
Packit Service a31ea6
class parserError(libxmlError):
Packit Service a31ea6
    def __init__(self, msg):
Packit Service a31ea6
        self.msg = msg
Packit Service a31ea6
    def __str__(self):
Packit Service a31ea6
        return self.msg
Packit Service a31ea6
Packit Service a31ea6
class uriError(libxmlError):
Packit Service a31ea6
    def __init__(self, msg):
Packit Service a31ea6
        self.msg = msg
Packit Service a31ea6
    def __str__(self):
Packit Service a31ea6
        return self.msg
Packit Service a31ea6
Packit Service a31ea6
class xpathError(libxmlError):
Packit Service a31ea6
    def __init__(self, msg):
Packit Service a31ea6
        self.msg = msg
Packit Service a31ea6
    def __str__(self):
Packit Service a31ea6
        return self.msg
Packit Service a31ea6
Packit Service a31ea6
class ioWrapper:
Packit Service a31ea6
    def __init__(self, _obj):
Packit Service a31ea6
        self.__io = _obj
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def io_close(self):
Packit Service a31ea6
        if self.__io == None:
Packit Service a31ea6
            return(-1)
Packit Service a31ea6
        self.__io.close()
Packit Service a31ea6
        self.__io = None
Packit Service a31ea6
        return(0)
Packit Service a31ea6
Packit Service a31ea6
    def io_flush(self):
Packit Service a31ea6
        if self.__io == None:
Packit Service a31ea6
            return(-1)
Packit Service a31ea6
        self.__io.flush()
Packit Service a31ea6
        return(0)
Packit Service a31ea6
Packit Service a31ea6
    def io_read(self, len = -1):
Packit Service a31ea6
        if self.__io == None:
Packit Service a31ea6
            return(-1)
Packit Service a31ea6
        try:
Packit Service a31ea6
            if len < 0:
Packit Service a31ea6
                ret = self.__io.read()
Packit Service a31ea6
            else:
Packit Service a31ea6
                ret = self.__io.read(len)
Packit Service a31ea6
        except Exception:
Packit Service a31ea6
            import sys
Packit Service a31ea6
            e = sys.exc_info()[1]
Packit Service a31ea6
            print("failed to read from Python:", type(e))
Packit Service a31ea6
            print("on IO:", self.__io)
Packit Service a31ea6
            self.__io == None
Packit Service a31ea6
            return(-1)
Packit Service a31ea6
Packit Service a31ea6
        return(ret)
Packit Service a31ea6
Packit Service a31ea6
    def io_write(self, str, len = -1):
Packit Service a31ea6
        if self.__io == None:
Packit Service a31ea6
            return(-1)
Packit Service a31ea6
        if len < 0:
Packit Service a31ea6
            return(self.__io.write(str))
Packit Service a31ea6
        return(self.__io.write(str, len))
Packit Service a31ea6
Packit Service a31ea6
class ioReadWrapper(ioWrapper):
Packit Service a31ea6
    def __init__(self, _obj, enc = ""):
Packit Service a31ea6
        ioWrapper.__init__(self, _obj)
Packit Service a31ea6
        self._o = libxml2mod.xmlCreateInputBuffer(self, enc)
Packit Service a31ea6
Packit Service a31ea6
    def __del__(self):
Packit Service a31ea6
        print("__del__")
Packit Service a31ea6
        self.io_close()
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlFreeParserInputBuffer(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def close(self):
Packit Service a31ea6
        self.io_close()
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlFreeParserInputBuffer(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
class ioWriteWrapper(ioWrapper):
Packit Service a31ea6
    def __init__(self, _obj, enc = ""):
Packit Service a31ea6
#        print "ioWriteWrapper.__init__", _obj
Packit Service a31ea6
        if type(_obj) == type(''):
Packit Service a31ea6
            print("write io from a string")
Packit Service a31ea6
            self.o = None
Packit Service a31ea6
        elif type(_obj).__name__ == 'PyCapsule':
Packit Service a31ea6
            file = libxml2mod.outputBufferGetPythonFile(_obj)
Packit Service a31ea6
            if file != None:
Packit Service a31ea6
                ioWrapper.__init__(self, file)
Packit Service a31ea6
            else:
Packit Service a31ea6
                ioWrapper.__init__(self, _obj)
Packit Service a31ea6
            self._o = _obj
Packit Service a31ea6
#        elif type(_obj) == types.InstanceType:
Packit Service a31ea6
#            print(("write io from instance of %s" % (_obj.__class__)))
Packit Service a31ea6
#            ioWrapper.__init__(self, _obj)
Packit Service a31ea6
#            self._o = libxml2mod.xmlCreateOutputBuffer(self, enc)
Packit Service a31ea6
        else:
Packit Service a31ea6
            file = libxml2mod.outputBufferGetPythonFile(_obj)
Packit Service a31ea6
            if file != None:
Packit Service a31ea6
                ioWrapper.__init__(self, file)
Packit Service a31ea6
            else:
Packit Service a31ea6
                ioWrapper.__init__(self, _obj)
Packit Service a31ea6
            self._o = _obj
Packit Service a31ea6
Packit Service a31ea6
    def __del__(self):
Packit Service a31ea6
#        print "__del__"
Packit Service a31ea6
        self.io_close()
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlOutputBufferClose(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def flush(self):
Packit Service a31ea6
        self.io_flush()
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlOutputBufferClose(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def close(self):
Packit Service a31ea6
        self.io_flush()
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlOutputBufferClose(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# Example of a class to handle SAX events
Packit Service a31ea6
#
Packit Service a31ea6
class SAXCallback:
Packit Service a31ea6
    """Base class for SAX handlers"""
Packit Service a31ea6
    def startDocument(self):
Packit Service a31ea6
        """called at the start of the document"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def endDocument(self):
Packit Service a31ea6
        """called at the end of the document"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def startElement(self, tag, attrs):
Packit Service a31ea6
        """called at the start of every element, tag is the name of
Packit Service a31ea6
           the element, attrs is a dictionary of the element's attributes"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def endElement(self, tag):
Packit Service a31ea6
        """called at the start of every element, tag is the name of
Packit Service a31ea6
           the element"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def characters(self, data):
Packit Service a31ea6
        """called when character data have been read, data is the string
Packit Service a31ea6
           containing the data, multiple consecutive characters() callback
Packit Service a31ea6
           are possible."""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def cdataBlock(self, data):
Packit Service a31ea6
        """called when CDATA section have been read, data is the string
Packit Service a31ea6
           containing the data, multiple consecutive cdataBlock() callback
Packit Service a31ea6
           are possible."""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def reference(self, name):
Packit Service a31ea6
        """called when an entity reference has been found"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def ignorableWhitespace(self, data):
Packit Service a31ea6
        """called when potentially ignorable white spaces have been found"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def processingInstruction(self, target, data):
Packit Service a31ea6
        """called when a PI has been found, target contains the PI name and
Packit Service a31ea6
           data is the associated data in the PI"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def comment(self, content):
Packit Service a31ea6
        """called when a comment has been found, content contains the comment"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def externalSubset(self, name, externalID, systemID):
Packit Service a31ea6
        """called when a DOCTYPE declaration has been found, name is the
Packit Service a31ea6
           DTD name and externalID, systemID are the DTD public and system
Packit Service a31ea6
           identifier for that DTd if available"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def internalSubset(self, name, externalID, systemID):
Packit Service a31ea6
        """called when a DOCTYPE declaration has been found, name is the
Packit Service a31ea6
           DTD name and externalID, systemID are the DTD public and system
Packit Service a31ea6
           identifier for that DTD if available"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def entityDecl(self, name, type, externalID, systemID, content):
Packit Service a31ea6
        """called when an ENTITY declaration has been found, name is the
Packit Service a31ea6
           entity name and externalID, systemID are the entity public and
Packit Service a31ea6
           system identifier for that entity if available, type indicates
Packit Service a31ea6
           the entity type, and content reports it's string content"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def notationDecl(self, name, externalID, systemID):
Packit Service a31ea6
        """called when an NOTATION declaration has been found, name is the
Packit Service a31ea6
           notation name and externalID, systemID are the notation public and
Packit Service a31ea6
           system identifier for that notation if available"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def attributeDecl(self, elem, name, type, defi, defaultValue, nameList):
Packit Service a31ea6
        """called when an ATTRIBUTE definition has been found"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def elementDecl(self, name, type, content):
Packit Service a31ea6
        """called when an ELEMENT definition has been found"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def entityDecl(self, name, publicId, systemID, notationName):
Packit Service a31ea6
        """called when an unparsed ENTITY declaration has been found,
Packit Service a31ea6
           name is the entity name and publicId,, systemID are the entity
Packit Service a31ea6
           public and system identifier for that entity if available,
Packit Service a31ea6
           and notationName indicate the associated NOTATION"""
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def warning(self, msg):
Packit Service a31ea6
        #print msg
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def error(self, msg):
Packit Service a31ea6
        raise parserError(msg)
Packit Service a31ea6
Packit Service a31ea6
    def fatalError(self, msg):
Packit Service a31ea6
        raise parserError(msg)
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# This class is the ancestor of all the Node classes. It provides
Packit Service a31ea6
# the basic functionalities shared by all nodes (and handle
Packit Service a31ea6
# gracefylly the exception), like name, navigation in the tree,
Packit Service a31ea6
# doc reference, content access and serializing to a string or URI
Packit Service a31ea6
#
Packit Service a31ea6
class xmlCore:
Packit Service a31ea6
    def __init__(self, _obj=None):
Packit Service a31ea6
        if _obj != None: 
Packit Service a31ea6
            self._o = _obj;
Packit Service a31ea6
            return
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def __eq__(self, other):
Packit Service a31ea6
        if other == None:
Packit Service a31ea6
            return False
Packit Service a31ea6
        ret = libxml2mod.compareNodesEqual(self._o, other._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return False
Packit Service a31ea6
        return ret == True
Packit Service a31ea6
    def __ne__(self, other):
Packit Service a31ea6
        if other == None:
Packit Service a31ea6
            return True
Packit Service a31ea6
        ret = libxml2mod.compareNodesEqual(self._o, other._o)
Packit Service a31ea6
        return not ret
Packit Service a31ea6
    def __hash__(self):
Packit Service a31ea6
        ret = libxml2mod.nodeHash(self._o)
Packit Service a31ea6
        return ret
Packit Service a31ea6
Packit Service a31ea6
    def __str__(self):
Packit Service a31ea6
        return self.serialize()
Packit Service a31ea6
    def get_parent(self):
Packit Service a31ea6
        ret = libxml2mod.parent(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return nodeWrap(ret)
Packit Service a31ea6
    def get_children(self):
Packit Service a31ea6
        ret = libxml2mod.children(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return nodeWrap(ret)
Packit Service a31ea6
    def get_last(self):
Packit Service a31ea6
        ret = libxml2mod.last(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return nodeWrap(ret)
Packit Service a31ea6
    def get_next(self):
Packit Service a31ea6
        ret = libxml2mod.next(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return nodeWrap(ret)
Packit Service a31ea6
    def get_properties(self):
Packit Service a31ea6
        ret = libxml2mod.properties(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return xmlAttr(_obj=ret)
Packit Service a31ea6
    def get_prev(self):
Packit Service a31ea6
        ret = libxml2mod.prev(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        return nodeWrap(ret)
Packit Service a31ea6
    def get_content(self):
Packit Service a31ea6
        return libxml2mod.xmlNodeGetContent(self._o)
Packit Service a31ea6
    getContent = get_content  # why is this duplicate naming needed ?
Packit Service a31ea6
    def get_name(self):
Packit Service a31ea6
        return libxml2mod.name(self._o)
Packit Service a31ea6
    def get_type(self):
Packit Service a31ea6
        return libxml2mod.type(self._o)
Packit Service a31ea6
    def get_doc(self):
Packit Service a31ea6
        ret = libxml2mod.doc(self._o)
Packit Service a31ea6
        if ret == None:
Packit Service a31ea6
            if self.type in ["document_xml", "document_html"]:
Packit Service a31ea6
                return xmlDoc(_obj=self._o)
Packit Service a31ea6
            else:
Packit Service a31ea6
                return None
Packit Service a31ea6
        return xmlDoc(_obj=ret)
Packit Service a31ea6
    #
Packit Service a31ea6
    # Those are common attributes to nearly all type of nodes
Packit Service a31ea6
    # defined as python2 properties
Packit Service a31ea6
    # 
Packit Service a31ea6
    import sys
Packit Service a31ea6
    if float(sys.version[0:3]) < 2.2:
Packit Service a31ea6
        def __getattr__(self, attr):
Packit Service a31ea6
            if attr == "parent":
Packit Service a31ea6
                ret = libxml2mod.parent(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return nodeWrap(ret)
Packit Service a31ea6
            elif attr == "properties":
Packit Service a31ea6
                ret = libxml2mod.properties(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return xmlAttr(_obj=ret)
Packit Service a31ea6
            elif attr == "children":
Packit Service a31ea6
                ret = libxml2mod.children(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return nodeWrap(ret)
Packit Service a31ea6
            elif attr == "last":
Packit Service a31ea6
                ret = libxml2mod.last(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return nodeWrap(ret)
Packit Service a31ea6
            elif attr == "next":
Packit Service a31ea6
                ret = libxml2mod.next(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return nodeWrap(ret)
Packit Service a31ea6
            elif attr == "prev":
Packit Service a31ea6
                ret = libxml2mod.prev(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    return None
Packit Service a31ea6
                return nodeWrap(ret)
Packit Service a31ea6
            elif attr == "content":
Packit Service a31ea6
                return libxml2mod.xmlNodeGetContent(self._o)
Packit Service a31ea6
            elif attr == "name":
Packit Service a31ea6
                return libxml2mod.name(self._o)
Packit Service a31ea6
            elif attr == "type":
Packit Service a31ea6
                return libxml2mod.type(self._o)
Packit Service a31ea6
            elif attr == "doc":
Packit Service a31ea6
                ret = libxml2mod.doc(self._o)
Packit Service a31ea6
                if ret == None:
Packit Service a31ea6
                    if self.type == "document_xml" or self.type == "document_html":
Packit Service a31ea6
                        return xmlDoc(_obj=self._o)
Packit Service a31ea6
                    else:
Packit Service a31ea6
                        return None
Packit Service a31ea6
                return xmlDoc(_obj=ret)
Packit Service a31ea6
            raise AttributeError(attr)
Packit Service a31ea6
    else:
Packit Service a31ea6
        parent = property(get_parent, None, None, "Parent node")
Packit Service a31ea6
        children = property(get_children, None, None, "First child node")
Packit Service a31ea6
        last = property(get_last, None, None, "Last sibling node")
Packit Service a31ea6
        next = property(get_next, None, None, "Next sibling node")
Packit Service a31ea6
        prev = property(get_prev, None, None, "Previous sibling node")
Packit Service a31ea6
        properties = property(get_properties, None, None, "List of properies")
Packit Service a31ea6
        content = property(get_content, None, None, "Content of this node")
Packit Service a31ea6
        name = property(get_name, None, None, "Node name")
Packit Service a31ea6
        type = property(get_type, None, None, "Node type")
Packit Service a31ea6
        doc = property(get_doc, None, None, "The document this node belongs to")
Packit Service a31ea6
Packit Service a31ea6
    #
Packit Service a31ea6
    # Serialization routines, the optional arguments have the following
Packit Service a31ea6
    # meaning:
Packit Service a31ea6
    #     encoding: string to ask saving in a specific encoding
Packit Service a31ea6
    #     indent: if 1 the serializer is asked to indent the output
Packit Service a31ea6
    #
Packit Service a31ea6
    def serialize(self, encoding = None, format = 0):
Packit Service a31ea6
        return libxml2mod.serializeNode(self._o, encoding, format)
Packit Service a31ea6
    def saveTo(self, file, encoding = None, format = 0):
Packit Service a31ea6
        return libxml2mod.saveNodeTo(self._o, file, encoding, format)
Packit Service a31ea6
            
Packit Service a31ea6
    #
Packit Service a31ea6
    # Canonicalization routines:
Packit Service a31ea6
    #
Packit Service a31ea6
    #   nodes: the node set (tuple or list) to be included in the
Packit Service a31ea6
    #     canonized image or None if all document nodes should be
Packit Service a31ea6
    #     included.
Packit Service a31ea6
    #   exclusive: the exclusive flag (0 - non-exclusive
Packit Service a31ea6
    #     canonicalization; otherwise - exclusive canonicalization)
Packit Service a31ea6
    #   prefixes: the list of inclusive namespace prefixes (strings),
Packit Service a31ea6
    #     or None if there is no inclusive namespaces (only for
Packit Service a31ea6
    #     exclusive canonicalization, ignored otherwise)
Packit Service a31ea6
    #   with_comments: include comments in the result (!=0) or not
Packit Service a31ea6
    #     (==0)
Packit Service a31ea6
    def c14nMemory(self,
Packit Service a31ea6
                   nodes=None,
Packit Service a31ea6
                   exclusive=0,
Packit Service a31ea6
                   prefixes=None,
Packit Service a31ea6
                   with_comments=0):
Packit Service a31ea6
        if nodes:
Packit Service a31ea6
            nodes = [n._o for n in nodes]
Packit Service a31ea6
        return libxml2mod.xmlC14NDocDumpMemory(
Packit Service a31ea6
            self.get_doc()._o,
Packit Service a31ea6
            nodes,
Packit Service a31ea6
            exclusive != 0,
Packit Service a31ea6
            prefixes,
Packit Service a31ea6
            with_comments != 0)
Packit Service a31ea6
    def c14nSaveTo(self,
Packit Service a31ea6
                   file,
Packit Service a31ea6
                   nodes=None,
Packit Service a31ea6
                   exclusive=0,
Packit Service a31ea6
                   prefixes=None,
Packit Service a31ea6
                   with_comments=0):
Packit Service a31ea6
        if nodes:
Packit Service a31ea6
            nodes = [n._o for n in nodes]
Packit Service a31ea6
        return libxml2mod.xmlC14NDocSaveTo(
Packit Service a31ea6
            self.get_doc()._o,
Packit Service a31ea6
            nodes,
Packit Service a31ea6
            exclusive != 0,
Packit Service a31ea6
            prefixes,
Packit Service a31ea6
            with_comments != 0,
Packit Service a31ea6
            file)
Packit Service a31ea6
Packit Service a31ea6
    #
Packit Service a31ea6
    # Selecting nodes using XPath, a bit slow because the context
Packit Service a31ea6
    # is allocated/freed every time but convenient.
Packit Service a31ea6
    #
Packit Service a31ea6
    def xpathEval(self, expr):
Packit Service a31ea6
        doc = self.doc
Packit Service a31ea6
        if doc == None:
Packit Service a31ea6
            return None
Packit Service a31ea6
        ctxt = doc.xpathNewContext()
Packit Service a31ea6
        ctxt.setContextNode(self)
Packit Service a31ea6
        res = ctxt.xpathEval(expr)
Packit Service a31ea6
        ctxt.xpathFreeContext()
Packit Service a31ea6
        return res
Packit Service a31ea6
Packit Service a31ea6
#    #
Packit Service a31ea6
#    # Selecting nodes using XPath, faster because the context
Packit Service a31ea6
#    # is allocated just once per xmlDoc.
Packit Service a31ea6
#    #
Packit Service a31ea6
#    # Removed: DV memleaks c.f. #126735
Packit Service a31ea6
#    #
Packit Service a31ea6
#    def xpathEval2(self, expr):
Packit Service a31ea6
#        doc = self.doc
Packit Service a31ea6
#        if doc == None:
Packit Service a31ea6
#            return None
Packit Service a31ea6
#        try:
Packit Service a31ea6
#            doc._ctxt.setContextNode(self)
Packit Service a31ea6
#        except:
Packit Service a31ea6
#            doc._ctxt = doc.xpathNewContext()
Packit Service a31ea6
#            doc._ctxt.setContextNode(self)
Packit Service a31ea6
#        res = doc._ctxt.xpathEval(expr)
Packit Service a31ea6
#        return res
Packit Service a31ea6
    def xpathEval2(self, expr):
Packit Service a31ea6
        return self.xpathEval(expr)
Packit Service a31ea6
Packit Service a31ea6
    # Remove namespaces
Packit Service a31ea6
    def removeNsDef(self, href):
Packit Service a31ea6
        """
Packit Service a31ea6
        Remove a namespace definition from a node.  If href is None,
Packit Service a31ea6
        remove all of the ns definitions on that node.  The removed
Packit Service a31ea6
        namespaces are returned as a linked list.
Packit Service a31ea6
Packit Service a31ea6
        Note: If any child nodes referred to the removed namespaces,
Packit Service a31ea6
        they will be left with dangling links.  You should call
Packit Service a31ea6
        renconciliateNs() to fix those pointers.
Packit Service a31ea6
Packit Service a31ea6
        Note: This method does not free memory taken by the ns
Packit Service a31ea6
        definitions.  You will need to free it manually with the
Packit Service a31ea6
        freeNsList() method on the returns xmlNs object.
Packit Service a31ea6
        """
Packit Service a31ea6
Packit Service a31ea6
        ret = libxml2mod.xmlNodeRemoveNsDef(self._o, href)
Packit Service a31ea6
        if ret is None:return None
Packit Service a31ea6
        __tmp = xmlNs(_obj=ret)
Packit Service a31ea6
        return __tmp
Packit Service a31ea6
Packit Service a31ea6
    # support for python2 iterators
Packit Service a31ea6
    def walk_depth_first(self):
Packit Service a31ea6
        return xmlCoreDepthFirstItertor(self)
Packit Service a31ea6
    def walk_breadth_first(self):
Packit Service a31ea6
        return xmlCoreBreadthFirstItertor(self)
Packit Service a31ea6
    __iter__ = walk_depth_first
Packit Service a31ea6
Packit Service a31ea6
    def free(self):
Packit Service a31ea6
        try:
Packit Service a31ea6
            self.doc._ctxt.xpathFreeContext()
Packit Service a31ea6
        except:
Packit Service a31ea6
            pass
Packit Service a31ea6
        libxml2mod.xmlFreeDoc(self._o)
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# implements the depth-first iterator for libxml2 DOM tree
Packit Service a31ea6
#
Packit Service a31ea6
class xmlCoreDepthFirstItertor:
Packit Service a31ea6
    def __init__(self, node):
Packit Service a31ea6
        self.node = node
Packit Service a31ea6
        self.parents = []
Packit Service a31ea6
    def __iter__(self):
Packit Service a31ea6
        return self
Packit Service a31ea6
    def __next__(self):
Packit Service a31ea6
        while 1:
Packit Service a31ea6
            if self.node:
Packit Service a31ea6
                ret = self.node
Packit Service a31ea6
                self.parents.append(self.node)
Packit Service a31ea6
                self.node = self.node.children
Packit Service a31ea6
                return ret
Packit Service a31ea6
            try:
Packit Service a31ea6
                parent = self.parents.pop()
Packit Service a31ea6
            except IndexError:
Packit Service a31ea6
                raise StopIteration
Packit Service a31ea6
            self.node = parent.next
Packit Service a31ea6
    next = __next__
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# implements the breadth-first iterator for libxml2 DOM tree
Packit Service a31ea6
#
Packit Service a31ea6
class xmlCoreBreadthFirstItertor:
Packit Service a31ea6
    def __init__(self, node):
Packit Service a31ea6
        self.node = node
Packit Service a31ea6
        self.parents = []
Packit Service a31ea6
    def __iter__(self):
Packit Service a31ea6
        return self
Packit Service a31ea6
    def __next__(self):
Packit Service a31ea6
        while 1:
Packit Service a31ea6
            if self.node:
Packit Service a31ea6
                ret = self.node
Packit Service a31ea6
                self.parents.append(self.node)
Packit Service a31ea6
                self.node = self.node.next
Packit Service a31ea6
                return ret
Packit Service a31ea6
            try:
Packit Service a31ea6
                parent = self.parents.pop()
Packit Service a31ea6
            except IndexError:
Packit Service a31ea6
                raise StopIteration
Packit Service a31ea6
            self.node = parent.children
Packit Service a31ea6
    next = __next__
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# converters to present a nicer view of the XPath returns
Packit Service a31ea6
#
Packit Service a31ea6
def nodeWrap(o):
Packit Service a31ea6
    # TODO try to cast to the most appropriate node class
Packit Service a31ea6
    name = libxml2mod.type(o)
Packit Service a31ea6
    if name == "element" or name == "text":
Packit Service a31ea6
        return xmlNode(_obj=o)
Packit Service a31ea6
    if name == "attribute":
Packit Service a31ea6
        return xmlAttr(_obj=o)
Packit Service a31ea6
    if name[0:8] == "document":
Packit Service a31ea6
        return xmlDoc(_obj=o)
Packit Service a31ea6
    if name == "namespace":
Packit Service a31ea6
        return xmlNs(_obj=o)
Packit Service a31ea6
    if name == "elem_decl":
Packit Service a31ea6
        return xmlElement(_obj=o)
Packit Service a31ea6
    if name == "attribute_decl":
Packit Service a31ea6
        return xmlAttribute(_obj=o)
Packit Service a31ea6
    if name == "entity_decl":
Packit Service a31ea6
        return xmlEntity(_obj=o)
Packit Service a31ea6
    if name == "dtd":
Packit Service a31ea6
        return xmlDtd(_obj=o)
Packit Service a31ea6
    return xmlNode(_obj=o)
Packit Service a31ea6
Packit Service a31ea6
def xpathObjectRet(o):
Packit Service a31ea6
    otype = type(o)
Packit Service a31ea6
    if otype == type([]):
Packit Service a31ea6
        ret = list(map(xpathObjectRet, o))
Packit Service a31ea6
        return ret
Packit Service a31ea6
    elif otype == type(()):
Packit Service a31ea6
        ret = list(map(xpathObjectRet, o))
Packit Service a31ea6
        return tuple(ret)
Packit Service a31ea6
    elif otype == type('') or otype == type(0) or otype == type(0.0):
Packit Service a31ea6
        return o
Packit Service a31ea6
    else:
Packit Service a31ea6
        return nodeWrap(o)
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# register an XPath function
Packit Service a31ea6
#
Packit Service a31ea6
def registerXPathFunction(ctxt, name, ns_uri, f):
Packit Service a31ea6
    ret = libxml2mod.xmlRegisterXPathFunction(ctxt, name, ns_uri, f)
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# For the xmlTextReader parser configuration
Packit Service a31ea6
#
Packit Service a31ea6
PARSER_LOADDTD=1
Packit Service a31ea6
PARSER_DEFAULTATTRS=2
Packit Service a31ea6
PARSER_VALIDATE=3
Packit Service a31ea6
PARSER_SUBST_ENTITIES=4
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# For the error callback severities
Packit Service a31ea6
#
Packit Service a31ea6
PARSER_SEVERITY_VALIDITY_WARNING=1
Packit Service a31ea6
PARSER_SEVERITY_VALIDITY_ERROR=2
Packit Service a31ea6
PARSER_SEVERITY_WARNING=3
Packit Service a31ea6
PARSER_SEVERITY_ERROR=4
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# register the libxml2 error handler
Packit Service a31ea6
#
Packit Service a31ea6
def registerErrorHandler(f, ctx):
Packit Service a31ea6
    """Register a Python written function to for error reporting.
Packit Service a31ea6
       The function is called back as f(ctx, error). """
Packit Service a31ea6
    import sys
Packit Service a31ea6
    if 'libxslt' not in sys.modules:
Packit Service a31ea6
        # normal behaviour when libxslt is not imported
Packit Service a31ea6
        ret = libxml2mod.xmlRegisterErrorHandler(f,ctx)
Packit Service a31ea6
    else:
Packit Service a31ea6
        # when libxslt is already imported, one must
Packit Service a31ea6
        # use libxst's error handler instead
Packit Service a31ea6
        import libxslt
Packit Service a31ea6
        ret = libxslt.registerErrorHandler(f,ctx)
Packit Service a31ea6
    return ret
Packit Service a31ea6
Packit Service a31ea6
class parserCtxtCore:
Packit Service a31ea6
Packit Service a31ea6
    def __init__(self, _obj=None):
Packit Service a31ea6
        if _obj != None: 
Packit Service a31ea6
            self._o = _obj;
Packit Service a31ea6
            return
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def __del__(self):
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlFreeParserCtxt(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def setErrorHandler(self,f,arg):
Packit Service a31ea6
        """Register an error handler that will be called back as
Packit Service a31ea6
           f(arg,msg,severity,reserved).
Packit Service a31ea6
           
Packit Service a31ea6
           @reserved is currently always None."""
Packit Service a31ea6
        libxml2mod.xmlParserCtxtSetErrorHandler(self._o,f,arg)
Packit Service a31ea6
Packit Service a31ea6
    def getErrorHandler(self):
Packit Service a31ea6
        """Return (f,arg) as previously registered with setErrorHandler
Packit Service a31ea6
           or (None,None)."""
Packit Service a31ea6
        return libxml2mod.xmlParserCtxtGetErrorHandler(self._o)
Packit Service a31ea6
Packit Service a31ea6
    def addLocalCatalog(self, uri):
Packit Service a31ea6
        """Register a local catalog with the parser"""
Packit Service a31ea6
        return libxml2mod.addLocalCatalog(self._o, uri)
Packit Service a31ea6
    
Packit Service a31ea6
Packit Service a31ea6
class ValidCtxtCore:
Packit Service a31ea6
Packit Service a31ea6
    def __init__(self, *args, **kw):
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def setValidityErrorHandler(self, err_func, warn_func, arg=None):
Packit Service a31ea6
        """
Packit Service a31ea6
        Register error and warning handlers for DTD validation.
Packit Service a31ea6
        These will be called back as f(msg,arg)
Packit Service a31ea6
        """
Packit Service a31ea6
        libxml2mod.xmlSetValidErrors(self._o, err_func, warn_func, arg)
Packit Service a31ea6
    
Packit Service a31ea6
Packit Service a31ea6
class SchemaValidCtxtCore:
Packit Service a31ea6
Packit Service a31ea6
    def __init__(self, *args, **kw):
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def setValidityErrorHandler(self, err_func, warn_func, arg=None):
Packit Service a31ea6
        """
Packit Service a31ea6
        Register error and warning handlers for Schema validation.
Packit Service a31ea6
        These will be called back as f(msg,arg)
Packit Service a31ea6
        """
Packit Service a31ea6
        libxml2mod.xmlSchemaSetValidErrors(self._o, err_func, warn_func, arg)
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
class relaxNgValidCtxtCore:
Packit Service a31ea6
Packit Service a31ea6
    def __init__(self, *args, **kw):
Packit Service a31ea6
        pass
Packit Service a31ea6
Packit Service a31ea6
    def setValidityErrorHandler(self, err_func, warn_func, arg=None):
Packit Service a31ea6
        """
Packit Service a31ea6
        Register error and warning handlers for RelaxNG validation.
Packit Service a31ea6
        These will be called back as f(msg,arg)
Packit Service a31ea6
        """
Packit Service a31ea6
        libxml2mod.xmlRelaxNGSetValidErrors(self._o, err_func, warn_func, arg)
Packit Service a31ea6
Packit Service a31ea6
    
Packit Service a31ea6
def _xmlTextReaderErrorFunc(xxx_todo_changeme,msg,severity,locator):
Packit Service a31ea6
    """Intermediate callback to wrap the locator"""
Packit Service a31ea6
    (f,arg) = xxx_todo_changeme
Packit Service a31ea6
    return f(arg,msg,severity,xmlTextReaderLocator(locator))
Packit Service a31ea6
Packit Service a31ea6
class xmlTextReaderCore:
Packit Service a31ea6
Packit Service a31ea6
    def __init__(self, _obj=None):
Packit Service a31ea6
        self.input = None
Packit Service a31ea6
        if _obj != None:self._o = _obj;return
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def __del__(self):
Packit Service a31ea6
        if self._o != None:
Packit Service a31ea6
            libxml2mod.xmlFreeTextReader(self._o)
Packit Service a31ea6
        self._o = None
Packit Service a31ea6
Packit Service a31ea6
    def SetErrorHandler(self,f,arg):
Packit Service a31ea6
        """Register an error handler that will be called back as
Packit Service a31ea6
           f(arg,msg,severity,locator)."""
Packit Service a31ea6
        if f is None:
Packit Service a31ea6
            libxml2mod.xmlTextReaderSetErrorHandler(\
Packit Service a31ea6
                self._o,None,None)
Packit Service a31ea6
        else:
Packit Service a31ea6
            libxml2mod.xmlTextReaderSetErrorHandler(\
Packit Service a31ea6
                self._o,_xmlTextReaderErrorFunc,(f,arg))
Packit Service a31ea6
Packit Service a31ea6
    def GetErrorHandler(self):
Packit Service a31ea6
        """Return (f,arg) as previously registered with setErrorHandler
Packit Service a31ea6
           or (None,None)."""
Packit Service a31ea6
        f,arg = libxml2mod.xmlTextReaderGetErrorHandler(self._o)
Packit Service a31ea6
        if f is None:
Packit Service a31ea6
            return None,None
Packit Service a31ea6
        else:
Packit Service a31ea6
            # assert f is _xmlTextReaderErrorFunc
Packit Service a31ea6
            return arg
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# The cleanup now goes though a wrapper in libxml.c
Packit Service a31ea6
#
Packit Service a31ea6
def cleanupParser():
Packit Service a31ea6
    libxml2mod.xmlPythonCleanupParser()
Packit Service a31ea6
Packit Service a31ea6
#
Packit Service a31ea6
# The interface to xmlRegisterInputCallbacks.
Packit Service a31ea6
# Since this API does not allow to pass a data object along with
Packit Service a31ea6
# match/open callbacks, it is necessary to maintain a list of all
Packit Service a31ea6
# Python callbacks.
Packit Service a31ea6
#
Packit Service a31ea6
__input_callbacks = []
Packit Service a31ea6
def registerInputCallback(func):
Packit Service a31ea6
    def findOpenCallback(URI):
Packit Service a31ea6
        for cb in reversed(__input_callbacks):
Packit Service a31ea6
            o = cb(URI)
Packit Service a31ea6
            if o is not None:
Packit Service a31ea6
                return o
Packit Service a31ea6
    libxml2mod.xmlRegisterInputCallback(findOpenCallback)
Packit Service a31ea6
    __input_callbacks.append(func)
Packit Service a31ea6
Packit Service a31ea6
def popInputCallbacks():
Packit Service a31ea6
    # First pop python-level callbacks, when no more available - start
Packit Service a31ea6
    # popping built-in ones.
Packit Service a31ea6
    if len(__input_callbacks) > 0:
Packit Service a31ea6
        __input_callbacks.pop()
Packit Service a31ea6
    if len(__input_callbacks) == 0:
Packit Service a31ea6
        libxml2mod.xmlUnregisterInputCallback()
Packit Service a31ea6
Packit Service a31ea6
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
Packit Service a31ea6
#
Packit Service a31ea6
# Everything before this line comes from libxml.py 
Packit Service a31ea6
# Everything after this line is automatically generated
Packit Service a31ea6
#
Packit Service a31ea6
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
Packit Service a31ea6