Exemplo n.º 1
0
 def test_next_node_return_self_on_root_element(self):
     ele = Element("foo")
     ctx = XmlContext()
     cfg = ParserConfig()
     meta = ctx.build(Foo)
     node = RootNode(position=0, meta=meta, config=cfg)
     self.assertIs(node, node.next_node(ele, 0, ctx))
Exemplo n.º 2
0
    def test_next_node_return_next_node(self):
        root = Element("a")
        ele = SubElement(root, "b")

        ctx = XmlContext()
        cfg = ParserConfig()
        meta = ctx.build(Foo)
        node = RootNode(position=0, meta=meta, config=cfg)
        actual = node.next_node(ele, 0, ctx)

        self.assertIsInstance(actual, PrimitiveNode)
        self.assertIsNot(actual, node)
Exemplo n.º 3
0
    def test_queue(self, mock_emit_event, mock_next_node):
        primitive_node = PrimitiveNode(position=1, types=[int])
        mock_next_node.return_value = primitive_node
        element = Element("{urn:books}books")
        config = ParserConfig()
        root_queue_item = RootNode(
            position=0,
            meta=self.parser.context.build(Books),
            default=None,
            config=config,
        )

        objects = list()
        queue = list()
        queue.append(root_queue_item)
        self.parser.queue(element, queue, objects)

        self.assertEqual(2, len(queue))
        self.assertEqual(root_queue_item, queue[0])
        self.assertEqual(primitive_node, queue[1])

        mock_emit_event.assert_called_once_with(EventType.START,
                                                element.tag,
                                                item=root_queue_item,
                                                element=element)
Exemplo n.º 4
0
    def test_queue(self, mock_emit_event, mock_next_node):
        var = XmlText(name="foo", qname=QName("foo"))
        primitive_node = PrimitiveNode(position=1, var=var)
        mock_next_node.return_value = primitive_node
        element = Element("{urn:books}books")
        config = ParserConfig()
        root_queue_item = RootNode(
            position=0,
            meta=self.parser.context.build(Books),
            config=config,
        )

        objects = []
        queue = []
        queue.append(root_queue_item)
        self.parser.queue(element, queue, objects)

        self.assertEqual(2, len(queue))
        self.assertEqual(root_queue_item, queue[0])
        self.assertEqual(primitive_node, queue[1])

        mock_emit_event.assert_called_once_with(EventType.START,
                                                element.tag,
                                                item=root_queue_item,
                                                element=element)
Exemplo n.º 5
0
    def parse_context(self, context: iterparse, clazz: Type[T]) -> T:
        """
        Dispatch elements to handlers as they arrive and are fully parsed.

        :raises ParserError: When the requested type doesn't match the result object
        """
        obj = None
        meta = self.context.build(clazz)
        self.namespaces.clear()
        objects: ParsedObjects = []
        queue: XmlNodes = [RootNode(position=0, meta=meta, config=self.config)]

        for event, element in context:
            if event == EventType.START_NS:
                self.add_namespace(element)
            if event == EventType.START:
                self.queue(element, queue, objects)
            elif event == EventType.END:
                obj = self.dequeue(element, queue, objects)

        if not obj:
            raise ParserError(
                f"Failed to create target class `{clazz.__name__}`")

        return obj