~ubuntu-branches/ubuntu/saucy/lxml/saucy-updates

« back to all changes in this revision

Viewing changes to src/lxml/includes/etreepublic.pxd

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-10-19 19:02:57 UTC
  • mfrom: (1.3.9)
  • Revision ID: package-import@ubuntu.com-20121019190257-ryczr7c7lbgrvi9h
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "lxml.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 a new Element for an existing or new document (doc = None)
 
65
    # builds Python object after setting text, tail, namespaces and attributes
 
66
    cdef _Element makeElement(tag, _Document doc, parser,
 
67
                              text, tail, attrib, nsmap)
 
68
 
 
69
    # create a new SubElement for an existing parent
 
70
    # builds Python object after setting text, tail, namespaces and attributes
 
71
    cdef _Element makeSubElement(_Element parent, tag, text, tail,
 
72
                                 attrib, nsmap)
 
73
 
 
74
    # deep copy a node to include it in the Document
 
75
    cdef _Element deepcopyNodeToDocument(_Document doc, tree.xmlNode* c_root)
 
76
 
 
77
    # set the internal lookup function for Element/Comment/PI classes
 
78
    # use setElementClassLookupFunction(NULL, None) to reset it
 
79
    # note that the lookup function *must always* return an _Element subclass!
 
80
    cdef void setElementClassLookupFunction(
 
81
         object (*function)(object, _Document, tree.xmlNode*), object state)
 
82
 
 
83
    # lookup function that always returns the default Element class
 
84
    # note that the first argument is expected to be None!
 
85
    cdef object lookupDefaultElementClass(_1, _Document _2,
 
86
                                          tree.xmlNode* c_node)
 
87
 
 
88
    # lookup function for namespace/tag specific Element classes
 
89
    # note that the first argument is expected to be None!
 
90
    cdef object lookupNamespaceElementClass(_1, _Document _2,
 
91
                                            tree.xmlNode* c_node)
 
92
 
 
93
    # call the fallback lookup function of a FallbackElementClassLookup
 
94
    cdef object callLookupFallback(FallbackElementClassLookup lookup,
 
95
                                   _Document doc, tree.xmlNode* c_node)
 
96
 
 
97
    ##########################################################################
 
98
    # XML attribute access
 
99
 
 
100
    # return an attribute value for a C attribute on a C element node
 
101
    cdef object attributeValue(tree.xmlNode* c_element,
 
102
                               tree.xmlAttr* c_attrib_node)
 
103
 
 
104
    # return the value of the attribute with 'ns' and 'name' (or None)
 
105
    cdef object attributeValueFromNsName(tree.xmlNode* c_element,
 
106
                                         const_xmlChar* c_ns, const_xmlChar* c_name)
 
107
 
 
108
    # return the value of attribute "{ns}name", or the default value
 
109
    cdef object getAttributeValue(_Element element, key, default)
 
110
 
 
111
    # return an iterator over attribute names (1), values (2) or items (3)
 
112
    # attributes must not be removed during iteration!
 
113
    cdef object iterattributes(_Element element, int keysvalues)
 
114
 
 
115
    # return the list of all attribute names (1), values (2) or items (3)
 
116
    cdef list collectAttributes(tree.xmlNode* c_element, int keysvalues)
 
117
 
 
118
    # set an attribute value on an element
 
119
    # on failure, sets an exception and returns -1
 
120
    cdef int setAttributeValue(_Element element, key, value) except -1
 
121
 
 
122
    # delete an attribute
 
123
    # on failure, sets an exception and returns -1
 
124
    cdef int delAttribute(_Element element, key) except -1
 
125
 
 
126
    # delete an attribute based on name and namespace URI
 
127
    # returns -1 if the attribute was not found (no exception)
 
128
    cdef int delAttributeFromNsName(tree.xmlNode* c_element,
 
129
                                    const_xmlChar* c_href, const_xmlChar* c_name)
 
130
 
 
131
    ##########################################################################
 
132
    # XML node helper functions
 
133
 
 
134
    # check if the element has at least one child
 
135
    cdef bint hasChild(tree.xmlNode* c_node) nogil
 
136
 
 
137
    # find child element number 'index' (supports negative indexes)
 
138
    cdef tree.xmlNode* findChild(tree.xmlNode* c_node,
 
139
                                 Py_ssize_t index) nogil
 
140
 
 
141
    # find child element number 'index' starting at first one
 
142
    cdef tree.xmlNode* findChildForwards(tree.xmlNode* c_node,
 
143
                                         Py_ssize_t index) nogil
 
144
 
 
145
    # find child element number 'index' starting at last one
 
146
    cdef tree.xmlNode* findChildBackwards(tree.xmlNode* c_node,
 
147
                                          Py_ssize_t index) nogil
 
148
 
 
149
    # return next/previous sibling element of the node
 
150
    cdef tree.xmlNode* nextElement(tree.xmlNode* c_node) nogil
 
151
    cdef tree.xmlNode* previousElement(tree.xmlNode* c_node) nogil
 
152
 
 
153
    ##########################################################################
 
154
    # iterators (deprecated API, don't use in new code)
 
155
 
 
156
    cdef class lxml.etree._ElementTagMatcher [ object LxmlElementTagMatcher ]:
 
157
        cdef char* _href
 
158
        cdef char* _name
 
159
 
 
160
    # store "{ns}tag" (or None) filter for this matcher or element iterator
 
161
    # ** unless _href *and* _name are set up 'by hand', this function *must*
 
162
    # ** be called when subclassing the iterator below!
 
163
    cdef void initTagMatch(_ElementTagMatcher matcher, tag)
 
164
 
 
165
    cdef class lxml.etree._ElementIterator(_ElementTagMatcher) [
 
166
        object LxmlElementIterator ]:
 
167
        cdef _Element _node
 
168
        cdef tree.xmlNode* (*_next_element)(tree.xmlNode*)
 
169
 
 
170
    # store the initial node of the iterator if it matches the required tag
 
171
    # or its next matching sibling if not
 
172
    cdef void iteratorStoreNext(_ElementIterator iterator, _Element node)
 
173
 
 
174
    ##########################################################################
 
175
    # other helper functions
 
176
 
 
177
    # check if a C node matches a tag name and namespace
 
178
    # (NULL allowed for each => always matches)
 
179
    cdef int tagMatches(tree.xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name)
 
180
 
 
181
    # convert a UTF-8 char* to a Python string or unicode string
 
182
    cdef object pyunicode(const_xmlChar* s)
 
183
 
 
184
    # convert the string to UTF-8 using the normal lxml.etree semantics
 
185
    cdef bytes utf8(object s)
 
186
 
 
187
    # split a tag into a (URI, name) tuple, return None as URI for '{}tag'
 
188
    cdef tuple getNsTag(object tag)
 
189
 
 
190
    # split a tag into a (URI, name) tuple, return b'' as URI for '{}tag'
 
191
    cdef tuple getNsTagWithEmptyNs(object tag)
 
192
 
 
193
    # get the "{ns}tag" string for a C node
 
194
    cdef object namespacedName(tree.xmlNode* c_node)
 
195
 
 
196
    # get the "{ns}tag" string for a href/tagname pair (c_ns may be NULL)
 
197
    cdef object namespacedNameFromNsName(const_xmlChar* c_ns, const_xmlChar* c_tag)
 
198
 
 
199
    # check if the node has a text value (which may be '')
 
200
    cdef bint hasText(tree.xmlNode* c_node) nogil
 
201
 
 
202
    # check if the node has a tail value (which may be '')
 
203
    cdef bint hasTail(tree.xmlNode* c_node) nogil
 
204
 
 
205
    # get the text content of an element (or None)
 
206
    cdef object textOf(tree.xmlNode* c_node)
 
207
 
 
208
    # get the tail content of an element (or None)
 
209
    cdef object tailOf(tree.xmlNode* c_node)
 
210
 
 
211
    # set the text value of an element
 
212
    cdef int setNodeText(tree.xmlNode* c_node, text) except -1
 
213
 
 
214
    # set the tail text value of an element
 
215
    cdef int setTailText(tree.xmlNode* c_node, text) except -1
 
216
 
 
217
    # append an element to the children of a parent element
 
218
    cdef void appendChild(_Element parent, _Element child)
 
219
 
 
220
    # recursively lookup a namespace in element or ancestors, or create it
 
221
    cdef tree.xmlNs* findOrBuildNodeNsPrefix(
 
222
        _Document doc, tree.xmlNode* c_node, const_xmlChar* href, const_xmlChar* prefix)
 
223
 
 
224
    # find the Document of an Element, ElementTree or Document (itself!)
 
225
    cdef _Document documentOrRaise(object input)
 
226
 
 
227
    # find the root Element of an Element (itself!), ElementTree or Document
 
228
    cdef _Element rootNodeOrRaise(object input)