Ejemplo n.º 1
0
 def read(cls, path: Path) -> "GeneratorConfig":
     ctx = XmlContext(
         element_name_generator=text.pascal_case,
         attribute_name_generator=text.camel_case,
     )
     config = ParserConfig(fail_on_unknown_properties=False)
     parser = XmlParser(context=ctx, config=config)
     return parser.from_path(path, cls)
Ejemplo n.º 2
0
 def setUp(self) -> None:
     super().setUp()
     self.context = XmlContext()
     self.meta = XmlMeta(clazz=Foo, qname="foo", source_qname="foo", nillable=False)
     self.node = ElementNode(
         position=0,
         meta=self.meta,
         context=self.context,
         config=ParserConfig(),
         attrs={},
         ns_map={},
     )
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def test_next_node_when_config_fail_on_unknown_properties_is_false(self):
     ele = Element("nope")
     ctx = XmlContext()
     cfg = ParserConfig(fail_on_unknown_properties=False)
     meta = XmlMeta(
         name="foo",
         clazz=None,
         qname=QName("foo"),
         source_qname=QName("foo"),
         nillable=False,
     )
     node = ElementNode(position=0, meta=meta, config=cfg)
     actual = node.next_node(ele, 10, ctx)
     self.assertEqual(SkipNode(position=10), actual)
Ejemplo n.º 5
0
    def test_bind_with_derived_element(self):
        a = make_dataclass("A", fields=[])
        node = ElementNode(
            position=0,
            meta=self.context.build(a),
            context=self.context,
            config=ParserConfig(),
            attrs={},
            ns_map={},
            derived=True,
        )

        objects = []

        self.assertTrue(node.bind("foo", None, None, objects))
        self.assertEqual("foo", objects[-1][0])
        self.assertEqual(DerivedElement("foo", a()), objects[-1][1])
Ejemplo n.º 6
0
    def test_parse_element(
        self,
        mock_bind_element_attrs,
        mock_bind_element_text,
        mock_bind_element_children,
        mock_bind_element_wild_text,
    ):
        def add_attr(x, *args):
            x["a"] = 1

        def add_text(x, *args):
            x["b"] = 2

        def add_child(x, *args):
            x["c"] = 3

        def add_wild_text(x, *args):
            x["d"] = 4

        mock_bind_element_attrs.side_effect = add_attr
        mock_bind_element_text.side_effect = add_text
        mock_bind_element_children.side_effect = add_child
        mock_bind_element_wild_text.side_effect = add_wild_text

        ctx = XmlContext()
        meta = ctx.build(Foo)

        ele = Element("foo")
        pool = [1, 2, 3]

        node = ElementNode(position=0,
                           meta=meta,
                           default=None,
                           config=ParserConfig())
        qname, obj = node.parse_element(ele, pool)

        self.assertEqual(QName(ele.tag), qname)
        self.assertEqual(Foo(1, 2, 3, 4), obj)

        mock_bind_element_attrs.assert_called_once_with(mock.ANY, meta, ele)
        mock_bind_element_text.assert_called_once_with(mock.ANY, meta, ele)
        mock_bind_element_children.assert_called_once_with(
            mock.ANY, meta, 0, pool)
        mock_bind_element_wild_text.assert_called_once_with(
            mock.ANY, meta, ele)
Ejemplo n.º 7
0
    def test_next_node_when_given_qname_does_not_match_any_var(self):
        ele = Element("nope")
        ctx = XmlContext()
        cfg = ParserConfig()
        meta = XmlMeta(
            name="foo",
            clazz=None,
            qname=QName("foo"),
            source_qname=QName("foo"),
            nillable=False,
        )
        node = ElementNode(position=0, meta=meta, config=cfg)

        with self.assertRaises(XmlContextError) as cm:
            node.next_node(ele, 10, ctx)

        self.assertEqual("foo does not support mixed content: nope",
                         str(cm.exception))
Ejemplo n.º 8
0
    def test_next_node_when_given_qname_matches_primitive_var(self):
        ele = Element("a")
        ctx = XmlContext()
        cfg = ParserConfig()
        var = XmlText(name="a", qname=QName("a"), types=[int], default=100)
        meta = XmlMeta(
            name="foo",
            clazz=None,
            qname=QName("foo"),
            source_qname=QName("foo"),
            nillable=False,
            vars=[var],
        )
        node = ElementNode(position=0, meta=meta, config=cfg)

        actual = node.next_node(ele, 10, ctx)
        self.assertIsInstance(actual, PrimitiveNode)
        self.assertEqual(10, actual.position)
        self.assertEqual(var, actual.var)
