From fe0fab61f93c3e09dbecc45875aff7e3e133bcf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otto?= Date: Wed, 6 Nov 2024 11:05:35 +0100 Subject: [PATCH] Result: remove path fragment --- aas_test_engines/file.py | 37 ++++++++++++++++++------------------- aas_test_engines/result.py | 10 +++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/aas_test_engines/file.py b/aas_test_engines/file.py index 556addb..21f217f 100755 --- a/aas_test_engines/file.py +++ b/aas_test_engines/file.py @@ -78,7 +78,7 @@ def check_json_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestRes try: data = json.load(file) except json.decoder.JSONDecodeError as e: - return AasTestResult(f"Invalid JSON: {e}", '', Level.ERROR) + return AasTestResult(f"Invalid JSON: {e}", Level.ERROR) return check_json_data(data, version) @@ -91,7 +91,7 @@ def check_xml_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestResu try: data = ElementTree.fromstring(file.read()) except ElementTree.ParseError as e: - return AasTestResult(f"Invalid xml: {e}", '', Level.ERROR) + return AasTestResult(f"Invalid xml: {e}", Level.ERROR) return check_xml_data(data, version) @@ -128,7 +128,7 @@ def dump(self, indent=0): def _check_content_type(zipfile: zipfile.ZipFile) -> AasTestResult: content_types_xml = '[Content_Types].xml' - result = AasTestResult(f'Checking {content_types_xml}', content_types_xml) + result = AasTestResult(f'Checking {content_types_xml}') try: with zipfile.open(content_types_xml, 'r') as f: content_types = ElementTree.parse(f) @@ -138,7 +138,7 @@ def _check_content_type(zipfile: zipfile.ZipFile) -> AasTestResult: AasTestResult(f"root must have tag {expected_tag}, got {content_types.getroot().tag}", content_types_xml, Level.ERROR)) except KeyError: result.append(AasTestResult( - f"{content_types_xml} not found", content_types_xml, Level.ERROR)) + f"{content_types_xml} not found", Level.ERROR)) return result @@ -152,23 +152,22 @@ def _scan_relationships(zipfile: zipfile.ZipFile, parent_rel: Relationship, dir: return None expected_tag = f"{NS_RELATIONSHIPS}Relationships" if relationships.tag != expected_tag: - return AasTestResult(f'Invalid root tag {relationships.tag}, expected {expected_tag}', relationships.tag, Level.ERROR) + return AasTestResult(f'Invalid root tag {relationships.tag}, expected {expected_tag}', Level.ERROR) if dir: - result = AasTestResult(f"Checking relationships of {dir}{file}", relationships.tag) + result = AasTestResult(f"Checking relationships of {dir}{file}") else: - result = AasTestResult(f"Checking root relationship", relationships.tag) + result = AasTestResult(f"Checking root relationship") for idx, rel in enumerate(relationships): if rel.tag != f"{NS_RELATIONSHIPS}Relationship": result.append(AasTestResult( - f'Invalid tag {rel.tag}', str(idx), Level.ERROR)) + f'Invalid tag {rel.tag}', Level.ERROR)) continue try: type = rel.attrib['Type'] target = rel.attrib['Target'] except KeyError as e: - result.append(AasTestResult( - f'Attribute {e} is missing', str(idx), Level.ERROR)) + result.append(AasTestResult(f'Attribute {e} is missing', Level.ERROR)) continue if type in DEPRECATED_TYPES: @@ -184,15 +183,15 @@ def _scan_relationships(zipfile: zipfile.ZipFile, parent_rel: Relationship, dir: sub_dir, file = splitpath(target) sub_rel = Relationship(type, target) - result.append(AasTestResult(f'Relationship {sub_rel.target} is of type {sub_rel.type}', str(idx), Level.INFO)) + result.append(AasTestResult(f'Relationship {sub_rel.target} is of type {sub_rel.type}', Level.INFO)) parent_rel.sub_rels.append(sub_rel) if target in visited_targets: - result.append(AasTestResult(f'Already checked {target}', str(idx), Level.INFO)) + result.append(AasTestResult(f'Already checked {target}', Level.INFO)) continue visited_targets.add(target) if target not in zipfile.namelist(): result.append(AasTestResult( - f'Relationship has non-existing target {target}', str(idx), Level.ERROR)) + f'Relationship has non-existing target {target}', Level.ERROR)) continue r = _scan_relationships(zipfile, sub_rel, sub_dir + '/', file, visited_targets) if r: @@ -202,18 +201,18 @@ def _scan_relationships(zipfile: zipfile.ZipFile, parent_rel: Relationship, dir: def _check_relationships(zipfile: zipfile.ZipFile, root_rel: Relationship) -> AasTestResult: - result = AasTestResult('Checking relationships', '') + result = AasTestResult('Checking relationships') visited_targets = set() r = _scan_relationships(zipfile, root_rel, '', '', visited_targets) if r: result.append(r) else: - result.append(AasTestResult(f"Root relationship does not exist", '', Level.ERROR)) + result.append(AasTestResult(f"Root relationship does not exist", Level.ERROR)) return result def _check_files(zipfile: zipfile.ZipFile, root_rel: Relationship, version: str) -> AasTestResult: - result = AasTestResult('Checking files', '') + result = AasTestResult('Checking files') origin_rels = root_rel.sub_rels_by_type(TYPE_AASX_ORIGIN) if len(origin_rels) != 1: result.append(AasTestResult(f"Expected exactly one aas origin, but found {len(origin_rels)}", level=Level.WARNING)) @@ -222,7 +221,7 @@ def _check_files(zipfile: zipfile.ZipFile, root_rel: Relationship, version: str) if not spec_rels: result.append(AasTestResult("No aas spec found", level=Level.WARNING)) for aasx_spec in spec_rels: - sub_result = AasTestResult(f'Checking {aasx_spec.target}', aasx_spec.target) + sub_result = AasTestResult(f'Checking {aasx_spec.target}') try: with zipfile.open(aasx_spec.target) as f: if aasx_spec.target.endswith('.xml'): @@ -230,7 +229,7 @@ def _check_files(zipfile: zipfile.ZipFile, root_rel: Relationship, version: str) elif aasx_spec.target.endswith('.json'): r = check_json_file(f, version) else: - r = AasTestResult('Unknown filetype', aasx_spec.target, Level.WARNING) + r = AasTestResult('Unknown filetype', Level.WARNING) sub_result.append(r) except KeyError: return AasTestResult("File does not exist") @@ -240,7 +239,7 @@ def _check_files(zipfile: zipfile.ZipFile, root_rel: Relationship, version: str) def check_aasx_data(zipfile: zipfile.ZipFile, version: str = _DEFAULT_VERSION) -> AasTestResult: - result = AasTestResult('Checking AASX package', '') + result = AasTestResult('Checking AASX package') result.append(_check_content_type(zipfile)) if not result.ok(): diff --git a/aas_test_engines/result.py b/aas_test_engines/result.py index 5c09a52..0669146 100644 --- a/aas_test_engines/result.py +++ b/aas_test_engines/result.py @@ -30,9 +30,9 @@ def color(self) -> str: class AasTestResult: - def __init__(self, message: str, path_fragment: str = '', level=Level.INFO): + def __init__(self, message: str, level=Level.INFO): + assert isinstance(level, Level) self.message = message - self.path_fragment = path_fragment self.level = level self.sub_results: List[AasTestResult] = [] @@ -52,7 +52,7 @@ def to_lines(self, indent=0, path=''): ENDC = '\033[0m' yield " " * indent + self.level.color() + self.message + ENDC for sub_result in self.sub_results: - yield from sub_result.to_lines(indent + 1, path + "/" + self.path_fragment) + yield from sub_result.to_lines(indent + 1) def _to_html(self) -> str: cls = { @@ -91,7 +91,6 @@ def to_html(self) -> str: def to_dict(self): return { 'm': self.message, - 'f': self.path_fragment, 'l': self.level.value, 's': [i.to_dict() for i in self.sub_results] } @@ -99,7 +98,7 @@ def to_dict(self): @classmethod def from_json(self, data: dict) -> "AasTestResult": v = AasTestResult( - data['m'], data['f'], Level(data['l']) + data['m'], Level(data['l']) ) for i in data['s']: v.append(AasTestResult.from_json(i)) @@ -137,6 +136,7 @@ class ResultException(Exception): def __init__(self, result) -> None: self.result = result + def _as_result(message: Union[str, AasTestResult], level: Level): if isinstance(message, AasTestResult): return message