Commit b2cc3d04 authored by Luke Mewburn's avatar Luke Mewburn
Browse files

103707: fix validate_examples.py

Update validate_examples.py to load all the schemas now required,
using modern XMLSchema idioms.

Modernise to at least python 3.11

Fix various ruff issues.
parent 7a404cd0
Loading
Loading
Loading
Loading
+x+23 −29
Changes for 103707/testing/validate_examples.py: 23 added lines, 29 removed lines.
Original line number Diff line number Diff line
import glob
import sys
from pathlib import Path
from pprint import pprint
#!/usr/bin/env python3

if __name__ == "__main__":
    if sys.version_info <= (3, 5):
        sys.exit("ERROR: You need at least Python 3.5 to run this tool")

    try:
        from lxml import etree
    except ImportError:
        sys.exit("ERROR: You need to install the Python lxml library")
import pathlib

    try:
import xmlschema
    except ImportError:
        sys.exit("ERROR: You need to install the xml schema library")

    extraSchemas = ["examples/FooServiceSchema.xsd", "TS_103_280_v020301.xsd"]

    locations = []
    for schemaFile in extraSchemas:
        xs = xmlschema.XMLSchema(schemaFile, validation="skip")
        locations.append((xs.default_namespace, str(Path(schemaFile))))
def main() -> None:
    # ruff: disable[E501] (line-too-long)
    extra_locations = {
        "http://uri.etsi.org/03120/common/2016/02/Common": "../103120/schema/xsd/ts_103120_Common.xsd",
        "http://uri.etsi.org/03120/common/2019/10/Core": "../103120/schema/xsd/ts_103120_Core.xsd",
        "http://uri.etsi.org/03120/common/2020/09/Task": "../103120/schema/xsd/ts_103120_Task.xsd",
        "http://uri.etsi.org/03120/common/2024/06/Config": "../103120/schema/xsd/ts_103120_Config.xsd",
        "http://uri.etsi.org/03280/common/2017/07": "../103280/TS_103_280.xsd",
        "http://uri.etsi.org/03280/common/2019/10/Dictionaries": "../103120/dictionaries/ts_103120_Dictionaries.xsd",
        "http://FooServiceSchema.example.com/schema/v1.1.1/": "examples/FooServiceSchema.xsd",
    }
    # ruff: enable[E501] (line-too-long)

    coreSchema = xmlschema.XMLSchema("TS_103_707_v010201.xsd", locations=locations)
    schema = xmlschema.XMLSchema("TS_103_707.xsd", locations=extra_locations)

    for schema in extraSchemas:
        newSchema = xmlschema.XMLSchema(schema)
        coreSchema.import_schema(newSchema.default_namespace, schema)

    examples = glob.glob("examples/*.xml")
    examples = pathlib.Path("examples").glob("*.xml")
    for example in examples:
        try:
            coreSchema.validate(example)
            print("{0} passed validation".format(example))
            schema.validate(example)
            print(f"{example} passed validation")
        except Exception as ex:
            print("{0} failed validation: {1}".format(example, ex))
            print(f"{example} failed validation: {ex}")

    print("Done")


if __name__ == "__main__":
    main()