Commit 3c125252 authored by Steije van Schelt's avatar Steije van Schelt Committed by Mark Canterbury
Browse files

Check for untyped elements

parent 0b44d8e5
Loading
Loading
Loading
Loading
+49 −9
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ from colorama import Fore, Style

import argparse

from lxml import etree

colorama.init()

ignore_paths = [Path(x) for x in ["testing/deps"]]
@@ -16,6 +18,15 @@ ignore_files = [str(Path("104000/schema/TS_104_000.xsd")),
                str(Path("103221-2/TS_103_221_02_Configuration.xsd")),
                str(Path("103707/TS_103_707.xsd"))]

XS = "{http://www.w3.org/2001/XMLSchema}"
etree_parser = etree.XMLParser(
    resolve_entities=False,
    no_network=True,
    dtd_validation=False,
    load_dtd=False,
    huge_tree=False,
)


def print_colorized_diff_line(line: str):
    if line.startswith("-"):
@@ -35,23 +46,52 @@ def run_linter(file: Path) -> str:
    linted_xml = completed.stdout
    return linted_xml

def get_linting_errors(file: Path) -> str | None:
def get_errors(file: Path) -> str:
    errors = get_linting_errors(file)
    errors += get_type_errors(file)

    return errors

def get_linting_errors(file: Path) -> str:
    linted_xml = run_linter(file)
    orig_xml = file.read_text(encoding="utf8")
    diff = list(unified_diff(orig_xml.splitlines(), linted_xml.splitlines()))
    if len(diff) == 0:
        return None
        return ""
    else:
        return "\n".join([print_colorized_diff_line(d) for d in diff])

def get_type_errors(file: Path) -> str:
    tree = etree.parse(file, parser=etree_parser)
    root = tree.getroot()

    errors = []

    for el in root.iter(f"{XS}element"):
        has_ref = el.get("ref") is not None
        has_type_attr = el.get("type") is not None
        has_inline_type = (
            el.find(f"{XS}simpleType") is not None
            or el.find(f"{XS}complexType") is not None
        )

        if not has_ref and not has_type_attr and not has_inline_type:
            errors.append(f"Line {el.sourceline} has untyped element {el.get('name')}")

    if len(errors) > 0:
        return "\n".join(errors)

    return ""

def prettify_file(file: Path):
    linted_xml = run_linter(file)
    linting_errors = get_linting_errors(file)
    if linting_errors:

    errors = get_errors(file)
    if errors:
        file.write_text(linted_xml, encoding="utf8")
        return True
    else:
        print("Linter returned no errors - nothing to do!")

    print("Check returned no errors - nothing to do!")
    return False


@@ -95,7 +135,7 @@ if __name__ == "__main__":
           (str(f) in ignore_files):
            print(f"(Ignoring {f})")
            continue
        new_errors = get_linting_errors(f)
        new_errors = get_errors(f)
        if new_errors:
            files_with_errors += 1
            print(f"{str(f)}")
+1 −0
Original line number Diff line number Diff line
colorama
lxml