Ejemplo n.º 9
0
    def test_next_node_when_given_qname_matches_any_element_var(self):
        ele = Element("a")
        ctx = XmlContext()
        cfg = ParserConfig()
        var = XmlWildcard(name="a",
                          qname=QName("a"),
                          types=[],
                          dataclass=False)
        meta = XmlMeta(
            name="foo",
            clazz=None,
            qname=QName("foo"),
            source_qname=QName("foo"),
            nillable=False,
            vars=[var],
        )
        node = ElementNode(position=0, meta=meta, config=cfg)

        actual = node.next_node(ele, 10, ctx)
        self.assertIsInstance(actual, WildcardNode)
        self.assertEqual(10, actual.position)
        self.assertEqual(var.qname, actual.qname)
Ejemplo n.º 10
0
    def test_bind_with_wildcard_var(
        self,
        mock_bind_attrs,
        mock_bind_content,
        mock_bind_wild_content,
        mock_bind_objects,
        mock_find_var,
    ):
        mock_bind_attrs.side_effect = add_attr
        mock_bind_content.return_value = False
        mock_bind_wild_content.side_effect = add_text
        mock_bind_objects.side_effect = add_child
        mock_find_var.return_value = XmlVar(wildcard=True, qname="b", name="b")

        node = ElementNode(
            position=0,
            meta=self.context.build(Foo),
            context=self.context,
            config=ParserConfig(),
            attrs={"a": "b"},
            ns_map={"ns0": "xsdata"},
        )

        objects = [1, 2, 3]

        self.assertTrue(node.bind("foo", "text", "tail", objects))
        self.assertEqual("foo", objects[-1][0])
        self.assertEqual(Foo(1, 2, 3), objects[-1][1])

        mock_bind_attrs.assert_called_once_with(
            mock.ANY, node.meta, node.attrs, node.ns_map
        )
        mock_bind_content.assert_called_once_with(
            mock.ANY, node.meta, "text", node.ns_map
        )
        mock_bind_objects.assert_called_once_with(mock.ANY, node.meta, 0, objects)
Ejemplo n.º 11
0
    def test_parse_with_fail_on_unknown_properties_false(self):
        xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
               "<books>\n"
               '  <book id="bk001">\n'
               "    <author>Hightower, Kim</author>\n"
               "    <title>The First Book</title>\n"
               "  </book>\n"
               '  <book id="bk002">\n'
               "    <author>Nagata, Suanne</author>\n"
               "    <title>Becoming Somebody</title>\n"
               "  </book>\n"
               "</books>\n")

        @dataclass
        class Book:
            author: str = field(metadata=dict(type="Element"))

        @dataclass
        class MyBooks:
            class Meta:
                name = "books"

            book: List[Book] = field(default_factory=list,
                                     metadata=dict(type="Element"))

        config = ParserConfig(fail_on_unknown_properties=False)
        parser = XmlParser(config=config)
        actual = parser.from_string(xml, MyBooks)
        expected = {
            "book": [{
                "author": "Hightower, Kim"
            }, {
                "author": "Nagata, Suanne"
            }]
        }
        self.assertEqual(expected, asdict(actual))
Ejemplo n.º 12
0
def timing(description: str) -> Any:
    start = time.time()
    yield
    ellapsed_time = time.time() - start

    print(f"{description}: {ellapsed_time}")


with timing("importing module"):
    from netex.models import *

sample = str(Path(__file__).parent.joinpath("NeTEx_HTM__2020-10-12.tar.xz"))
with timing("decompress sample"):
    subprocess.run(["tar", "-xf", sample, "-C", "/tmp"])

sample = "/tmp/NeTEx_HTM__2020-10-12.xml"
context = XmlContext()
config = ParserConfig(fail_on_unknown_properties=False)

with timing("XmlContext warmup"):
    context.build_recursive(PublicationDelivery)
    print(f"Context cache size: {len(context.cache)}")

parser = XmlParser(context=context, config=config, handler=LxmlEventHandler)
with timing("Parse[LxmlEventHandler]"):
    parser.parse(sample, PublicationDelivery)

parser = XmlParser(context=context, config=config, handler=XmlEventHandler)
with timing("Parse[EventHandler]"):
    parser.parse(sample, PublicationDelivery)
Ejemplo n.º 13
0
 def __init_parser(self):
     """
     Inits the internal parser.
     """
     config = ParserConfig(fail_on_unknown_properties=True)
     self.parser = XmlParser(config=config)
