|
22 | 22 | import datetime |
23 | 23 | import logging |
24 | 24 | from typing import Optional, Tuple |
| 25 | +import io |
| 26 | +from lxml import etree # type: ignore |
25 | 27 |
|
26 | 28 | import pyecma376_2 |
27 | 29 |
|
| 30 | +from . import compliance_check_json, compliance_check_xml |
28 | 31 | from .. import model |
29 | 32 | from ..adapter import aasx |
30 | 33 | from ..examples.data import example_aas, create_example_aas_binding |
@@ -90,6 +93,69 @@ def check_deserialization(file_path: str, state_manager: ComplianceToolStateMana |
90 | 93 | return obj_store, files, new_cp |
91 | 94 |
|
92 | 95 |
|
| 96 | +def check_schema(file_path: str, state_manager: ComplianceToolStateManager) -> None: |
| 97 | + """ |
| 98 | + Checks a given file against the official json schema and reports any issues using the given |
| 99 | + :class:`~aas.compliance_tool.state_manager.ComplianceToolStateManager` |
| 100 | +
|
| 101 | + Opens the file and checks if the data inside is stored in XML or JSON. Then calls the respective compliance tool |
| 102 | + schema check |
| 103 | + """ |
| 104 | + logger = logging.getLogger('compliance_check') |
| 105 | + logger.addHandler(state_manager) |
| 106 | + logger.propagate = False |
| 107 | + logger.setLevel(logging.INFO) |
| 108 | + |
| 109 | + # create handler to get logger info |
| 110 | + logger_deserialization = logging.getLogger(aasx.__name__) |
| 111 | + logger_deserialization.addHandler(state_manager) |
| 112 | + logger_deserialization.propagate = False |
| 113 | + logger_deserialization.setLevel(logging.INFO) |
| 114 | + |
| 115 | + state_manager.add_step('Open file') |
| 116 | + try: |
| 117 | + # open given file |
| 118 | + reader = aasx.AASXReader(file_path) |
| 119 | + state_manager.set_step_status_from_log() |
| 120 | + except ValueError as error: |
| 121 | + logger.error(error) |
| 122 | + state_manager.set_step_status_from_log() |
| 123 | + state_manager.add_step('Read file') |
| 124 | + state_manager.set_step_status(Status.NOT_EXECUTED) |
| 125 | + return |
| 126 | + |
| 127 | + try: |
| 128 | + # read given file (Find XML and JSON parts) |
| 129 | + state_manager.add_step('Read file') |
| 130 | + core_rels = reader.reader.get_related_parts_by_type() |
| 131 | + try: |
| 132 | + aasx_origin_part = core_rels[aasx.RELATIONSHIP_TYPE_AASX_ORIGIN][0] |
| 133 | + except IndexError as e: |
| 134 | + raise ValueError("Not a valid AASX file: aasx-origin Relationship is missing.") from e |
| 135 | + state_manager.set_step_status(Status.SUCCESS) |
| 136 | + for aas_part in reader.reader.get_related_parts_by_type(aasx_origin_part)[ |
| 137 | + aasx.RELATIONSHIP_TYPE_AAS_SPEC]: |
| 138 | + content_type = reader.reader.get_content_type(aas_part) |
| 139 | + extension = aas_part.split("/")[-1].split(".")[-1] |
| 140 | + with reader.reader.open_part(aas_part) as p: |
| 141 | + if content_type.split(";")[0] in ( |
| 142 | + "text/xml", "application/xml") or content_type == "" and extension == "xml": |
| 143 | + logger.debug("Parsing AAS objects from XML stream in OPC part {} ...".format(aas_part)) |
| 144 | + compliance_check_xml._check_schema(p, state_manager) |
| 145 | + elif content_type.split(";")[0] == "application/json" \ |
| 146 | + or content_type == "" and extension == "json": |
| 147 | + logger.debug("Parsing AAS objects from JSON stream in OPC part {} ...".format(aas_part)) |
| 148 | + compliance_check_json._check_schema(io.TextIOWrapper(p, encoding='utf-8-sig'), state_manager) |
| 149 | + else: |
| 150 | + raise ValueError("Could not determine part format of AASX part {} (Content Type: {}, extension: {}" |
| 151 | + .format(aas_part, content_type, extension)) |
| 152 | + except ValueError as error: |
| 153 | + logger.error(error) |
| 154 | + state_manager.set_step_status(Status.FAILED) |
| 155 | + finally: |
| 156 | + reader.close() |
| 157 | + |
| 158 | + |
93 | 159 | def check_aas_example(file_path: str, state_manager: ComplianceToolStateManager) -> None: |
94 | 160 | """ |
95 | 161 | Checks if a file contains all elements of the aas example and reports any issues using the given |
|
0 commit comments