comparison env/lib/python3.9/site-packages/lxml/includes/etreepublic.pxd @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # public Cython/C interface to lxml.etree
2
3 from lxml.includes cimport tree
4 from lxml.includes.tree cimport const_xmlChar
5
6 cdef extern from "lxml-version.h":
7 cdef char* LXML_VERSION_STRING
8
9 cdef extern from "etree_defs.h":
10 # test if c_node is considered an Element (i.e. Element, Comment, etc.)
11 cdef bint _isElement(tree.xmlNode* c_node) nogil
12
13 # return the namespace URI of the node or NULL
14 cdef const_xmlChar* _getNs(tree.xmlNode* node) nogil
15
16 # pair of macros for tree traversal
17 cdef void BEGIN_FOR_EACH_ELEMENT_FROM(tree.xmlNode* tree_top,
18 tree.xmlNode* start_node,
19 int start_node_inclusive) nogil
20 cdef void END_FOR_EACH_ELEMENT_FROM(tree.xmlNode* start_node) nogil
21
22 cdef extern from "etree_api.h":
23
24 # first function to call!
25 cdef int import_lxml__etree() except -1
26
27 ##########################################################################
28 # public ElementTree API classes
29
30 cdef class lxml.etree._Document [ object LxmlDocument ]:
31 cdef tree.xmlDoc* _c_doc
32
33 cdef class lxml.etree._Element [ object LxmlElement ]:
34 cdef _Document _doc
35 cdef tree.xmlNode* _c_node
36
37 cdef class lxml.etree.ElementBase(_Element) [ object LxmlElementBase ]:
38 pass
39
40 cdef class lxml.etree._ElementTree [ object LxmlElementTree ]:
41 cdef _Document _doc
42 cdef _Element _context_node
43
44 cdef class lxml.etree.ElementClassLookup [ object LxmlElementClassLookup ]:
45 cdef object (*_lookup_function)(object, _Document, tree.xmlNode*)
46
47 cdef class lxml.etree.FallbackElementClassLookup(ElementClassLookup) \
48 [ object LxmlFallbackElementClassLookup ]:
49 cdef ElementClassLookup fallback
50 cdef object (*_fallback_function)(object, _Document, tree.xmlNode*)
51
52 ##########################################################################
53 # creating Element objects
54
55 # create an Element for a C-node in the Document
56 cdef _Element elementFactory(_Document doc, tree.xmlNode* c_node)
57
58 # create an ElementTree for an Element
59 cdef _ElementTree elementTreeFactory(_Element context_node)
60
61 # create an ElementTree subclass for an Element
62 cdef _ElementTree newElementTree(_Element context_node, object subclass)
63
64 # create an ElementTree from an external document
65 cdef _ElementTree adoptExternalDocument(tree.xmlDoc* c_doc, parser, bint is_owned)
66
67 # create a new Element for an existing or new document (doc = None)
68 # builds Python object after setting text, tail, namespaces and attributes
69 cdef _Element makeElement(tag, _Document doc, parser,
70 text, tail, attrib, nsmap)
71
72 # create a new SubElement for an existing parent
73 # builds Python object after setting text, tail, namespaces and attributes
74 cdef _Element makeSubElement(_Element parent, tag, text, tail,
75 attrib, nsmap)
76
77 # deep copy a node to include it in the Document
78 cdef _Element deepcopyNodeToDocument(_Document doc, tree.xmlNode* c_root)
79
80 # set the internal lookup function for Element/Comment/PI classes
81 # use setElementClassLookupFunction(NULL, None) to reset it
82 # note that the lookup function *must always* return an _Element subclass!
83 cdef void setElementClassLookupFunction(
84 object (*function)(object, _Document, tree.xmlNode*), object state)
85
86 # lookup function that always returns the default Element class
87 # note that the first argument is expected to be None!
88 cdef object lookupDefaultElementClass(_1, _Document _2,
89 tree.xmlNode* c_node)
90
91 # lookup function for namespace/tag specific Element classes
92 # note that the first argument is expected to be None!
93 cdef object lookupNamespaceElementClass(_1, _Document _2,
94 tree.xmlNode* c_node)
95
96 # call the fallback lookup function of a FallbackElementClassLookup
97 cdef object callLookupFallback(FallbackElementClassLookup lookup,
98 _Document doc, tree.xmlNode* c_node)
99
100 ##########################################################################
101 # XML attribute access
102
103 # return an attribute value for a C attribute on a C element node
104 cdef object attributeValue(tree.xmlNode* c_element,
105 tree.xmlAttr* c_attrib_node)
106
107 # return the value of the attribute with 'ns' and 'name' (or None)
108 cdef object attributeValueFromNsName(tree.xmlNode* c_element,
109 const_xmlChar* c_ns, const_xmlChar* c_name)
110
111 # return the value of attribute "{ns}name", or the default value
112 cdef object getAttributeValue(_Element element, key, default)
113
114 # return an iterator over attribute names (1), values (2) or items (3)
115 # attributes must not be removed during iteration!
116 cdef object iterattributes(_Element element, int keysvalues)
117
118 # return the list of all attribute names (1), values (2) or items (3)
119 cdef list collectAttributes(tree.xmlNode* c_element, int keysvalues)
120
121 # set an attribute value on an element
122 # on failure, sets an exception and returns -1
123 cdef int setAttributeValue(_Element element, key, value) except -1
124
125 # delete an attribute
126 # on failure, sets an exception and returns -1
127 cdef int delAttribute(_Element element, key) except -1
128
129 # delete an attribute based on name and namespace URI
130 # returns -1 if the attribute was not found (no exception)
131 cdef int delAttributeFromNsName(tree.xmlNode* c_element,
132 const_xmlChar* c_href, const_xmlChar* c_name)
133
134 ##########################################################################
135 # XML node helper functions
136
137 # check if the element has at least one child
138 cdef bint hasChild(tree.xmlNode* c_node) nogil
139
140 # find child element number 'index' (supports negative indexes)
141 cdef tree.xmlNode* findChild(tree.xmlNode* c_node,
142 Py_ssize_t index) nogil
143
144 # find child element number 'index' starting at first one
145 cdef tree.xmlNode* findChildForwards(tree.xmlNode* c_node,
146 Py_ssize_t index) nogil
147
148 # find child element number 'index' starting at last one
149 cdef tree.xmlNode* findChildBackwards(tree.xmlNode* c_node,
150 Py_ssize_t index) nogil
151
152 # return next/previous sibling element of the node
153 cdef tree.xmlNode* nextElement(tree.xmlNode* c_node) nogil
154 cdef tree.xmlNode* previousElement(tree.xmlNode* c_node) nogil
155
156 ##########################################################################
157 # iterators (DEPRECATED API, don't use in new code!)
158
159 cdef class lxml.etree._ElementTagMatcher [ object LxmlElementTagMatcher ]:
160 cdef char* _href
161 cdef char* _name
162
163 # store "{ns}tag" (or None) filter for this matcher or element iterator
164 # ** unless _href *and* _name are set up 'by hand', this function *must*
165 # ** be called when subclassing the iterator below!
166 cdef void initTagMatch(_ElementTagMatcher matcher, tag)
167
168 cdef class lxml.etree._ElementIterator(_ElementTagMatcher) [
169 object LxmlElementIterator ]:
170 cdef _Element _node
171 cdef tree.xmlNode* (*_next_element)(tree.xmlNode*)
172
173 # store the initial node of the iterator if it matches the required tag
174 # or its next matching sibling if not
175 cdef void iteratorStoreNext(_ElementIterator iterator, _Element node)
176
177 ##########################################################################
178 # other helper functions
179
180 # check if a C node matches a tag name and namespace
181 # (NULL allowed for each => always matches)
182 cdef int tagMatches(tree.xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name)
183
184 # convert a UTF-8 char* to a Python string or unicode string
185 cdef object pyunicode(const_xmlChar* s)
186
187 # convert the string to UTF-8 using the normal lxml.etree semantics
188 cdef bytes utf8(object s)
189
190 # split a tag into a (URI, name) tuple, return None as URI for '{}tag'
191 cdef tuple getNsTag(object tag)
192
193 # split a tag into a (URI, name) tuple, return b'' as URI for '{}tag'
194 cdef tuple getNsTagWithEmptyNs(object tag)
195
196 # get the "{ns}tag" string for a C node
197 cdef object namespacedName(tree.xmlNode* c_node)
198
199 # get the "{ns}tag" string for a href/tagname pair (c_ns may be NULL)
200 cdef object namespacedNameFromNsName(const_xmlChar* c_ns, const_xmlChar* c_tag)
201
202 # check if the node has a text value (which may be '')
203 cdef bint hasText(tree.xmlNode* c_node) nogil
204
205 # check if the node has a tail value (which may be '')
206 cdef bint hasTail(tree.xmlNode* c_node) nogil
207
208 # get the text content of an element (or None)
209 cdef object textOf(tree.xmlNode* c_node)
210
211 # get the tail content of an element (or None)
212 cdef object tailOf(tree.xmlNode* c_node)
213
214 # set the text value of an element
215 cdef int setNodeText(tree.xmlNode* c_node, text) except -1
216
217 # set the tail text value of an element
218 cdef int setTailText(tree.xmlNode* c_node, text) except -1
219
220 # append an element to the children of a parent element
221 # deprecated: don't use, does not propagate exceptions!
222 # use appendChildToElement() instead
223 cdef void appendChild(_Element parent, _Element child)
224
225 # added in lxml 3.3 as a safe replacement for appendChild()
226 # return -1 for exception, 0 for ok
227 cdef int appendChildToElement(_Element parent, _Element child) except -1
228
229 # recursively lookup a namespace in element or ancestors, or create it
230 cdef tree.xmlNs* findOrBuildNodeNsPrefix(
231 _Document doc, tree.xmlNode* c_node, const_xmlChar* href, const_xmlChar* prefix)
232
233 # find the Document of an Element, ElementTree or Document (itself!)
234 cdef _Document documentOrRaise(object input)
235
236 # find the root Element of an Element (itself!), ElementTree or Document
237 cdef _Element rootNodeOrRaise(object input)