Ejemplo n.º 14
0
 def test_parse_from_memory_with_process_xinclude_true(self):
     path = fixtures_dir.joinpath("books/books-xinclude.xml")
     config = ParserConfig(process_xinclude=True, base_url=path.as_uri())
     parser = XmlParser(config=config)
     actual = parser.from_bytes(path.read_bytes(), Books)
     self.assertEqual(self.books, actual)
Ejemplo n.º 15
0
 def test_parse_with_process_xinclude_true(self):
     path = fixtures_dir.joinpath("books/books-xinclude.xml")
     config = ParserConfig(process_xinclude=True)
     parser = XmlParser(config=config)
     actual = parser.from_path(path, Books)
     self.assertEqual(self.books, actual)
Ejemplo n.º 16
0
def CreateFromDocument(document):
    parser = XmlParser(config=ParserConfig(fail_on_unknown_properties=True))
    if isinstance(document, str):
        return parser.from_string(document, ismrmrdHeader)
    return parser.from_bytes(document, ismrmrdHeader)
Ejemplo n.º 17
0
import requests
import traceback

from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
config = ParserConfig(fail_on_unknown_properties=True)
parser = XmlParser(config=config)

from lxml import etree
from lxml import objectify
xmlschema_doc = etree.parse('btlx_11.xsd')
xmlschema = etree.XMLSchema(xmlschema_doc)

from btlx.btlx_11 import Btlx

fname = 'btlx_filelist.txt'

with open(fname, 'r') as fd:
    for url in fd.readlines():
        try:
            if url.startswith('#'):
                continue
            
            url = url.strip()

            r = requests.get(url, allow_redirects=True)

            # Upgrade to BTLx 1.1
            btlx_str = r.text.replace('Version="1.0"', 'Version="1.1"')

            # Validate BTLx against XSD
Ejemplo n.º 18
0
def loads(
    sdf: str,
    *,
    version: str = None,
    custom_constructor: Dict[Type[T], Callable] = None,
    handler: str = None,
):
    """Convert an XML string into a sdformat.models tree.

    Parameters
    ----------
    sdf : str
        The SDFormat XML to be parsed.
    version : str
        The SDFormat version to use while parsing. If None (default) it will
        automatically determine the version from the <sdf> element. If specified
        the given version will be used instead.
    custom_constructor : Dict[Type[T], Callable]
        Overwrite the default constructor for a certain model class with
        callable. This is useful for doing pre- or post-initialization of
        bound classes or to replace them entirely.
    handler : str
        The handler that the parser should use when traversing the XML. If
        unspecified the default xsData parser will be used (lxml if it is
        installed, otherwise xml.etree). Possible values are:

            "XmlEventHandler"
                A xml.etree event-based handler.
            "LxmlEventHandler"
                A lxml.etree event-based handler.

    Returns
    -------
    SdfRoot : object
        An instance of ``skbot.ignition.models.vXX.Sdf`` where XX corresponds to the
        version of the SDFormat XML.

    Notes
    -----
    ``custom_constructure`` is currently disabled and has no effect. It will
    become available with xsData v21.8.

    Examples
    --------
    .. minigallery:: skbot.ignition.sdformat.loads

    """

    if custom_constructor is None:
        custom_constructor = dict()

    def custom_class_factory(clazz, params):
        if clazz in custom_constructor:
            return custom_constructor[clazz](**params)

        return clazz(**params)

    if version is None:
        version = get_version(sdf)

    if handler in ["XmlSaxHandler", "LxmlSaxHandler"]:
        warnings.warn(
            "SAX handlers have been deprecated in xsData >= 21.9;"
            " falling back to EventHandler. If you need the SAX handler, please open an issue."
            " To make this warning dissapear change `handler` to the corresponding EventHandler.",
            DeprecationWarning,
        )

    if handler == "XmlSaxHandler":
        handler = "XmlEventHandler"
    elif handler == "LxmlSaxHandler":
        handler = "LxmlEventHandler"

    handler_class = {
        None: handlers.default_handler(),
        "XmlEventHandler": handlers.XmlEventHandler,
        "LxmlEventHandler": handlers.LxmlEventHandler,
    }[handler]

    binding_location = _parser_roots[version]

    bindings = importlib.import_module(binding_location, __name__)

    sdf_parser = XmlParser(
        ParserConfig(class_factory=custom_class_factory),
        context=xml_ctx,
        handler=handler_class,
    )

    try:
        root_el = sdf_parser.from_string(sdf, bindings.Sdf)
    except XSDataParserError as e:
        raise ParseError("Invalid SDFormat XML.") from e

    return root_el