1
2
3 """
4 Tests specific to the lxml.objectify API
5 """
6
7
8 import unittest, operator, sys, os.path
9
10 this_dir = os.path.dirname(__file__)
11 if this_dir not in sys.path:
12 sys.path.insert(0, this_dir)
13
14 from common_imports import etree, HelperTestCase, fileInTestDir
15 from common_imports import SillyFileLike, canonicalize, doctest, make_doctest
16 from common_imports import _bytes, _str, StringIO, BytesIO
17
18 from lxml import objectify
19
20 PYTYPE_NAMESPACE = "http://codespeak.net/lxml/objectify/pytype"
21 XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"
22 XML_SCHEMA_INSTANCE_NS = "http://www.w3.org/2001/XMLSchema-instance"
23 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
24 XML_SCHEMA_NIL_ATTR = "{%s}nil" % XML_SCHEMA_INSTANCE_NS
25 TREE_PYTYPE = "TREE"
26 DEFAULT_NSMAP = { "py" : PYTYPE_NAMESPACE,
27 "xsi" : XML_SCHEMA_INSTANCE_NS,
28 "xsd" : XML_SCHEMA_NS}
29
30 objectclass2xsitype = {
31
32 objectify.IntElement: ("int", "short", "byte", "unsignedShort",
33 "unsignedByte", "integer", "nonPositiveInteger",
34 "negativeInteger", "long", "nonNegativeInteger",
35 "unsignedLong", "unsignedInt", "positiveInteger",),
36 objectify.FloatElement: ("float", "double"),
37 objectify.BoolElement: ("boolean",),
38 objectify.StringElement: ("string", "normalizedString", "token", "language",
39 "Name", "NCName", "ID", "IDREF", "ENTITY",
40 "NMTOKEN", ),
41
42 }
43
44 xsitype2objclass = dict([ (v, k) for k in objectclass2xsitype
45 for v in objectclass2xsitype[k] ])
46
47 objectclass2pytype = {
48
49 objectify.IntElement: "int",
50 objectify.FloatElement: "float",
51 objectify.BoolElement: "bool",
52 objectify.StringElement: "str",
53
54 }
55
56 pytype2objclass = dict([ (objectclass2pytype[k], k)
57 for k in objectclass2pytype])
58
59 xml_str = '''\
60 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
61 <obj:c1 a1="A1" a2="A2" other:a3="A3">
62 <obj:c2>0</obj:c2>
63 <obj:c2>1</obj:c2>
64 <obj:c2>2</obj:c2>
65 <other:c2>3</other:c2>
66 <c2>3</c2>
67 </obj:c1>
68 </obj:root>'''
69
71 """Test cases for lxml.objectify
72 """
73 etree = etree
74
77
91
105
106
110
115
122
132
137
143
151
162
166
171
178
188
193
199
207
218
220
221 value = objectify.DataElement(23, _pytype="str", _xsi="foobar",
222 attrib={"gnu": "muh", "cat": "meeow",
223 "dog": "wuff"},
224 bird="tchilp", dog="grrr")
225 self.assertEquals(value.get("gnu"), "muh")
226 self.assertEquals(value.get("cat"), "meeow")
227 self.assertEquals(value.get("dog"), "grrr")
228 self.assertEquals(value.get("bird"), "tchilp")
229
241
243
244
245 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
246 attrib={"gnu": "muh", "cat": "meeow",
247 "dog": "wuff"},
248 bird="tchilp", dog="grrr")
249 value = objectify.DataElement(arg, _pytype="NoneType")
250 self.assert_(isinstance(value, objectify.NoneElement))
251 self.assertEquals(value.get(XML_SCHEMA_NIL_ATTR), "true")
252 self.assertEquals(value.text, None)
253 self.assertEquals(value.pyval, None)
254 for attr in arg.attrib:
255
256 self.assertEquals(value.get(attr), arg.get(attr))
257
259
260
261 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
262 attrib={"gnu": "muh", "cat": "meeow",
263 "dog": "wuff"},
264 bird="tchilp", dog="grrr")
265 value = objectify.DataElement(arg, _pytype="int")
266 self.assert_(isinstance(value, objectify.IntElement))
267 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
268 for attr in arg.attrib:
269 if not attr == objectify.PYTYPE_ATTRIBUTE:
270 self.assertEquals(value.get(attr), arg.get(attr))
271
273
274
275 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
276 attrib={"gnu": "muh", "cat": "meeow",
277 "dog": "wuff"},
278 bird="tchilp", dog="grrr")
279 value = objectify.DataElement(arg, _xsi="xsd:int")
280 self.assert_(isinstance(value, objectify.IntElement))
281 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
282 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
283 for attr in arg.attrib:
284 if not attr in [objectify.PYTYPE_ATTRIBUTE,
285 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
286 self.assertEquals(value.get(attr), arg.get(attr))
287
289
290
291 arg = objectify.DataElement(23, _pytype="str", _xsi="foobar",
292 attrib={"gnu": "muh", "cat": "meeow",
293 "dog": "wuff"},
294 bird="tchilp", dog="grrr")
295 value = objectify.DataElement(arg, _pytype="int", _xsi="xsd:int")
296 self.assert_(isinstance(value, objectify.IntElement))
297 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), "int")
298 self.assertEquals(value.get(XML_SCHEMA_INSTANCE_TYPE_ATTR), "xsd:int")
299 for attr in arg.attrib:
300 if not attr in [objectify.PYTYPE_ATTRIBUTE,
301 XML_SCHEMA_INSTANCE_TYPE_ATTR]:
302 self.assertEquals(value.get(attr), arg.get(attr))
303
307
311
316
321
325
329
333
338
340 root = self.XML(xml_str)
341 self.assertEquals("0", getattr(root.c1, "{objectified}c2").text)
342 self.assertEquals("3", getattr(root.c1, "{otherNS}c2").text)
343
345 root = self.XML(xml_str)
346 self.assertRaises(AttributeError, getattr, root.c1, "NOT_THERE")
347 self.assertRaises(AttributeError, getattr, root.c1, "{unknownNS}c2")
348
350 for val in [
351 2, 2**32, 1.2, "Won't get fooled again",
352 _str("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1'), True,
353 False, None]:
354 root = self.Element('root')
355 attrname = 'val'
356 setattr(root, attrname, val)
357 result = getattr(root, attrname)
358 self.assertEquals(val, result)
359 self.assertEquals(type(val), type(result.pyval))
360
362 root = self.Element('root')
363 attrname = 'val'
364 val = _bytes("W\xf6n't get f\xf6\xf6led \xe4g\xe4in", 'ISO-8859-1')
365 self.assertRaises(ValueError, setattr, root, attrname, val)
366 self.assertRaises(AttributeError, getattr, root, attrname)
367
374
376 root = self.XML(xml_str)
377 self.assertEquals(1, len(root.c1))
378
379 new_el = self.Element("test", myattr="5")
380 root.addattr("c1", new_el)
381 self.assertEquals(2, len(root.c1))
382 self.assertEquals(None, root.c1[0].get("myattr"))
383 self.assertEquals("5", root.c1[1].get("myattr"))
384
386 root = self.XML(xml_str)
387 self.assertEquals(1, len(root.c1))
388
389 new_el = self.Element("test")
390 self.etree.SubElement(new_el, "a", myattr="A")
391 self.etree.SubElement(new_el, "a", myattr="B")
392
393 root.addattr("c1", list(new_el.a))
394 self.assertEquals(3, len(root.c1))
395 self.assertEquals(None, root.c1[0].get("myattr"))
396 self.assertEquals("A", root.c1[1].get("myattr"))
397 self.assertEquals("B", root.c1[2].get("myattr"))
398
405
407 root = self.XML(xml_str)
408 self.assertEquals("0", root.c1.c2[0].text)
409 self.assertEquals("1", root.c1.c2[1].text)
410 self.assertEquals("2", root.c1.c2[2].text)
411 self.assertRaises(IndexError, operator.getitem, root.c1.c2, 3)
412
414 root = self.XML(xml_str)
415 self.assertEquals("0", root.c1.c2[0].text)
416 self.assertEquals("0", root.c1.c2[-3].text)
417 self.assertEquals("1", root.c1.c2[-2].text)
418 self.assertEquals("2", root.c1.c2[-1].text)
419 self.assertRaises(IndexError, operator.getitem, root.c1.c2, -4)
420
422 root = self.XML(xml_str)
423 self.assertEquals(1, len(root))
424 self.assertEquals(1, len(root.c1))
425 self.assertEquals(3, len(root.c1.c2))
426
435
441
451
456
461
462
463
465 root = self.XML("<root><c>c1</c><c>c2</c></root>")
466 self.assertEquals(["c1", "c2"],
467 [ c.text for c in root.c[:] ])
468
470 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
471 test_list = ["c1", "c2", "c3", "c4"]
472
473 self.assertEquals(test_list,
474 [ c.text for c in root.c[:] ])
475 self.assertEquals(test_list[1:2],
476 [ c.text for c in root.c[1:2] ])
477 self.assertEquals(test_list[-3:-1],
478 [ c.text for c in root.c[-3:-1] ])
479 self.assertEquals(test_list[-3:3],
480 [ c.text for c in root.c[-3:3] ])
481 self.assertEquals(test_list[-3000:3],
482 [ c.text for c in root.c[-3000:3] ])
483 self.assertEquals(test_list[-3:3000],
484 [ c.text for c in root.c[-3:3000] ])
485
487 root = self.XML("<root><c>c1</c><c>c2</c><c>c3</c><c>c4</c></root>")
488 test_list = ["c1", "c2", "c3", "c4"]
489
490 self.assertEquals(test_list,
491 [ c.text for c in root.c[:] ])
492 self.assertEquals(test_list[2:1:-1],
493 [ c.text for c in root.c[2:1:-1] ])
494 self.assertEquals(test_list[-1:-3:-1],
495 [ c.text for c in root.c[-1:-3:-1] ])
496 self.assertEquals(test_list[2:-3:-1],
497 [ c.text for c in root.c[2:-3:-1] ])
498 self.assertEquals(test_list[2:-3000:-1],
499 [ c.text for c in root.c[2:-3000:-1] ])
500
501
502
514
516 Element = self.Element
517 root = Element("root")
518 root.c = ["c1", "c2"]
519
520 c1 = root.c[0]
521 c2 = root.c[1]
522
523 self.assertEquals([c1,c2], list(root.c))
524 self.assertEquals(["c1", "c2"],
525 [ c.text for c in root.c ])
526
527 root2 = Element("root2")
528 root2.el = [ "test", "test" ]
529 self.assertEquals(["test", "test"],
530 [ el.text for el in root2.el ])
531
532 root.c = [ root2.el, root2.el ]
533 self.assertEquals(["test", "test"],
534 [ c.text for c in root.c ])
535 self.assertEquals(["test", "test"],
536 [ el.text for el in root2.el ])
537
538 root.c[:] = [ c1, c2, c2, c1 ]
539 self.assertEquals(["c1", "c2", "c2", "c1"],
540 [ c.text for c in root.c ])
541
543 Element = self.Element
544 root = Element("root")
545 l = ["c1", "c2", "c3", "c4"]
546 root.c = l
547
548 self.assertEquals(["c1", "c2", "c3", "c4"],
549 [ c.text for c in root.c ])
550 self.assertEquals(l,
551 [ c.text for c in root.c ])
552
553 new_slice = ["cA", "cB"]
554 l[1:2] = new_slice
555 root.c[1:2] = new_slice
556
557 self.assertEquals(["c1", "cA", "cB", "c3", "c4"], l)
558 self.assertEquals(["c1", "cA", "cB", "c3", "c4"],
559 [ c.text for c in root.c ])
560 self.assertEquals(l,
561 [ c.text for c in root.c ])
562
564 Element = self.Element
565 root = Element("root")
566 l = ["c1", "c2", "c3", "c4"]
567 root.c = l
568
569 self.assertEquals(["c1", "c2", "c3", "c4"],
570 [ c.text for c in root.c ])
571 self.assertEquals(l,
572 [ c.text for c in root.c ])
573
574 new_slice = ["cA", "cB"]
575 l[1:1] = new_slice
576 root.c[1:1] = new_slice
577
578 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"], l)
579 self.assertEquals(["c1", "cA", "cB", "c2", "c3", "c4"],
580 [ c.text for c in root.c ])
581 self.assertEquals(l,
582 [ c.text for c in root.c ])
583
585 Element = self.Element
586 root = Element("root")
587 l = ["c1", "c2", "c3", "c4"]
588 root.c = l
589
590 self.assertEquals(["c1", "c2", "c3", "c4"],
591 [ c.text for c in root.c ])
592 self.assertEquals(l,
593 [ c.text for c in root.c ])
594
595 new_slice = ["cA", "cB"]
596 l[-2:-2] = new_slice
597 root.c[-2:-2] = new_slice
598
599 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"], l)
600 self.assertEquals(["c1", "c2", "cA", "cB", "c3", "c4"],
601 [ c.text for c in root.c ])
602 self.assertEquals(l,
603 [ c.text for c in root.c ])
604
612
614 Element = self.Element
615 root = Element("root")
616 l = ["c1", "c2", "c3", "c4"]
617 root.c = l
618
619 self.assertEquals(["c1", "c2", "c3", "c4"],
620 [ c.text for c in root.c ])
621 self.assertEquals(l,
622 [ c.text for c in root.c ])
623
624 new_slice = ["cA", "cB", "cC"]
625 self.assertRaises(
626 ValueError, operator.setitem,
627 l, slice(1,2,-1), new_slice)
628 self.assertRaises(
629 ValueError, operator.setitem,
630 root.c, slice(1,2,-1), new_slice)
631
633 Element = self.Element
634 root = Element("root")
635 l = ["c1", "c2", "c3", "c4"]
636 root.c = l
637
638 self.assertEquals(["c1", "c2", "c3", "c4"],
639 [ c.text for c in root.c ])
640 self.assertEquals(l,
641 [ c.text for c in root.c ])
642
643 new_slice = ["cA", "cB"]
644 l[-1:1:-1] = new_slice
645 root.c[-1:1:-1] = new_slice
646
647 self.assertEquals(["c1", "c2", "cB", "cA"], l)
648 self.assertEquals(["c1", "c2", "cB", "cA"],
649 [ c.text for c in root.c ])
650 self.assertEquals(l,
651 [ c.text for c in root.c ])
652
654 Element = self.Element
655 root = Element("root")
656 l = ["c1", "c2", "c3", "c4"]
657 root.c = l
658
659 self.assertEquals(["c1", "c2", "c3", "c4"],
660 [ c.text for c in root.c ])
661 self.assertEquals(l,
662 [ c.text for c in root.c ])
663
664 new_slice = ["cA", "cB"]
665 l[-1:-4:-2] = new_slice
666 root.c[-1:-4:-2] = new_slice
667
668 self.assertEquals(["c1", "cB", "c3", "cA"], l)
669 self.assertEquals(["c1", "cB", "c3", "cA"],
670 [ c.text for c in root.c ])
671 self.assertEquals(l,
672 [ c.text for c in root.c ])
673
674
675
683
691
693
694 Element = self.Element
695 root = Element("root")
696
697 root["text"] = "TEST"
698 self.assertEquals(["TEST"],
699 [ c.text for c in root["text"] ])
700
701 root["tail"] = "TEST"
702 self.assertEquals(["TEST"],
703 [ c.text for c in root["tail"] ])
704
705 root["pyval"] = "TEST"
706 self.assertEquals(["TEST"],
707 [ c.text for c in root["pyval"] ])
708
709 root["tag"] = "TEST"
710 self.assertEquals(["TEST"],
711 [ c.text for c in root["tag"] ])
712
720
722 XML = self.XML
723 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
724 self.assertEquals(2, len(root.findall(".//{X}b")))
725 self.assertEquals(3, len(root.findall(".//b")))
726 self.assertEquals(2, len(root.findall("b")))
727
735
749
755
757 Element = self.Element
758 SubElement = self.etree.SubElement
759 root = Element("{objectified}root")
760 root.bool = True
761 self.assertEquals(root.bool, True)
762 self.assertEquals(root.bool + root.bool, True + True)
763 self.assertEquals(True + root.bool, True + root.bool)
764 self.assertEquals(root.bool * root.bool, True * True)
765 self.assertEquals(int(root.bool), int(True))
766 self.assertEquals(complex(root.bool), complex(True))
767 self.assert_(isinstance(root.bool, objectify.BoolElement))
768
769 root.bool = False
770 self.assertEquals(root.bool, False)
771 self.assertEquals(root.bool + root.bool, False + False)
772 self.assertEquals(False + root.bool, False + root.bool)
773 self.assertEquals(root.bool * root.bool, False * False)
774 self.assertEquals(int(root.bool), int(False))
775 self.assertEquals(complex(root.bool), complex(False))
776 self.assert_(isinstance(root.bool, objectify.BoolElement))
777
786
793
800
807
809 Element = self.Element
810 SubElement = self.etree.SubElement
811 root = Element("{objectified}root")
812 root.s = "test"
813
814 self.assertEquals("test" * 5, root.s * 5)
815 self.assertEquals(5 * "test", 5 * root.s)
816
817 self.assertRaises(TypeError, operator.mul, root.s, "honk")
818 self.assertRaises(TypeError, operator.mul, "honk", root.s)
819
829
850
855
860
865
874
879
884
889
896
903
910
922
932
937
942
947
954
959
966
971
980
989
995
1004
1006 pyval = 1
1007 pytype = "NoneType"
1008 objclass = objectify.NoneElement
1009 value = objectify.DataElement(pyval, _pytype=pytype)
1010 self.assert_(isinstance(value, objclass),
1011 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1012 % (pyval, pytype, type(value), objclass))
1013 self.assertEquals(value.text, None)
1014 self.assertEquals(value.pyval, None)
1015
1017
1018 pyval = 1
1019 pytype = "none"
1020 objclass = objectify.NoneElement
1021 value = objectify.DataElement(pyval, _pytype=pytype)
1022 self.assert_(isinstance(value, objclass),
1023 "DataElement(%s, _pytype='%s') returns %s, expected %s"
1024 % (pyval, pytype, type(value), objclass))
1025 self.assertEquals(value.text, None)
1026 self.assertEquals(value.pyval, None)
1027
1033 root = Element("{objectified}root")
1034 root.myfloat = MyFloat(5.5)
1035 self.assert_(isinstance(root.myfloat, objectify.FloatElement))
1036 self.assertEquals(root.myfloat.get(objectify.PYTYPE_ATTRIBUTE), None)
1037
1039 class MyFloat(float):
1040 pass
1041 value = objectify.DataElement(MyFloat(5.5))
1042 self.assert_(isinstance(value, objectify.FloatElement))
1043 self.assertEquals(value, 5.5)
1044 self.assertEquals(value.get(objectify.PYTYPE_ATTRIBUTE), None)
1045
1047 XML = self.XML
1048 root = XML('''\
1049 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1050 <b xsi:type="boolean">true</b>
1051 <b xsi:type="boolean">false</b>
1052 <b xsi:type="boolean">1</b>
1053 <b xsi:type="boolean">0</b>
1054
1055 <f xsi:type="float">5</f>
1056 <f xsi:type="double">5</f>
1057
1058 <s xsi:type="string">5</s>
1059 <s xsi:type="normalizedString">5</s>
1060 <s xsi:type="token">5</s>
1061 <s xsi:type="language">5</s>
1062 <s xsi:type="Name">5</s>
1063 <s xsi:type="NCName">5</s>
1064 <s xsi:type="ID">5</s>
1065 <s xsi:type="IDREF">5</s>
1066 <s xsi:type="ENTITY">5</s>
1067 <s xsi:type="NMTOKEN">5</s>
1068
1069 <l xsi:type="integer">5</l>
1070 <l xsi:type="nonPositiveInteger">5</l>
1071 <l xsi:type="negativeInteger">5</l>
1072 <l xsi:type="long">5</l>
1073 <l xsi:type="nonNegativeInteger">5</l>
1074 <l xsi:type="unsignedLong">5</l>
1075 <l xsi:type="unsignedInt">5</l>
1076 <l xsi:type="positiveInteger">5</l>
1077
1078 <i xsi:type="int">5</i>
1079 <i xsi:type="short">5</i>
1080 <i xsi:type="byte">5</i>
1081 <i xsi:type="unsignedShort">5</i>
1082 <i xsi:type="unsignedByte">5</i>
1083
1084 <n xsi:nil="true"/>
1085 </root>
1086 ''')
1087
1088 for b in root.b:
1089 self.assert_(isinstance(b, objectify.BoolElement))
1090 self.assertEquals(True, root.b[0])
1091 self.assertEquals(False, root.b[1])
1092 self.assertEquals(True, root.b[2])
1093 self.assertEquals(False, root.b[3])
1094
1095 for f in root.f:
1096 self.assert_(isinstance(f, objectify.FloatElement))
1097 self.assertEquals(5, f)
1098
1099 for s in root.s:
1100 self.assert_(isinstance(s, objectify.StringElement))
1101 self.assertEquals("5", s)
1102
1103 for i in root.i:
1104 self.assert_(isinstance(i, objectify.IntElement))
1105 self.assertEquals(5, i)
1106
1107 for l in root.l:
1108 self.assert_(isinstance(l, objectify.IntElement))
1109 self.assertEquals(5, i)
1110
1111 self.assert_(isinstance(root.n, objectify.NoneElement))
1112 self.assertEquals(None, root.n)
1113
1115 XML = self.XML
1116 root = XML('''\
1117 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1118 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1119 <b xsi:type="xsd:boolean">true</b>
1120 <b xsi:type="xsd:boolean">false</b>
1121 <b xsi:type="xsd:boolean">1</b>
1122 <b xsi:type="xsd:boolean">0</b>
1123
1124 <f xsi:type="xsd:float">5</f>
1125 <f xsi:type="xsd:double">5</f>
1126
1127 <s xsi:type="xsd:string">5</s>
1128 <s xsi:type="xsd:normalizedString">5</s>
1129 <s xsi:type="xsd:token">5</s>
1130 <s xsi:type="xsd:language">5</s>
1131 <s xsi:type="xsd:Name">5</s>
1132 <s xsi:type="xsd:NCName">5</s>
1133 <s xsi:type="xsd:ID">5</s>
1134 <s xsi:type="xsd:IDREF">5</s>
1135 <s xsi:type="xsd:ENTITY">5</s>
1136 <s xsi:type="xsd:NMTOKEN">5</s>
1137
1138 <l xsi:type="xsd:integer">5</l>
1139 <l xsi:type="xsd:nonPositiveInteger">5</l>
1140 <l xsi:type="xsd:negativeInteger">5</l>
1141 <l xsi:type="xsd:long">5</l>
1142 <l xsi:type="xsd:nonNegativeInteger">5</l>
1143 <l xsi:type="xsd:unsignedLong">5</l>
1144 <l xsi:type="xsd:unsignedInt">5</l>
1145 <l xsi:type="xsd:positiveInteger">5</l>
1146
1147 <i xsi:type="xsd:int">5</i>
1148 <i xsi:type="xsd:short">5</i>
1149 <i xsi:type="xsd:byte">5</i>
1150 <i xsi:type="xsd:unsignedShort">5</i>
1151 <i xsi:type="xsd:unsignedByte">5</i>
1152
1153 <n xsi:nil="true"/>
1154 </root>
1155 ''')
1156
1157 for b in root.b:
1158 self.assert_(isinstance(b, objectify.BoolElement))
1159 self.assertEquals(True, root.b[0])
1160 self.assertEquals(False, root.b[1])
1161 self.assertEquals(True, root.b[2])
1162 self.assertEquals(False, root.b[3])
1163
1164 for f in root.f:
1165 self.assert_(isinstance(f, objectify.FloatElement))
1166 self.assertEquals(5, f)
1167
1168 for s in root.s:
1169 self.assert_(isinstance(s, objectify.StringElement))
1170 self.assertEquals("5", s)
1171
1172 for i in root.i:
1173 self.assert_(isinstance(i, objectify.IntElement))
1174 self.assertEquals(5, i)
1175
1176 for l in root.l:
1177 self.assert_(isinstance(l, objectify.IntElement))
1178 self.assertEquals(5, l)
1179
1180 self.assert_(isinstance(root.n, objectify.NoneElement))
1181 self.assertEquals(None, root.n)
1182
1184 XML = self.XML
1185 root = XML(_bytes('<root><b>why</b><b>try</b></root>'))
1186 strs = [ str(s) for s in root.b ]
1187 self.assertEquals(["why", "try"],
1188 strs)
1189
1216
1236
1237
1238
1262
1264 XML = self.XML
1265 root = XML(_bytes("""
1266 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1267 <b xsi:nil="true"></b><b xsi:nil="true"/>
1268 </root>"""))
1269 self.assert_(root.b[0] == root.b[1])
1270 self.assertFalse(root.b[0])
1271 self.assertEquals(root.b[0], None)
1272 self.assertEquals(None, root.b[0])
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1288
1295
1299
1301 XML = self.XML
1302 root = XML(_bytes('''\
1303 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1304 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1305 <b>5</b>
1306 <b>test</b>
1307 <c>1.1</c>
1308 <c>\uF8D2</c>
1309 <x>true</x>
1310 <n xsi:nil="true" />
1311 <n></n>
1312 <b xsi:type="double">5</b>
1313 <b xsi:type="float">5</b>
1314 <s xsi:type="string">23</s>
1315 <s py:pytype="str">42</s>
1316 <f py:pytype="float">300</f>
1317 <l py:pytype="long">2</l>
1318 <t py:pytype="TREE"></t>
1319 </a>
1320 '''))
1321 objectify.annotate(root)
1322
1323 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1324 for c in root.iterchildren() ]
1325 self.assertEquals("int", child_types[ 0])
1326 self.assertEquals("str", child_types[ 1])
1327 self.assertEquals("float", child_types[ 2])
1328 self.assertEquals("str", child_types[ 3])
1329 self.assertEquals("bool", child_types[ 4])
1330 self.assertEquals("NoneType", child_types[ 5])
1331 self.assertEquals(None, child_types[ 6])
1332 self.assertEquals("float", child_types[ 7])
1333 self.assertEquals("float", child_types[ 8])
1334 self.assertEquals("str", child_types[ 9])
1335 self.assertEquals("int", child_types[10])
1336 self.assertEquals("int", child_types[11])
1337 self.assertEquals("int", child_types[12])
1338 self.assertEquals(None, child_types[13])
1339
1340 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1341
1361
1363 XML = self.XML
1364 root = XML(_bytes('''\
1365 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1366 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1367 <b>5</b>
1368 <b>test</b>
1369 <c>1.1</c>
1370 <c>\uF8D2</c>
1371 <x>true</x>
1372 <n xsi:nil="true" />
1373 <n></n>
1374 <b xsi:type="double">5</b>
1375 <b xsi:type="float">5</b>
1376 <s xsi:type="string">23</s>
1377 <s py:pytype="str">42</s>
1378 <f py:pytype="float">300</f>
1379 <l py:pytype="long">2</l>
1380 <t py:pytype="TREE"></t>
1381 </a>
1382 '''))
1383 objectify.annotate(root, ignore_old=False)
1384
1385 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1386 for c in root.iterchildren() ]
1387 self.assertEquals("int", child_types[ 0])
1388 self.assertEquals("str", child_types[ 1])
1389 self.assertEquals("float", child_types[ 2])
1390 self.assertEquals("str", child_types[ 3])
1391 self.assertEquals("bool", child_types[ 4])
1392 self.assertEquals("NoneType", child_types[ 5])
1393 self.assertEquals(None, child_types[ 6])
1394 self.assertEquals("float", child_types[ 7])
1395 self.assertEquals("float", child_types[ 8])
1396 self.assertEquals("str", child_types[ 9])
1397 self.assertEquals("str", child_types[10])
1398 self.assertEquals("float", child_types[11])
1399 self.assertEquals("int", child_types[12])
1400 self.assertEquals(TREE_PYTYPE, child_types[13])
1401
1402 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1403
1405 XML = self.XML
1406 root = XML(_bytes('''\
1407 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1408 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1409 <b>5</b>
1410 <b>test</b>
1411 <c>1.1</c>
1412 <c>\uF8D2</c>
1413 <x>true</x>
1414 <n xsi:nil="true" />
1415 <n></n>
1416 <b xsi:type="double">5</b>
1417 <b xsi:type="float">5</b>
1418 <s xsi:type="string">23</s>
1419 <s py:pytype="str">42</s>
1420 <f py:pytype="float">300</f>
1421 <l py:pytype="long">2</l>
1422 <t py:pytype="TREE"></t>
1423 </a>
1424 '''))
1425 objectify.annotate(root, ignore_old=False, ignore_xsi=False,
1426 annotate_xsi=1, annotate_pytype=1)
1427
1428
1429 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1430 for c in root.iterchildren() ]
1431 self.assertEquals("int", child_types[ 0])
1432 self.assertEquals("str", child_types[ 1])
1433 self.assertEquals("float", child_types[ 2])
1434 self.assertEquals("str", child_types[ 3])
1435 self.assertEquals("bool", child_types[ 4])
1436 self.assertEquals("NoneType", child_types[ 5])
1437 self.assertEquals(None, child_types[ 6])
1438 self.assertEquals("float", child_types[ 7])
1439 self.assertEquals("float", child_types[ 8])
1440 self.assertEquals("str", child_types[ 9])
1441 self.assertEquals("str", child_types[10])
1442 self.assertEquals("float", child_types[11])
1443 self.assertEquals("int", child_types[12])
1444 self.assertEquals(TREE_PYTYPE, child_types[13])
1445
1446 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1447
1448 child_xsitypes = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1449 for c in root.iterchildren() ]
1450
1451
1452 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1453 for c in root.iterchildren() ]
1454 self.assertEquals("xsd:integer", child_types[ 0])
1455 self.assertEquals("xsd:string", child_types[ 1])
1456 self.assertEquals("xsd:double", child_types[ 2])
1457 self.assertEquals("xsd:string", child_types[ 3])
1458 self.assertEquals("xsd:boolean", child_types[ 4])
1459 self.assertEquals(None, child_types[ 5])
1460 self.assertEquals(None, child_types[ 6])
1461 self.assertEquals("xsd:double", child_types[ 7])
1462 self.assertEquals("xsd:float", child_types[ 8])
1463 self.assertEquals("xsd:string", child_types[ 9])
1464 self.assertEquals("xsd:string", child_types[10])
1465 self.assertEquals("xsd:double", child_types[11])
1466 self.assertEquals("xsd:integer", child_types[12])
1467 self.assertEquals(None, child_types[13])
1468
1469 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1470
1472 XML = self.XML
1473 root = XML(_bytes('''\
1474 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1475 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1476 <b>5</b>
1477 <b>test</b>
1478 <c>1.1</c>
1479 <c>\uF8D2</c>
1480 <x>true</x>
1481 <n xsi:nil="true" />
1482 <n></n>
1483 <b xsi:type="double">5</b>
1484 <b xsi:type="float">5</b>
1485 <s xsi:type="string">23</s>
1486 <s py:pytype="str">42</s>
1487 <f py:pytype="float">300</f>
1488 <l py:pytype="long">2</l>
1489 <t py:pytype="TREE"></t>
1490 </a>
1491 '''))
1492 objectify.xsiannotate(root, ignore_old=False)
1493
1494 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1495 for c in root.iterchildren() ]
1496 self.assertEquals("xsd:integer", child_types[ 0])
1497 self.assertEquals("xsd:string", child_types[ 1])
1498 self.assertEquals("xsd:double", child_types[ 2])
1499 self.assertEquals("xsd:string", child_types[ 3])
1500 self.assertEquals("xsd:boolean", child_types[ 4])
1501 self.assertEquals(None, child_types[ 5])
1502 self.assertEquals(None, child_types[ 6])
1503 self.assertEquals("xsd:double", child_types[ 7])
1504 self.assertEquals("xsd:float", child_types[ 8])
1505 self.assertEquals("xsd:string", child_types[ 9])
1506 self.assertEquals("xsd:string", child_types[10])
1507 self.assertEquals("xsd:double", child_types[11])
1508 self.assertEquals("xsd:integer", child_types[12])
1509 self.assertEquals(None, child_types[13])
1510
1512 XML = self.XML
1513 root = XML(_bytes('''\
1514 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1515 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1516 <b>5</b>
1517 <b>test</b>
1518 <c>1.1</c>
1519 <c>\uF8D2</c>
1520 <x>true</x>
1521 <n xsi:nil="true" />
1522 <n></n>
1523 <b xsi:type="double">5</b>
1524 <b xsi:type="float">5</b>
1525 <s xsi:type="string">23</s>
1526 <s py:pytype="str">42</s>
1527 <f py:pytype="float">300</f>
1528 <l py:pytype="long">2</l>
1529 <t py:pytype="TREE"></t>
1530 </a>
1531 '''))
1532 objectify.pyannotate(root, ignore_old=True)
1533
1534 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1535 for c in root.iterchildren() ]
1536 self.assertEquals("int", child_types[ 0])
1537 self.assertEquals("str", child_types[ 1])
1538 self.assertEquals("float", child_types[ 2])
1539 self.assertEquals("str", child_types[ 3])
1540 self.assertEquals("bool", child_types[ 4])
1541 self.assertEquals("NoneType", child_types[ 5])
1542 self.assertEquals(None, child_types[ 6])
1543 self.assertEquals("float", child_types[ 7])
1544 self.assertEquals("float", child_types[ 8])
1545 self.assertEquals("str", child_types[ 9])
1546 self.assertEquals("int", child_types[10])
1547 self.assertEquals("int", child_types[11])
1548 self.assertEquals("int", child_types[12])
1549 self.assertEquals(None, child_types[13])
1550
1551 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1552
1572
1574 XML = self.XML
1575 root = XML('''\
1576 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1577 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1578 <b>5</b>
1579 <b>test</b>
1580 <c>1.1</c>
1581 <c>\uF8D2</c>
1582 <x>true</x>
1583 <n xsi:nil="true" />
1584 <n></n>
1585 <b xsi:type="double">5</b>
1586 <b xsi:type="float">5</b>
1587 <s xsi:type="string">23</s>
1588 <s py:pytype="str">42</s>
1589 <f py:pytype="float">300</f>
1590 <l py:pytype="long">2</l>
1591 <t py:pytype="TREE"></t>
1592 </a>
1593 ''')
1594 objectify.pyannotate(root)
1595
1596 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1597 for c in root.iterchildren() ]
1598 self.assertEquals("int", child_types[ 0])
1599 self.assertEquals("str", child_types[ 1])
1600 self.assertEquals("float", child_types[ 2])
1601 self.assertEquals("str", child_types[ 3])
1602 self.assertEquals("bool", child_types[ 4])
1603 self.assertEquals("NoneType", child_types[ 5])
1604 self.assertEquals(None, child_types[ 6])
1605 self.assertEquals("float", child_types[ 7])
1606 self.assertEquals("float", child_types[ 8])
1607 self.assertEquals("str", child_types[ 9])
1608 self.assertEquals("str", child_types[10])
1609 self.assertEquals("float", child_types[11])
1610 self.assertEquals("int", child_types[12])
1611 self.assertEquals(TREE_PYTYPE, child_types[13])
1612
1613 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1614
1616 XML = self.XML
1617 root = XML(_bytes('''\
1618 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1619 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1620 <b>5</b>
1621 <b>test</b>
1622 <c>1.1</c>
1623 <c>\uF8D2</c>
1624 <x>true</x>
1625 <n xsi:nil="true" />
1626 <n></n>
1627 <b xsi:type="double">5</b>
1628 <b xsi:type="float">5</b>
1629 <s xsi:type="string">23</s>
1630 <s py:pytype="str">42</s>
1631 <f py:pytype="float">300</f>
1632 <l py:pytype="long">2</l>
1633 <t py:pytype="TREE"></t>
1634 </a>
1635 '''))
1636 objectify.xsiannotate(root, ignore_old=True)
1637
1638 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1639 for c in root.iterchildren() ]
1640 self.assertEquals("xsd:integer", child_types[ 0])
1641 self.assertEquals("xsd:string", child_types[ 1])
1642 self.assertEquals("xsd:double", child_types[ 2])
1643 self.assertEquals("xsd:string", child_types[ 3])
1644 self.assertEquals("xsd:boolean", child_types[ 4])
1645 self.assertEquals(None, child_types[ 5])
1646 self.assertEquals(None, child_types[ 6])
1647 self.assertEquals("xsd:integer", child_types[ 7])
1648 self.assertEquals("xsd:integer", child_types[ 8])
1649 self.assertEquals("xsd:integer", child_types[ 9])
1650 self.assertEquals("xsd:string", child_types[10])
1651 self.assertEquals("xsd:double", child_types[11])
1652 self.assertEquals("xsd:integer", child_types[12])
1653 self.assertEquals(None, child_types[13])
1654
1655 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1656
1658 XML = self.XML
1659 root = XML(_bytes('''\
1660 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1661 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1662 <b>5</b>
1663 <b>test</b>
1664 <c>1.1</c>
1665 <c>\uF8D2</c>
1666 <x>true</x>
1667 <n xsi:nil="true" />
1668 <n></n>
1669 <b xsi:type="double">5</b>
1670 <b xsi:type="float">5</b>
1671 <s xsi:type="string">23</s>
1672 <s py:pytype="str">42</s>
1673 <f py:pytype="float">300</f>
1674 <l py:pytype="long">2</l>
1675 <t py:pytype="TREE"></t>
1676 </a>
1677 '''))
1678 objectify.deannotate(root)
1679
1680 for c in root.getiterator():
1681 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1682 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1683
1684 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1685
1687 XML = self.XML
1688 root = XML(_bytes('''\
1689 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1690 xmlns:py="http://codespeak.net/lxml/objectify/pytype">
1691 <b>5</b>
1692 <b>test</b>
1693 <c>1.1</c>
1694 <c>\uF8D2</c>
1695 <x>true</x>
1696 <n xsi:nil="true" />
1697 <n></n>
1698 <b xsi:type="double">5</b>
1699 <b xsi:type="float">5</b>
1700 <s xsi:type="string">23</s>
1701 <s py:pytype="str">42</s>
1702 <f py:pytype="float">300</f>
1703 <l py:pytype="long">2</l>
1704 <t py:pytype="TREE"></t>
1705 </a>
1706 '''))
1707 objectify.annotate(
1708 root, ignore_old=False, ignore_xsi=False, annotate_xsi=True,
1709 empty_pytype='str', empty_type='string')
1710 objectify.deannotate(root, pytype=False, xsi=False, xsi_nil=True)
1711
1712 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1713 for c in root.iterchildren() ]
1714 self.assertEquals("xsd:integer", child_types[ 0])
1715 self.assertEquals("xsd:string", child_types[ 1])
1716 self.assertEquals("xsd:double", child_types[ 2])
1717 self.assertEquals("xsd:string", child_types[ 3])
1718 self.assertEquals("xsd:boolean", child_types[ 4])
1719 self.assertEquals(None, child_types[ 5])
1720 self.assertEquals("xsd:string", child_types[ 6])
1721 self.assertEquals("xsd:double", child_types[ 7])
1722 self.assertEquals("xsd:float", child_types[ 8])
1723 self.assertEquals("xsd:string", child_types[ 9])
1724 self.assertEquals("xsd:string", child_types[10])
1725 self.assertEquals("xsd:double", child_types[11])
1726 self.assertEquals("xsd:integer", child_types[12])
1727 self.assertEquals(None, child_types[13])
1728
1729 self.assertEquals(None, root.n.get(XML_SCHEMA_NIL_ATTR))
1730
1731 for c in root.iterchildren():
1732 self.assertNotEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1733
1734 if (c.get(objectify.PYTYPE_ATTRIBUTE) not in [TREE_PYTYPE,
1735 "NoneType"]):
1736 self.assertNotEquals(
1737 None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1738
1740 XML = self.XML
1741 root = XML(_bytes('''\
1742 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1743 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1744 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1745 <b>5</b>
1746 <b>test</b>
1747 <c>1.1</c>
1748 <c>\uF8D2</c>
1749 <x>true</x>
1750 <n xsi:nil="true" />
1751 <n></n>
1752 <b xsi:type="xsd:double">5</b>
1753 <b xsi:type="xsd:float">5</b>
1754 <s xsi:type="xsd:string">23</s>
1755 <s py:pytype="str">42</s>
1756 <f py:pytype="float">300</f>
1757 <l py:pytype="long">2</l>
1758 <t py:pytype="TREE"></t>
1759 </a>
1760 '''))
1761 objectify.annotate(root)
1762 objectify.deannotate(root, pytype=False)
1763
1764 child_types = [ c.get(objectify.PYTYPE_ATTRIBUTE)
1765 for c in root.iterchildren() ]
1766 self.assertEquals("int", child_types[ 0])
1767 self.assertEquals("str", child_types[ 1])
1768 self.assertEquals("float", child_types[ 2])
1769 self.assertEquals("str", child_types[ 3])
1770 self.assertEquals("bool", child_types[ 4])
1771 self.assertEquals("NoneType", child_types[ 5])
1772 self.assertEquals(None, child_types[ 6])
1773 self.assertEquals("float", child_types[ 7])
1774 self.assertEquals("float", child_types[ 8])
1775 self.assertEquals("str", child_types[ 9])
1776 self.assertEquals("int", child_types[10])
1777 self.assertEquals("int", child_types[11])
1778 self.assertEquals("int", child_types[12])
1779 self.assertEquals(None, child_types[13])
1780
1781 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1782
1783 for c in root.getiterator():
1784 self.assertEquals(None, c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR))
1785
1787 XML = self.XML
1788 root = XML(_bytes('''\
1789 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1790 xmlns:py="http://codespeak.net/lxml/objectify/pytype"
1791 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
1792 <b xsi:type="xsd:int">5</b>
1793 <b xsi:type="xsd:string">test</b>
1794 <c xsi:type="xsd:float">1.1</c>
1795 <c xsi:type="xsd:string">\uF8D2</c>
1796 <x xsi:type="xsd:boolean">true</x>
1797 <n xsi:nil="true" />
1798 <n></n>
1799 <b xsi:type="xsd:double">5</b>
1800 <b xsi:type="xsd:float">5</b>
1801 <s xsi:type="xsd:string">23</s>
1802 <s xsi:type="xsd:string">42</s>
1803 <f xsi:type="xsd:float">300</f>
1804 <l xsi:type="xsd:long">2</l>
1805 <t py:pytype="TREE"></t>
1806 </a>
1807 '''))
1808 objectify.annotate(root)
1809 objectify.deannotate(root, xsi=False)
1810
1811 child_types = [ c.get(XML_SCHEMA_INSTANCE_TYPE_ATTR)
1812 for c in root.iterchildren() ]
1813 self.assertEquals("xsd:int", child_types[ 0])
1814 self.assertEquals("xsd:string", child_types[ 1])
1815 self.assertEquals("xsd:float", child_types[ 2])
1816 self.assertEquals("xsd:string", child_types[ 3])
1817 self.assertEquals("xsd:boolean", child_types[ 4])
1818 self.assertEquals(None, child_types[ 5])
1819 self.assertEquals(None, child_types[ 6])
1820 self.assertEquals("xsd:double", child_types[ 7])
1821 self.assertEquals("xsd:float", child_types[ 8])
1822 self.assertEquals("xsd:string", child_types[ 9])
1823 self.assertEquals("xsd:string", child_types[10])
1824 self.assertEquals("xsd:float", child_types[11])
1825 self.assertEquals("xsd:long", child_types[12])
1826 self.assertEquals(None, child_types[13])
1827
1828 self.assertEquals("true", root.n.get(XML_SCHEMA_NIL_ATTR))
1829
1830 for c in root.getiterator():
1831 self.assertEquals(None, c.get(objectify.PYTYPE_ATTRIBUTE))
1832
1834 XML = self.XML
1835
1836 xml = _bytes('''\
1837 <a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1838 <b>5</b>
1839 <b>test</b>
1840 <c>1.1</c>
1841 <c>\uF8D2</c>
1842 <x>true</x>
1843 <n xsi:nil="true" />
1844 <n></n>
1845 <b xsi:type="double">5</b>
1846 </a>
1847 ''')
1848
1849 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1850 objectify.set_pytype_attribute_tag("{TEST}test")
1851
1852 root = XML(xml)
1853 objectify.annotate(root)
1854
1855 attribs = root.xpath("//@py:%s" % pytype_name,
1856 namespaces={"py" : pytype_ns})
1857 self.assertEquals(0, len(attribs))
1858 attribs = root.xpath("//@py:test",
1859 namespaces={"py" : "TEST"})
1860 self.assertEquals(7, len(attribs))
1861
1862 objectify.set_pytype_attribute_tag()
1863 pytype_ns, pytype_name = objectify.PYTYPE_ATTRIBUTE[1:].split('}')
1864
1865 self.assertNotEqual("test", pytype_ns.lower())
1866 self.assertNotEqual("test", pytype_name.lower())
1867
1868 root = XML(xml)
1869 attribs = root.xpath("//@py:%s" % pytype_name,
1870 namespaces={"py" : pytype_ns})
1871 self.assertEquals(0, len(attribs))
1872
1873 objectify.annotate(root)
1874 attribs = root.xpath("//@py:%s" % pytype_name,
1875 namespaces={"py" : pytype_ns})
1876 self.assertEquals(7, len(attribs))
1877
1885
1886 def checkMyType(s):
1887 return True
1888
1889 pytype = objectify.PyType("mytype", checkMyType, NewType)
1890 self.assert_(pytype not in objectify.getRegisteredTypes())
1891 pytype.register()
1892 self.assert_(pytype in objectify.getRegisteredTypes())
1893 pytype.unregister()
1894 self.assert_(pytype not in objectify.getRegisteredTypes())
1895
1896 pytype.register(before = [objectify.getRegisteredTypes()[0].name])
1897 self.assertEquals(pytype, objectify.getRegisteredTypes()[0])
1898 pytype.unregister()
1899
1900 pytype.register(after = [objectify.getRegisteredTypes()[0].name])
1901 self.assertNotEqual(pytype, objectify.getRegisteredTypes()[0])
1902 pytype.unregister()
1903
1904 self.assertRaises(ValueError, pytype.register,
1905 before = [objectify.getRegisteredTypes()[0].name],
1906 after = [objectify.getRegisteredTypes()[1].name])
1907
1909 from datetime import datetime
1910 def parse_date(value):
1911 if len(value) != 14:
1912 raise ValueError(value)
1913 Y = int(value[0:4])
1914 M = int(value[4:6])
1915 D = int(value[6:8])
1916 h = int(value[8:10])
1917 m = int(value[10:12])
1918 s = int(value[12:14])
1919 return datetime(Y, M, D, h, m, s)
1920
1921 def stringify_date(date):
1922 return date.strftime("%Y%m%d%H%M%S")
1923
1924 class DatetimeElement(objectify.ObjectifiedDataElement):
1925 def pyval(self):
1926 return parse_date(self.text)
1927 pyval = property(pyval)
1928
1929 datetime_type = objectify.PyType(
1930 "datetime", parse_date, DatetimeElement, stringify_date)
1931 datetime_type.xmlSchemaTypes = "dateTime"
1932 datetime_type.register()
1933
1934 NAMESPACE = "http://foo.net/xmlns"
1935 NAMESPACE_MAP = {'ns': NAMESPACE}
1936
1937 r = objectify.Element("{%s}root" % NAMESPACE, nsmap=NAMESPACE_MAP)
1938 time = datetime.now()
1939 r.date = time
1940
1941 self.assert_(isinstance(r.date, DatetimeElement))
1942 self.assert_(isinstance(r.date.pyval, datetime))
1943
1944 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1945 self.assertEquals(r.date.text, stringify_date(time))
1946
1947 r.date = objectify.E.date(time)
1948
1949 self.assert_(isinstance(r.date, DatetimeElement))
1950 self.assert_(isinstance(r.date.pyval, datetime))
1951
1952 self.assertEquals(r.date.pyval, parse_date(stringify_date(time)))
1953 self.assertEquals(r.date.text, stringify_date(time))
1954
1960
1966
1971
1980
1987
1995
1998
2001
2020
2025
2030
2035
2040
2060
2062 root = self.XML(xml_str)
2063 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[0]'] )
2064 self.assertEquals(root.c1.c2.text, path(root).text)
2065
2066 path = objectify.ObjectPath( ['root', 'c1[0]', 'c2[2]'] )
2067 self.assertEquals(root.c1.c2[2].text, path(root).text)
2068
2069 path = objectify.ObjectPath( ['root', 'c1', 'c2[2]'] )
2070 self.assertEquals(root.c1.c2[2].text, path(root).text)
2071
2072 path = objectify.ObjectPath( ['root', 'c1', 'c2[-1]'] )
2073 self.assertEquals(root.c1.c2[-1].text, path(root).text)
2074
2075 path = objectify.ObjectPath( ['root', 'c1', 'c2[-3]'] )
2076 self.assertEquals(root.c1.c2[-3].text, path(root).text)
2077
2079 self.assertRaises(ValueError, objectify.ObjectPath,
2080 "root.c1[0].c2[-1-2]")
2081 self.assertRaises(ValueError, objectify.ObjectPath,
2082 ['root', 'c1[0]', 'c2[-1-2]'])
2083
2084 self.assertRaises(ValueError, objectify.ObjectPath,
2085 "root[2].c1.c2")
2086 self.assertRaises(ValueError, objectify.ObjectPath,
2087 ['root[2]', 'c1', 'c2'])
2088
2089 self.assertRaises(ValueError, objectify.ObjectPath,
2090 [])
2091 self.assertRaises(ValueError, objectify.ObjectPath,
2092 ['', '', ''])
2093
2095 root = self.XML(xml_str)
2096 path = objectify.ObjectPath("root.c1[9999].c2")
2097 self.assertRaises(AttributeError, path, root)
2098
2099 path = objectify.ObjectPath("root.c1[0].c2[9999]")
2100 self.assertRaises(AttributeError, path, root)
2101
2102 path = objectify.ObjectPath(".c1[9999].c2[0]")
2103 self.assertRaises(AttributeError, path, root)
2104
2105 path = objectify.ObjectPath("root.c1[-2].c2")
2106 self.assertRaises(AttributeError, path, root)
2107
2108 path = objectify.ObjectPath("root.c1[0].c2[-4]")
2109 self.assertRaises(AttributeError, path, root)
2110
2124
2126 root = self.XML(xml_str)
2127 path = objectify.ObjectPath( ['{objectified}root', 'c1', 'c2'] )
2128 self.assertEquals(root.c1.c2.text, path.find(root).text)
2129 path = objectify.ObjectPath( ['{objectified}root', '{objectified}c1', 'c2'] )
2130 self.assertEquals(root.c1.c2.text, path.find(root).text)
2131 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2'] )
2132 self.assertEquals(root.c1.c2.text, path.find(root).text)
2133 path = objectify.ObjectPath( ['root', '{objectified}c1', '{objectified}c2[2]'] )
2134 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2135 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2'] )
2136 self.assertEquals(root.c1.c2.text, path.find(root).text)
2137 path = objectify.ObjectPath( ['root', 'c1', '{objectified}c2[2]'] )
2138 self.assertEquals(root.c1.c2[2].text, path.find(root).text)
2139 path = objectify.ObjectPath( ['root', 'c1', '{otherNS}c2'] )
2140 self.assertEquals(getattr(root.c1, '{otherNS}c2').text,
2141 path.find(root).text)
2142
2155
2170
2182
2196
2198 root = self.XML(xml_str)
2199 path = objectify.ObjectPath( "root.c1.c99" )
2200 self.assertRaises(AttributeError, path.find, root)
2201
2202 new_el = self.Element("{objectified}test")
2203 new_el.a = ["TEST1", "TEST2"]
2204 new_el.a[0].set("myattr", "ATTR1")
2205 new_el.a[1].set("myattr", "ATTR2")
2206
2207 path.setattr(root, list(new_el.a))
2208
2209 self.assertEquals(2, len(root.c1.c99))
2210 self.assertEquals("ATTR1", root.c1.c99[0].get("myattr"))
2211 self.assertEquals("TEST1", root.c1.c99[0].text)
2212 self.assertEquals("ATTR2", root.c1.c99[1].get("myattr"))
2213 self.assertEquals("TEST2", root.c1.c99[1].text)
2214 self.assertEquals("TEST1", path(root).text)
2215
2224
2238
2250
2264
2279
2281 root = self.XML(xml_str)
2282 self.assertEquals(
2283 ['{objectified}root', '{objectified}root.c1',
2284 '{objectified}root.c1.c2',
2285 '{objectified}root.c1.c2[1]', '{objectified}root.c1.c2[2]',
2286 '{objectified}root.c1.{otherNS}c2', '{objectified}root.c1.{}c2'],
2287 root.descendantpaths())
2288
2290 root = self.XML(xml_str)
2291 self.assertEquals(
2292 ['{objectified}c1', '{objectified}c1.c2',
2293 '{objectified}c1.c2[1]', '{objectified}c1.c2[2]',
2294 '{objectified}c1.{otherNS}c2', '{objectified}c1.{}c2'],
2295 root.c1.descendantpaths())
2296
2298 root = self.XML(xml_str)
2299 self.assertEquals(
2300 ['root.{objectified}c1', 'root.{objectified}c1.c2',
2301 'root.{objectified}c1.c2[1]', 'root.{objectified}c1.c2[2]',
2302 'root.{objectified}c1.{otherNS}c2',
2303 'root.{objectified}c1.{}c2'],
2304 root.c1.descendantpaths('root'))
2305
2317
2330
2331
2332
2337
2342
2347
2352
2357
2362
2367
2372
2374 E = objectify.E
2375 DataElement = objectify.DataElement
2376 root = E.root("text", E.sub(E.subsub()), "tail", DataElement(1),
2377 DataElement(2.0))
2378 self.assert_(isinstance(root, objectify.ObjectifiedElement))
2379 self.assertEquals(root.text, "text")
2380 self.assert_(isinstance(root.sub, objectify.ObjectifiedElement))
2381 self.assertEquals(root.sub.tail, "tail")
2382 self.assert_(isinstance(root.sub.subsub, objectify.StringElement))
2383 self.assertEquals(len(root.value), 2)
2384 self.assert_(isinstance(root.value[0], objectify.IntElement))
2385 self.assert_(isinstance(root.value[1], objectify.FloatElement))
2386
2393
2394 attr = Attribute()
2395 self.assertEquals(attr.text, None)
2396 self.assertEquals(attr.get("datatype"), "TYPE")
2397 self.assertEquals(attr.get("range"), "0.,1.")
2398
2403
2410
2415
2421
2423 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2424 self.assertEquals(root.base, "http://no/such/url")
2425 self.assertEquals(
2426 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2427 root.base = "https://secret/url"
2428 self.assertEquals(root.base, "https://secret/url")
2429 self.assertEquals(
2430 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2431 "https://secret/url")
2432
2434 root = objectify.XML(_bytes("<root/>"), base_url="http://no/such/url")
2435 self.assertEquals(root.base, "http://no/such/url")
2436 self.assertEquals(
2437 root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2438 root.set('{http://www.w3.org/XML/1998/namespace}base',
2439 "https://secret/url")
2440 self.assertEquals(root.base, "https://secret/url")
2441 self.assertEquals(
2442 root.get('{http://www.w3.org/XML/1998/namespace}base'),
2443 "https://secret/url")
2444
2446 XML = self.XML
2447
2448 xml = _bytes('''\
2449 <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2450 <i>5</i>
2451 <i>-5</i>
2452 <l>4294967296</l>
2453 <l>-4294967296</l>
2454 <f>1.1</f>
2455 <b>true</b>
2456 <b>false</b>
2457 <s>Strange things happen, where strings collide</s>
2458 <s>True</s>
2459 <s>False</s>
2460 <s>t</s>
2461 <s>f</s>
2462 <s></s>
2463 <s>None</s>
2464 <n xsi:nil="true" />
2465 </root>
2466 ''')
2467 root = XML(xml)
2468
2469 for i in root.i:
2470 self.assert_(isinstance(i, objectify.IntElement))
2471 for l in root.l:
2472 self.assert_(isinstance(l, objectify.IntElement))
2473 for f in root.f:
2474 self.assert_(isinstance(f, objectify.FloatElement))
2475 for b in root.b:
2476 self.assert_(isinstance(b, objectify.BoolElement))
2477 self.assertEquals(True, root.b[0])
2478 self.assertEquals(False, root.b[1])
2479 for s in root.s:
2480 self.assert_(isinstance(s, objectify.StringElement))
2481 self.assert_(isinstance(root.n, objectify.NoneElement))
2482 self.assertEquals(None, root.n)
2483
2485 suite = unittest.TestSuite()
2486 suite.addTests([unittest.makeSuite(ObjectifyTestCase)])
2487 suite.addTests(doctest.DocTestSuite(objectify))
2488 if sys.version_info >= (2,4):
2489 suite.addTests(
2490 [make_doctest('../../../doc/objectify.txt')])
2491 return suite
2492
2493 if __name__ == '__main__':
2494 print('to test use test.py %s' % __file__)
2495