Skip to content

Commit

Permalink
Result: remove path fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed Nov 6, 2024
1 parent 3805440 commit fe0fab6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
37 changes: 18 additions & 19 deletions aas_test_engines/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)


Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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))
Expand All @@ -222,15 +221,15 @@ 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'):
r = check_xml_file(f, version)
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")
Expand All @@ -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():
Expand Down
10 changes: 5 additions & 5 deletions aas_test_engines/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand All @@ -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 = {
Expand Down Expand Up @@ -91,15 +91,14 @@ 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]
}

@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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe0fab6

Please sign in to comment.