Skip to content
Snippets Groups Projects
Commit dffc1fa1 authored by Michael Thies's avatar Michael Thies
Browse files

Merge branch 'improve/republish_adapter_functions' into 'master'

adapter: re-publish reader/writer functions

Closes #65

See merge request acplt/pyaas!23
parents fbe3f911 f37dc315
Branches
Tags
1 merge request!23adapter: re-publish reader/writer functions
Pipeline #268074 passed
Showing
with 90 additions and 81 deletions
...@@ -87,12 +87,12 @@ submodel.submodel_element.add(property) ...@@ -87,12 +87,12 @@ submodel.submodel_element.add(property)
Serialize the `Submodel` to XML: Serialize the `Submodel` to XML:
```python ```python
import aas.adapter.xml.xml_serialization from aas.adapter.xml import write_aas_xml_file
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(submodel) data.add(submodel)
with open('Simple_Submodel.xml', 'w', encoding='utf-8') as f: with open('Simple_Submodel.xml', 'w', encoding='utf-8') as f:
aas.adapter.xml.xml_serialization.write_aas_xml_file(file=f, data=data) write_aas_xml_file(file=f, data=data)
``` ```
......
""" """
This package contains different kind of adapter This package contains different kinds of adapters.
json json
This package offers an adapter for serialization and deserialization of PyAAS objects to/from JSON This package offers an adapter for serialization and deserialization of PyAAS objects to/from JSON.
xml
This package offers an adapter for serialization and deserialization of PyAAS objects to/from XML.
""" """
...@@ -31,8 +31,7 @@ import re ...@@ -31,8 +31,7 @@ import re
from typing import Dict, Tuple, IO, Union, List, Set, Optional from typing import Dict, Tuple, IO, Union, List, Set, Optional
from .. import model from .. import model
from .json.json_deserialization import read_json_aas_file from .json import read_aas_json_file, write_aas_json_file
from .json.json_serialization import write_aas_json_file
import pyecma376_2 import pyecma376_2
from ..util import traversal from ..util import traversal
...@@ -212,7 +211,7 @@ class AASXReader: ...@@ -212,7 +211,7 @@ class AASXReader:
or content_type == "" and extension == "json": or content_type == "" and extension == "json":
logger.debug("Parsing AAS objects from JSON stream in OPC part {} ...".format(part_name)) logger.debug("Parsing AAS objects from JSON stream in OPC part {} ...".format(part_name))
with self.reader.open_part(part_name) as p: with self.reader.open_part(part_name) as p:
return read_json_aas_file(io.TextIOWrapper(p, encoding='utf-8-sig')) return read_aas_json_file(io.TextIOWrapper(p, encoding='utf-8-sig'))
else: else:
logger.error("Could not determine part format of AASX part {} (Content Type: {}, extension: {}" logger.error("Could not determine part format of AASX part {} (Content Type: {}, extension: {}"
.format(part_name, content_type, extension)) .format(part_name, content_type, extension))
......
...@@ -50,7 +50,7 @@ import threading ...@@ -50,7 +50,7 @@ import threading
import logging import logging
from .. import model from .. import model
from .json import json_serialization, json_deserialization from .json import StrictAASFromJsonDecoder, AASToJsonEncoder
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -194,7 +194,7 @@ class CouchDBObjectStore(model.AbstractObjectStore): ...@@ -194,7 +194,7 @@ class CouchDBObjectStore(model.AbstractObjectStore):
""" """
logger.debug("Adding object %s to CouchDB database ...", repr(x)) logger.debug("Adding object %s to CouchDB database ...", repr(x))
# Serialize data # Serialize data
data = json.dumps({'data': x}, cls=json_serialization.AASToJsonEncoder) data = json.dumps({'data': x}, cls=AASToJsonEncoder)
# Create and issue HTTP request (raises HTTPError on status != 200) # Create and issue HTTP request (raises HTTPError on status != 200)
request = urllib.request.Request( request = urllib.request.Request(
...@@ -222,7 +222,7 @@ class CouchDBObjectStore(model.AbstractObjectStore): ...@@ -222,7 +222,7 @@ class CouchDBObjectStore(model.AbstractObjectStore):
logger.debug("Committing changes of object %s based on revision %s to CouchDB database ...", logger.debug("Committing changes of object %s based on revision %s to CouchDB database ...",
repr(x), x.couchdb_revision) repr(x), x.couchdb_revision)
# Serialize data # Serialize data
data = json.dumps({'data': x, '_rev': x.couchdb_revision}, cls=json_serialization.AASToJsonEncoder) data = json.dumps({'data': x, '_rev': x.couchdb_revision}, cls=AASToJsonEncoder)
# Create and issue HTTP request (raises HTTPError on status != 200) # Create and issue HTTP request (raises HTTPError on status != 200)
request = urllib.request.Request( request = urllib.request.Request(
...@@ -468,7 +468,7 @@ class CouchDBSubmodel(model.Submodel, CouchDBIdentifiable): ...@@ -468,7 +468,7 @@ class CouchDBSubmodel(model.Submodel, CouchDBIdentifiable):
pass pass
class CouchDBJSONDecoder(json_deserialization.StrictAASFromJsonDecoder): class CouchDBJSONDecoder(StrictAASFromJsonDecoder):
""" """
Special json.JSONDecoder class for deserializing AAS objects received from the CouchDB server Special json.JSONDecoder class for deserializing AAS objects received from the CouchDB server
......
...@@ -12,3 +12,6 @@ json_deserialization.py ...@@ -12,3 +12,6 @@ json_deserialization.py
PyI40AAS object. A function `read_json_aas_file()` is provided to read all AAS objects within a JSON file and return PyI40AAS object. A function `read_json_aas_file()` is provided to read all AAS objects within a JSON file and return
them as PyI40AAS ObjectStore. them as PyI40AAS ObjectStore.
""" """
from .json_serialization import AASToJsonEncoder, write_aas_json_file, object_store_to_json
from .json_deserialization import StrictAASFromJsonDecoder, AASFromJsonDecoder, read_aas_json_file
...@@ -675,7 +675,7 @@ class StrictAASFromJsonDecoder(AASFromJsonDecoder): ...@@ -675,7 +675,7 @@ class StrictAASFromJsonDecoder(AASFromJsonDecoder):
failsafe = False failsafe = False
def read_json_aas_file(file: IO, failsafe: bool = True) -> model.DictObjectStore: def read_aas_json_file(file: IO, failsafe: bool = True) -> model.DictObjectStore:
""" """
Read an Asset Adminstration Shell JSON file according to 'Details of the Asset Administration Shell', chapter 5.5 Read an Asset Adminstration Shell JSON file according to 'Details of the Asset Administration Shell', chapter 5.5
......
"""
This package contains functionality for serialization and deserialization of PyI40AAS objects into/from XML.
xml_serialization:
The module offers a function to write an ObjectStore to a given file.
xml_deserialization.py
The module offers a function to create an ObjectStore from a given xml document.
"""
from .xml_serialization import write_aas_xml_file
...@@ -19,7 +19,7 @@ import argparse ...@@ -19,7 +19,7 @@ import argparse
import logging import logging
from aas.compliance_tool import compliance_check_json as compliance_tool_json from aas.compliance_tool import compliance_check_json as compliance_tool_json
from aas.adapter.json import json_serialization from aas.adapter.json import write_aas_json_file
from aas.examples.data import create_example from aas.examples.data import create_example
from aas.compliance_tool.state_manager import ComplianceToolStateManager, Status from aas.compliance_tool.state_manager import ComplianceToolStateManager, Status
...@@ -68,7 +68,7 @@ def main(): ...@@ -68,7 +68,7 @@ def main():
manager.add_step('Write data to file') manager.add_step('Write data to file')
if args.json: if args.json:
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
manager.set_step_status(Status.SUCCESS) manager.set_step_status(Status.SUCCESS)
elif args.xml: elif args.xml:
# Todo: if xml serialization is done add code here # Todo: if xml serialization is done add code here
......
...@@ -146,7 +146,7 @@ def check_deserialization(file_path: str, state_manager: ComplianceToolStateMana ...@@ -146,7 +146,7 @@ def check_deserialization(file_path: str, state_manager: ComplianceToolStateMana
state_manager.add_step('Read {} file and check if it is conform to the json schema'.format(file_info)) state_manager.add_step('Read {} file and check if it is conform to the json schema'.format(file_info))
else: else:
state_manager.add_step('Read file and check if it is conform to the json schema') state_manager.add_step('Read file and check if it is conform to the json schema')
obj_store = json_deserialization.read_json_aas_file(file_to_be_checked, True) obj_store = json_deserialization.read_aas_json_file(file_to_be_checked, True)
state_manager.set_step_status_from_log() state_manager.set_step_status_from_log()
......
...@@ -10,7 +10,7 @@ import json ...@@ -10,7 +10,7 @@ import json
# Import all PyI40AAS classes from model package # Import all PyI40AAS classes from model package
from aas import model from aas import model
from aas.adapter.json import json_serialization, json_deserialization from aas.adapter.json import AASToJsonEncoder, read_aas_json_file, write_aas_json_file, object_store_to_json
from aas.model import Asset, AssetAdministrationShell, Submodel from aas.model import Asset, AssetAdministrationShell, Submodel
# In this tutorial you get a step by step guide how to serialize objects of the meta model according to # In this tutorial you get a step by step guide how to serialize objects of the meta model according to
...@@ -58,13 +58,13 @@ aas = AssetAdministrationShell( ...@@ -58,13 +58,13 @@ aas = AssetAdministrationShell(
# step 2: serialize an object to json and write it to file # # step 2: serialize an object to json and write it to file #
############################################################ ############################################################
# step 2.1: serialize an object to json # step 2.1: serialize an object to json
# json_serialization.AASToJsonEncoder is a custom JSONDecoder class for serializing Asset Administration Shell data # AASToJsonEncoder is a custom JSONDecoder class for serializing Asset Administration Shell data
# into the official JSON format according to 'Details of the Asset Administration Shell', chapter 5.5 # into the official JSON format according to 'Details of the Asset Administration Shell', chapter 5.5
# serialize an asset administration shell # serialize an asset administration shell
json_data_object = json.loads(json.dumps(aas, cls=json_serialization.AASToJsonEncoder)) json_data_object = json.loads(json.dumps(aas, cls=AASToJsonEncoder))
# serialize a property # serialize a property
json_data_object = json.loads(json.dumps(submodel.submodel_element.get_referable('ExampleProperty'), json_data_object = json.loads(json.dumps(submodel.submodel_element.get_referable('ExampleProperty'),
cls=json_serialization.AASToJsonEncoder)) cls=AASToJsonEncoder))
# step 2.2: write json data to file # step 2.2: write json data to file
# define a file stream, here an internal file stream is used. For an external file stream use # define a file stream, here an internal file stream is used. For an external file stream use
# 'open('tutorial.json', 'w', encoding='utf-8')' for opening a json-File to write json data inside # 'open('tutorial.json', 'w', encoding='utf-8')' for opening a json-File to write json data inside
...@@ -82,8 +82,8 @@ obj_store: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() ...@@ -82,8 +82,8 @@ obj_store: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
obj_store.add(asset) obj_store.add(asset)
obj_store.add(submodel) obj_store.add(submodel)
obj_store.add(aas) obj_store.add(aas)
# serialize the store using the function 'object_store_to_json' of the 'json_serialization' module # serialize the store using the function 'object_store_to_json' of the 'json' module
json_data_store = json_serialization.object_store_to_json(obj_store) json_data_store = object_store_to_json(obj_store)
# step 2.2: write json data to file # step 2.2: write json data to file
# define a file stream, here an internal file stream is used. For an external file stream use # define a file stream, here an internal file stream is used. For an external file stream use
# 'open('tutorial.json', 'w', encoding='utf-8')' for opening a json-File to write json data inside # 'open('tutorial.json', 'w', encoding='utf-8')' for opening a json-File to write json data inside
...@@ -93,7 +93,7 @@ json.dump(json_data_store, file_store) ...@@ -93,7 +93,7 @@ json.dump(json_data_store, file_store)
# serialize an object store and write it to a file can be done in one step using the function 'write_aas_json_file' # serialize an object store and write it to a file can be done in one step using the function 'write_aas_json_file'
file_store_2 = io.StringIO() file_store_2 = io.StringIO()
json_serialization.write_aas_json_file(file=file_store_2, data=obj_store) write_aas_json_file(file=file_store_2, data=obj_store)
################################################################################################# #################################################################################################
# step 4: # read a json string from file and deserialize it into an object store of AAS objects # # step 4: # read a json string from file and deserialize it into an object store of AAS objects #
...@@ -103,7 +103,7 @@ json_serialization.write_aas_json_file(file=file_store_2, data=obj_store) ...@@ -103,7 +103,7 @@ json_serialization.write_aas_json_file(file=file_store_2, data=obj_store)
# we have to set the file pointer to the beginning cause we are using the same file stream. Normally, you do not need # we have to set the file pointer to the beginning cause we are using the same file stream. Normally, you do not need
# to do this. # to do this.
file_store_2.seek(0) file_store_2.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file_store_2, failsafe=False) json_object_store = read_aas_json_file(file_store_2, failsafe=False)
# take a submodel out of the object store, for more details look at 'tutorial_storage.py' # take a submodel out of the object store, for more details look at 'tutorial_storage.py'
tmp_submodel: Submodel = json_object_store.get_identifiable( # type: ignore tmp_submodel: Submodel = json_object_store.get_identifiable( # type: ignore
......
...@@ -19,7 +19,7 @@ import io ...@@ -19,7 +19,7 @@ import io
import json import json
import logging import logging
import unittest import unittest
from aas.adapter.json import json_deserialization from aas.adapter.json import AASFromJsonDecoder, StrictAASFromJsonDecoder, read_aas_json_file
from aas import model from aas import model
...@@ -32,9 +32,9 @@ class JsonDeserializationTest(unittest.TestCase): ...@@ -32,9 +32,9 @@ class JsonDeserializationTest(unittest.TestCase):
"conceptDescriptions": [] "conceptDescriptions": []
}""" }"""
with self.assertRaisesRegex(KeyError, r"submodels"): with self.assertRaisesRegex(KeyError, r"submodels"):
json_deserialization.read_json_aas_file(io.StringIO(data), False) read_aas_json_file(io.StringIO(data), False)
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
json_deserialization.read_json_aas_file(io.StringIO(data), True) read_aas_json_file(io.StringIO(data), True)
self.assertIn("submodels", cm.output[0]) self.assertIn("submodels", cm.output[0])
def test_file_format_wrong_list(self) -> None: def test_file_format_wrong_list(self) -> None:
...@@ -57,9 +57,9 @@ class JsonDeserializationTest(unittest.TestCase): ...@@ -57,9 +57,9 @@ class JsonDeserializationTest(unittest.TestCase):
] ]
}""" }"""
with self.assertRaisesRegex(TypeError, r"submodels.*Asset"): with self.assertRaisesRegex(TypeError, r"submodels.*Asset"):
json_deserialization.read_json_aas_file(io.StringIO(data), False) read_aas_json_file(io.StringIO(data), False)
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
json_deserialization.read_json_aas_file(io.StringIO(data), True) read_aas_json_file(io.StringIO(data), True)
self.assertIn("submodels", cm.output[0]) self.assertIn("submodels", cm.output[0])
self.assertIn("Asset", cm.output[0]) self.assertIn("Asset", cm.output[0])
...@@ -74,9 +74,9 @@ class JsonDeserializationTest(unittest.TestCase): ...@@ -74,9 +74,9 @@ class JsonDeserializationTest(unittest.TestCase):
] ]
}""" }"""
with self.assertRaisesRegex(TypeError, r"submodels.*'foo'"): with self.assertRaisesRegex(TypeError, r"submodels.*'foo'"):
json_deserialization.read_json_aas_file(io.StringIO(data), False) read_aas_json_file(io.StringIO(data), False)
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
json_deserialization.read_json_aas_file(io.StringIO(data), True) read_aas_json_file(io.StringIO(data), True)
self.assertIn("submodels", cm.output[0]) self.assertIn("submodels", cm.output[0])
self.assertIn("'foo'", cm.output[0]) self.assertIn("'foo'", cm.output[0])
...@@ -100,11 +100,11 @@ class JsonDeserializationTest(unittest.TestCase): ...@@ -100,11 +100,11 @@ class JsonDeserializationTest(unittest.TestCase):
]""" ]"""
# In strict mode, we should catch an exception # In strict mode, we should catch an exception
with self.assertRaisesRegex(KeyError, r"identification"): with self.assertRaisesRegex(KeyError, r"identification"):
json.loads(data, cls=json_deserialization.StrictAASFromJsonDecoder) json.loads(data, cls=StrictAASFromJsonDecoder)
# In failsafe mode, we should get a log entry and the first Asset entry should be returned as untouched dict # In failsafe mode, we should get a log entry and the first Asset entry should be returned as untouched dict
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
parsed_data = json.loads(data, cls=json_deserialization.AASFromJsonDecoder) parsed_data = json.loads(data, cls=AASFromJsonDecoder)
self.assertIn("identification", cm.output[0]) self.assertIn("identification", cm.output[0])
self.assertIsInstance(parsed_data, list) self.assertIsInstance(parsed_data, list)
self.assertEqual(3, len(parsed_data)) self.assertEqual(3, len(parsed_data))
...@@ -143,13 +143,13 @@ class JsonDeserializationTest(unittest.TestCase): ...@@ -143,13 +143,13 @@ class JsonDeserializationTest(unittest.TestCase):
# The broken object should not raise an exception, but log a warning, even in strict mode. # The broken object should not raise an exception, but log a warning, even in strict mode.
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
with self.assertRaisesRegex(TypeError, r"SubmodelElement.*Asset"): with self.assertRaisesRegex(TypeError, r"SubmodelElement.*Asset"):
json.loads(data, cls=json_deserialization.StrictAASFromJsonDecoder) json.loads(data, cls=StrictAASFromJsonDecoder)
self.assertIn("modelType", cm.output[0]) self.assertIn("modelType", cm.output[0])
# In failsafe mode, we should get a log entries for the broken object and the wrong type of the first two # In failsafe mode, we should get a log entries for the broken object and the wrong type of the first two
# submodelElements # submodelElements
with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm: with self.assertLogs(logging.getLogger(), level=logging.WARNING) as cm:
parsed_data = json.loads(data, cls=json_deserialization.AASFromJsonDecoder) parsed_data = json.loads(data, cls=AASFromJsonDecoder)
self.assertGreaterEqual(len(cm.output), 3) self.assertGreaterEqual(len(cm.output), 3)
self.assertIn("SubmodelElement", cm.output[1]) self.assertIn("SubmodelElement", cm.output[1])
self.assertIn("SubmodelElement", cm.output[2]) self.assertIn("SubmodelElement", cm.output[2])
...@@ -168,7 +168,7 @@ class JsonDeserializationDerivingTest(unittest.TestCase): ...@@ -168,7 +168,7 @@ class JsonDeserializationDerivingTest(unittest.TestCase):
super().__init__(**kwargs) super().__init__(**kwargs)
self.enhanced_attribute = "fancy!" self.enhanced_attribute = "fancy!"
class EnhancedAASDecoder(json_deserialization.AASFromJsonDecoder): class EnhancedAASDecoder(AASFromJsonDecoder):
@classmethod @classmethod
def _construct_asset(cls, dct): def _construct_asset(cls, dct):
return super()._construct_asset(dct, object_class=EnhancedAsset) return super()._construct_asset(dct, object_class=EnhancedAsset)
......
...@@ -15,7 +15,7 @@ import json ...@@ -15,7 +15,7 @@ import json
import os import os
from aas import model from aas import model
from aas.adapter.json import json_serialization from aas.adapter.json import AASToJsonEncoder, write_aas_json_file
from jsonschema import validate # type: ignore from jsonschema import validate # type: ignore
from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \ from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \
...@@ -28,7 +28,7 @@ class JsonSerializationTest(unittest.TestCase): ...@@ -28,7 +28,7 @@ class JsonSerializationTest(unittest.TestCase):
def test_serialize_object(self) -> None: def test_serialize_object(self) -> None:
test_object = model.Property("test_id_short", model.datatypes.String, category="PARAMETER", test_object = model.Property("test_id_short", model.datatypes.String, category="PARAMETER",
description={"en-us": "Germany", "de": "Deutschland"}) description={"en-us": "Germany", "de": "Deutschland"})
json_data = json.dumps(test_object, cls=json_serialization.AASToJsonEncoder) json_data = json.dumps(test_object, cls=AASToJsonEncoder)
def test_random_object_serialization(self) -> None: def test_random_object_serialization(self) -> None:
asset_key = (model.Key(model.KeyElements.ASSET, True, "asset", model.KeyType.CUSTOM),) asset_key = (model.Key(model.KeyElements.ASSET, True, "asset", model.KeyType.CUSTOM),)
...@@ -47,7 +47,7 @@ class JsonSerializationTest(unittest.TestCase): ...@@ -47,7 +47,7 @@ class JsonSerializationTest(unittest.TestCase):
'submodels': [submodel], 'submodels': [submodel],
'assets': [], 'assets': [],
'conceptDescriptions': [], 'conceptDescriptions': [],
}, cls=json_serialization.AASToJsonEncoder) }, cls=AASToJsonEncoder)
json_data_new = json.loads(json_data) json_data_new = json.loads(json_data)
...@@ -72,7 +72,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -72,7 +72,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
'submodels': [submodel], 'submodels': [submodel],
'assets': [], 'assets': [],
'conceptDescriptions': [], 'conceptDescriptions': [],
}, cls=json_serialization.AASToJsonEncoder) }, cls=AASToJsonEncoder)
json_data_new = json.loads(json_data) json_data_new = json.loads(json_data)
# load schema # load schema
...@@ -85,7 +85,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -85,7 +85,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
def test_aas_example_serialization(self) -> None: def test_aas_example_serialization(self) -> None:
data = example_aas.create_full_example() data = example_aas.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
...@@ -100,7 +100,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -100,7 +100,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_submodel_template.create_example_submodel_template()) data.add(example_submodel_template.create_example_submodel_template())
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
...@@ -114,7 +114,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -114,7 +114,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
def test_full_empty_example_serialization(self) -> None: def test_full_empty_example_serialization(self) -> None:
data = example_aas_mandatory_attributes.create_full_example() data = example_aas_mandatory_attributes.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
...@@ -128,7 +128,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -128,7 +128,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
def test_missing_serialization(self) -> None: def test_missing_serialization(self) -> None:
data = example_aas_missing_attributes.create_full_example() data = example_aas_missing_attributes.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
...@@ -143,7 +143,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -143,7 +143,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_concept_description.create_iec61360_concept_description()) data.add(example_concept_description.create_iec61360_concept_description())
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
...@@ -157,7 +157,7 @@ class JsonSerializationSchemaTest(unittest.TestCase): ...@@ -157,7 +157,7 @@ class JsonSerializationSchemaTest(unittest.TestCase):
def test_full_example_serialization(self) -> None: def test_full_example_serialization(self) -> None:
data = create_example() data = create_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
with open(JSON_SCHEMA_FILE, 'r') as json_file: with open(JSON_SCHEMA_FILE, 'r') as json_file:
aas_json_schema = json.load(json_file) aas_json_schema = json.load(json_file)
......
...@@ -15,7 +15,7 @@ import os ...@@ -15,7 +15,7 @@ import os
import unittest import unittest
from aas import model from aas import model
from aas.adapter.json import json_serialization, json_deserialization from aas.adapter.json import AASToJsonEncoder, write_aas_json_file, read_aas_json_file
from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \ from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \
example_aas_mandatory_attributes, example_aas, example_concept_description example_aas_mandatory_attributes, example_aas, example_concept_description
...@@ -41,22 +41,20 @@ class JsonSerializationDeserializationTest(unittest.TestCase): ...@@ -41,22 +41,20 @@ class JsonSerializationDeserializationTest(unittest.TestCase):
'submodels': [submodel], 'submodels': [submodel],
'assets': [], 'assets': [],
'conceptDescriptions': [], 'conceptDescriptions': [],
}, cls=json_serialization.AASToJsonEncoder) }, cls=AASToJsonEncoder)
json_data_new = json.loads(json_data) json_data_new = json.loads(json_data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module json_object_store = read_aas_json_file(io.StringIO(json_data), failsafe=False)
json_object_store = json_deserialization.read_json_aas_file(io.StringIO(json_data), failsafe=False)
def test_example_serialization_deserialization(self) -> None: def test_example_serialization_deserialization(self) -> None:
data = example_aas.create_full_example() data = example_aas.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module
file.seek(0) file.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file, failsafe=False) json_object_store = read_aas_json_file(file, failsafe=False)
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
example_aas.check_full_example(checker, json_object_store) example_aas.check_full_example(checker, json_object_store)
...@@ -65,12 +63,11 @@ class JsonSerializationDeserializationTest2(unittest.TestCase): ...@@ -65,12 +63,11 @@ class JsonSerializationDeserializationTest2(unittest.TestCase):
def test_example_mandatory_attributes_serialization_deserialization(self) -> None: def test_example_mandatory_attributes_serialization_deserialization(self) -> None:
data = example_aas_mandatory_attributes.create_full_example() data = example_aas_mandatory_attributes.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module
file.seek(0) file.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file, failsafe=False) json_object_store = read_aas_json_file(file, failsafe=False)
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
example_aas_mandatory_attributes.check_full_example(checker, json_object_store) example_aas_mandatory_attributes.check_full_example(checker, json_object_store)
...@@ -79,11 +76,10 @@ class JsonSerializationDeserializationTest3(unittest.TestCase): ...@@ -79,11 +76,10 @@ class JsonSerializationDeserializationTest3(unittest.TestCase):
def test_example_missing_attributes_serialization_deserialization(self) -> None: def test_example_missing_attributes_serialization_deserialization(self) -> None:
data = example_aas_missing_attributes.create_full_example() data = example_aas_missing_attributes.create_full_example()
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module
file.seek(0) file.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file, failsafe=False) json_object_store = read_aas_json_file(file, failsafe=False)
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
example_aas_missing_attributes.check_full_example(checker, json_object_store) example_aas_missing_attributes.check_full_example(checker, json_object_store)
...@@ -93,11 +89,10 @@ class JsonSerializationDeserializationTest4(unittest.TestCase): ...@@ -93,11 +89,10 @@ class JsonSerializationDeserializationTest4(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_submodel_template.create_example_submodel_template()) data.add(example_submodel_template.create_example_submodel_template())
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module
file.seek(0) file.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file, failsafe=False) json_object_store = read_aas_json_file(file, failsafe=False)
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
example_submodel_template.check_full_example(checker, json_object_store) example_submodel_template.check_full_example(checker, json_object_store)
...@@ -107,10 +102,9 @@ class JsonSerializationDeserializationTest5(unittest.TestCase): ...@@ -107,10 +102,9 @@ class JsonSerializationDeserializationTest5(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_concept_description.create_iec61360_concept_description()) data.add(example_concept_description.create_iec61360_concept_description())
file = io.StringIO() file = io.StringIO()
json_serialization.write_aas_json_file(file=file, data=data) write_aas_json_file(file=file, data=data)
# try deserializing the json string into a DictObjectStore of AAS objects with help of the json_deserialization # try deserializing the json string into a DictObjectStore of AAS objects with help of the json module
# module
file.seek(0) file.seek(0)
json_object_store = json_deserialization.read_json_aas_file(file, failsafe=False) json_object_store = read_aas_json_file(file, failsafe=False)
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
example_concept_description.check_full_example(checker, json_object_store) example_concept_description.check_full_example(checker, json_object_store)
...@@ -14,7 +14,7 @@ from lxml import etree # type: ignore ...@@ -14,7 +14,7 @@ from lxml import etree # type: ignore
import os import os
from aas import model from aas import model
from aas.adapter.xml import xml_serialization from aas.adapter.xml import write_aas_xml_file, xml_serialization
from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \ from aas.examples.data import example_aas_missing_attributes, example_submodel_template, \
example_aas_mandatory_attributes, example_aas, example_concept_description example_aas_mandatory_attributes, example_aas, example_concept_description
...@@ -47,7 +47,7 @@ class XMLSerializationTest(unittest.TestCase): ...@@ -47,7 +47,7 @@ class XMLSerializationTest(unittest.TestCase):
test_data.add(submodel) test_data.add(submodel)
test_file = io.BytesIO() test_file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=test_file, data=test_data) write_aas_xml_file(file=test_file, data=test_data)
@unittest.skipUnless(os.path.exists(XML_SCHEMA_FILE), "XML Schema not found for validation") @unittest.skipUnless(os.path.exists(XML_SCHEMA_FILE), "XML Schema not found for validation")
...@@ -69,7 +69,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -69,7 +69,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
test_data.add(submodel) test_data.add(submodel)
test_file = io.BytesIO() test_file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=test_file, data=test_data) write_aas_xml_file(file=test_file, data=test_data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
...@@ -82,7 +82,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -82,7 +82,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
def test_full_example_serialization(self) -> None: def test_full_example_serialization(self) -> None:
data = example_aas.create_full_example() data = example_aas.create_full_example()
file = io.BytesIO() file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=file, data=data) write_aas_xml_file(file=file, data=data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
...@@ -96,7 +96,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -96,7 +96,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_submodel_template.create_example_submodel_template()) data.add(example_submodel_template.create_example_submodel_template())
file = io.BytesIO() file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=file, data=data) write_aas_xml_file(file=file, data=data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
...@@ -109,7 +109,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -109,7 +109,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
def test_full_empty_example_serialization(self) -> None: def test_full_empty_example_serialization(self) -> None:
data = example_aas_mandatory_attributes.create_full_example() data = example_aas_mandatory_attributes.create_full_example()
file = io.BytesIO() file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=file, data=data) write_aas_xml_file(file=file, data=data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
...@@ -122,7 +122,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -122,7 +122,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
def test_missing_serialization(self) -> None: def test_missing_serialization(self) -> None:
data = example_aas_missing_attributes.create_full_example() data = example_aas_missing_attributes.create_full_example()
file = io.BytesIO() file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=file, data=data) write_aas_xml_file(file=file, data=data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
...@@ -136,7 +136,7 @@ class XMLSerializationSchemaTest(unittest.TestCase): ...@@ -136,7 +136,7 @@ class XMLSerializationSchemaTest(unittest.TestCase):
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
data.add(example_concept_description.create_iec61360_concept_description()) data.add(example_concept_description.create_iec61360_concept_description())
file = io.BytesIO() file = io.BytesIO()
xml_serialization.write_aas_xml_file(file=file, data=data) write_aas_xml_file(file=file, data=data)
# load schema # load schema
aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE) aas_schema = etree.XMLSchema(file=XML_SCHEMA_FILE)
......
...@@ -15,7 +15,7 @@ import unittest ...@@ -15,7 +15,7 @@ import unittest
import aas.compliance_tool import aas.compliance_tool
import tempfile import tempfile
from aas.adapter.json import json_deserialization from aas.adapter.json import read_aas_json_file
from aas.examples.data import create_example from aas.examples.data import create_example
from aas.examples.data._helper import AASDataChecker from aas.examples.data._helper import AASDataChecker
...@@ -145,7 +145,7 @@ class ComplianceToolTest(unittest.TestCase): ...@@ -145,7 +145,7 @@ class ComplianceToolTest(unittest.TestCase):
self.assertIn('SUCCESS: Write data to file', str(output.stdout)) self.assertIn('SUCCESS: Write data to file', str(output.stdout))
with open(filename, "r", encoding='utf-8-sig') as f: with open(filename, "r", encoding='utf-8-sig') as f:
json_object_store = json_deserialization.read_json_aas_file(f, failsafe=False) json_object_store = read_aas_json_file(f, failsafe=False)
data = create_example() data = create_example()
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
checker.check_object_store(json_object_store, data) checker.check_object_store(json_object_store, data)
...@@ -207,7 +207,7 @@ class ComplianceToolTest(unittest.TestCase): ...@@ -207,7 +207,7 @@ class ComplianceToolTest(unittest.TestCase):
self.assertIn('SUCCESS: Write data to file', str(output.stdout)) self.assertIn('SUCCESS: Write data to file', str(output.stdout))
with open(filename, "r", encoding='utf-8-sig') as f: with open(filename, "r", encoding='utf-8-sig') as f:
json_object_store = json_deserialization.read_json_aas_file(f, failsafe=False) json_object_store = read_aas_json_file(f, failsafe=False)
data = create_example() data = create_example()
checker = AASDataChecker(raise_immediately=True) checker = AASDataChecker(raise_immediately=True)
checker.check_object_store(json_object_store, data) checker.check_object_store(json_object_store, data)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment