File size: 8,779 Bytes
bc58406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""

On-device verification for the cross-compiled lxml wheel (norelro).



Run after installing:

    pip install lxml-6.1.1-cp312-cp312-android_24_x86_64.whl



Usage:

    python Test_lxml.py



Exit code 0 = everything required PASSed.



Generated by RIMI

"""
import sys

RESULTS = []


def test(name, fn):
    try:
        fn()
        RESULTS.append((name, "PASS", None))
    except Exception as exc:
        RESULTS.append((name, "FAIL", "%s: %s" % (type(exc).__name__, exc)))
        print("    ! %s -> %s: %s" % (name, type(exc).__name__, exc))


def section(title):
    print("=" * 60)
    print(title)
    print("=" * 60)


# ---------------------------------------------------------------------------
# 1. import / version
# ---------------------------------------------------------------------------
def import_lxml():
    import lxml.etree as etree
    print("    lxml.etree version", etree.LXML_VERSION)
    print("    libxml2", etree.LIBXML_VERSION)
    print("    libxslt", etree.LIBXSLT_VERSION)


def import_objectify():
    from lxml import objectify
    print("    lxml.objectify OK")


def import_html():
    from lxml import html
    print("    lxml.html OK")


def import_html_clean():
    from lxml_html_clean import clean
    print("    lxml_html_clean OK")


# ---------------------------------------------------------------------------
# 2. etree basics
# ---------------------------------------------------------------------------
def parse_string():
    from lxml import etree
    xml = b"<root><item id='1'>hello</item><item id='2'>world</item></root>"
    root = etree.fromstring(xml)
    assert root.tag == "root"
    items = root.findall("item")
    assert len(items) == 2
    assert items[0].get("id") == "1"
    assert items[0].text == "hello"


def parse_file():
    from lxml import etree
    xml = b"<data><row><a>1</a><b>2</b></row></data>"
    root = etree.fromstring(xml)
    row = root.find("row")
    assert int(row.find("a").text) == 1
    assert int(row.find("b").text) == 2


def tostring():
    from lxml import etree
    root = etree.Element("parent")
    child = etree.SubElement(root, "child")
    child.text = "text"
    s = etree.tostring(root, encoding="unicode")
    assert "<child>text</child>" in s


def xpath():
    from lxml import etree
    xml = b"<root><a x='1'/><a x='2'/><b/></root>"
    root = etree.fromstring(xml)
    a_list = root.xpath("//a")
    assert len(a_list) == 2
    vals = root.xpath("//a/@x")
    assert vals == ["1", "2"]
    first = root.xpath("//a[@x='2']")
    assert len(first) == 1


def namespaces():
    from lxml import etree
    ns = {"s": "http://example.com/ns"}
    xml = b'<root xmlns:s="http://example.com/ns"><s:item>ok</s:item></root>'
    root = etree.fromstring(xml)
    items = root.xpath("//s:item", namespaces=ns)
    assert len(items) == 1
    assert items[0].text == "ok"


# ---------------------------------------------------------------------------
# 3. build / modify trees
# ---------------------------------------------------------------------------
def build_tree():
    from lxml import etree
    root = etree.Element("root")
    for i in range(5):
        child = etree.SubElement(root, "item")
        child.set("index", str(i))
        child.text = "val_%d" % i
    assert len(root) == 5
    assert root[2].get("index") == "2"
    assert root[4].text == "val_4"


def modify_tree():
    from lxml import etree
    root = etree.Element("root")
    a = etree.SubElement(root, "a")
    b = etree.SubElement(root, "b")
    root.remove(b)
    assert len(root) == 1
    a.text = "modified"
    assert root[0].text == "modified"


# ---------------------------------------------------------------------------
# 4. HTML parsing
# ---------------------------------------------------------------------------
def html_parse():
    from lxml import html
    doc = html.fromstring("<html><body><p>Hello</p><p>World</p></body></html>")
    ps = doc.xpath("//p")
    assert len(ps) == 2
    assert ps[0].text == "Hello"


def html_tostring():
    from lxml import html
    doc = html.fromstring("<html><body><div id='main'>content</div></body></html>")
    s = html.tostring(doc, encoding="unicode")
    assert "content" in s
    assert 'id="main"' in s


# ---------------------------------------------------------------------------
# 5. html_clean (bundled dep)
# ---------------------------------------------------------------------------
def html_clean():
    from lxml_html_clean import Cleaner
    from lxml import html
    dirty = '<html><body><p>safe</p></body></html>'
    doc = html.fromstring(dirty)
    c = Cleaner()
    c.clean_html(doc)
    s = html.tostring(doc, encoding="unicode")
    assert "safe" in s


# ---------------------------------------------------------------------------
# 6. XSLT
# ---------------------------------------------------------------------------
def xslt_transform():
    from lxml import etree
    xml = b"<root><item>1</item><item>2</item></root>"
    xslt_str = b"""<xsl:stylesheet version="1.0"

        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">

        <out><xsl:for-each select="root/item">

          <val><xsl:value-of select="."/></val>

        </xsl:for-each></out>

      </xsl:template>

    </xsl:stylesheet>"""
    xml_doc = etree.fromstring(xml)
    xslt_doc = etree.fromstring(xslt_str)
    transform = etree.XSLT(xslt_doc)
    result = transform(xml_doc)
    s = etree.tostring(result, encoding="unicode")
    assert "<val>1</val>" in s
    assert "<val>2</val>" in s


# ---------------------------------------------------------------------------
# 7. XML schema validation
# ---------------------------------------------------------------------------
def schema_validate():
    from lxml import etree
    schema_xml = b"""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:element name="root">

        <xs:complexType>

          <xs:sequence>

            <xs:element name="name" type="xs:string"/>

            <xs:element name="age" type="xs:decimal"/>

          </xs:sequence>

        </xs:complexType>

      </xs:element>

    </xs:schema>"""
    schema_doc = etree.fromstring(schema_xml)
    schema = etree.XMLSchema(schema_doc)

    valid = etree.fromstring(b"<root><name>Alice</name><age>30</age></root>")
    assert schema.validate(valid)

    invalid = etree.fromstring(b"<root><name>Alice</name></root>")
    assert not schema.validate(invalid)


# ---------------------------------------------------------------------------
# 8. c14n (canonicalization)
# ---------------------------------------------------------------------------
def c14n():
    from lxml import etree
    xml = b'<root  attr="1"  ><child>text</child></root>'
    root = etree.fromstring(xml)
    c = etree.tostring(root, method="c14n")
    assert b'attr="1"' in c
    assert b"<child>" in c


# ---------------------------------------------------------------------------
def main():
    section("1. import / version")
    test("import lxml.etree", import_lxml)
    test("import lxml.objectify", import_objectify)
    test("import lxml.html", import_html)
    test("import lxml_html_clean", import_html_clean)

    section("2. etree basics")
    test("parse fromstring", parse_string)
    test("parse file-like", parse_file)
    test("tostring", tostring)
    test("xpath", xpath)
    test("namespaces", namespaces)

    section("3. build / modify trees")
    test("build tree", build_tree)
    test("modify tree", modify_tree)

    section("4. HTML parsing")
    test("html parse", html_parse)
    test("html tostring", html_tostring)

    section("5. html_clean (bundled dep)")
    test("clean HTML", html_clean)

    section("6. XSLT")
    test("xslt transform", xslt_transform)

    section("7. XML schema validation")
    test("schema validate", schema_validate)

    section("8. c14n")
    test("canonicalization", c14n)

    print()
    print("=" * 60)
    print("SUMMARY")
    print("=" * 60)
    fails = 0
    for name, status, why in RESULTS:
        mark = "  OK" if status == "PASS" else "FAIL"
        print("%s %s" % (mark, name))
        if why:
            print("        -> %s" % why)
        if status == "FAIL":
            fails += 1
    print()
    passed = len(RESULTS) - fails
    print("passed=%d failed=%d" % (passed, fails))
    if fails:
        print("RESULT: FAILED")
    else:
        print("RESULT: PASSED")
    sys.exit(1 if fails else 0)


if __name__ == "__main__":
    main()