def test_custom_lookup_ns_fallback(self):
        class TestElement1(etree.ElementBase):
            FIND_ME = "custom"

        class TestElement2(etree.ElementBase):
            FIND_ME = "nsclasses"

        class MyLookup(etree.CustomElementClassLookup):
            def lookup(self, t, d, ns, name):
                if name == 'c1':
                    return TestElement1

        lookup = etree.ElementNamespaceClassLookup( MyLookup() )
        etree.set_element_class_lookup(lookup)

        ns = lookup.get_namespace("otherNS")
        ns[None] = TestElement2

        root = etree.XML(xml_str)
        self.assertFalse(hasattr(root, 'FIND_ME'))
        self.assertEquals(root[0].FIND_ME,
                          TestElement1.FIND_ME)
        self.assertFalse(hasattr(root[0][1], 'FIND_ME'))
        self.assertEquals(root[0][-1].FIND_ME,
                          TestElement2.FIND_ME)
Ejemplo n.º 2
0
 def setUp(self):
     super(ETreeNamespaceClassesTestCase, self).setUp()
     lookup = etree.ElementNamespaceClassLookup()
     self.Namespace = lookup.get_namespace
     parser = etree.XMLParser()
     parser.set_element_class_lookup(lookup)
     etree.set_default_parser(parser)
Ejemplo n.º 3
0
    def test_namespace_lookup(self):
        class TestElement(etree.ElementBase):
            FIND_ME = "namespace class"

        lookup = etree.ElementNamespaceClassLookup()
        etree.set_element_class_lookup(lookup)

        ns = lookup.get_namespace("myNS")
        ns[None] = TestElement

        root = etree.XML(xml_str)
        self.assertEqual(root.FIND_ME, TestElement.FIND_ME)
        self.assertEqual(root[0].FIND_ME, TestElement.FIND_ME)
        self.assertFalse(hasattr(root[0][-1], 'FIND_ME'))