Beispiel #1
0
    def test_render_with_provided_namespaces(self):
        serializer = XmlSerializer(pretty_print=True)
        namespaces = Namespaces()
        namespaces.add("urn:books", "burn")
        actual = serializer.render(self.books, namespaces)

        expected = (
            "<?xml version='1.0' encoding='UTF-8'?>\n"
            '<burn:books xmlns:burn="urn:books">\n'
            '  <book id="bk001" lang="en">\n'
            "    <author>Hightower, Kim</author>\n"
            "    <title>The First Book</title>\n"
            "    <genre>Fiction</genre>\n"
            "    <price>44.95</price>\n"
            "    <pub_date>2000-10-01</pub_date>\n"
            "    <review>An amazing story of nothing.</review>\n"
            "  </book>\n"
            '  <book id="bk002" lang="en">\n'
            "    <author>Nagata, Suanne</author>\n"
            "    <title>Becoming Somebody</title>\n"
            "    <genre>Biography</genre>\n"
            "    <review>A masterpiece of the fine art of gossiping.</review>\n"
            "  </book>\n"
            "</burn:books>\n")
        self.assertEqual(expected, actual)
Beispiel #2
0
    def test_register(self):
        namespaces = Namespaces()
        namespaces.add("http://komposta.net", "bar")
        namespaces.add("http://foobar", "ns2")  # ns{\d} are not registered

        element = Element("{http://komposta.net}root")
        self.assertEqual({"ns0": "http://komposta.net"}, element.nsmap)

        namespaces.register()
        element = Element("{http://komposta.net}root")
        self.assertEqual({"bar": "http://komposta.net"}, element.nsmap)

        element = Element("{http://foobar}root")
        self.assertEqual({"ns0": "http://foobar"}, element.nsmap)
Beispiel #3
0
    def test_property_ns_map(self):
        namespaces = Namespaces()
        namespaces.add_all({
            "b": "bar",
            None: "http://www.w3.org/2001/XMLSchema",
            "foo": "http://www.w3.org/2001/XMLSchema-instance",
        })
        namespaces.add("bar", "again")
        namespaces.add("one")
        namespaces.add("two")

        expected = {
            "b": "bar",
            "again": "bar",
            "ns0": "one",
            "ns1": "two",
            "xs": "http://www.w3.org/2001/XMLSchema",
            "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        }
        self.assertEqual(expected, namespaces.ns_map)
Beispiel #4
0
    def render_node(self, obj, parent, namespaces: Namespaces) -> Element:
        """Recursively traverse the given dataclass instance fields and build
        the lxml Element structure."""
        if not is_dataclass(obj):
            self.set_text(parent, obj)
            return parent

        meta = self.class_meta(obj.__class__, QName(parent).namespace)
        for var, value in self.next_value(meta, obj):
            if value is not None:
                if not var.is_any_element and not var.is_any_attribute:
                    namespaces.add(var.namespace)

                if var.is_attribute:
                    self.set_attribute(parent, var.qname, value)
                elif var.is_any_attribute:
                    self.set_attributes(parent, value)
                elif var.is_text:
                    self.set_text(parent, value)
                else:
                    self.render_sub_nodes(parent, value, var, namespaces)

        self.set_nil_attribute(parent, meta.nillable, namespaces)
        return parent
Beispiel #5
0
    def test_add(self):
        namespaces = Namespaces()
        namespaces.add("foo")
        namespaces.add("foo")
        namespaces.add("bar", "one")
        namespaces.add("bar", "two")
        namespaces.add(Namespace.XSI.uri, "a")
        namespaces.add(Namespace.XSI.uri, "b")
        namespaces.add(Namespace.XS.uri, "c")
        namespaces.add(Namespace.XS.uri, "d")

        expected = {
            "bar": {"one", "two"},
            "foo": {"ns0"},
            "http://www.w3.org/2001/XMLSchema": {"xs"},
            "http://www.w3.org/2001/XMLSchema-instance": {"xsi"},
        }
        self.assertEqual(expected, namespaces.items)
Beispiel #6
0
 def set_nil_attribute(
     cls, element: Element, nillable: bool, namespaces: Namespaces
 ):
     if nillable and element.text is None and len(element) == 0:
         namespaces.add(Namespace.XSI.uri, Namespace.XSI.prefix)
         element.set(QNames.XSI_NIL, "true")