4.3. local_file - Store and Retrieve AAS-objects as JSON Files
This module adds the functionality of storing and retrieving Identifiable objects
in local files.
The LocalFileBackend takes care of updating and committing objects from and to the files, while the
LocalFileObjectStore handles adding, deleting and otherwise managing the AAS objects in a specific Directory.
- exception FileBackendSourceError
Raised, if the given object’s source is not resolvable as a local file
- class LocalFileBackend
This Backend stores each Identifiable object as a single JSON document as a local file in a directory. Each document’s id is build from the object’s identifier using a SHA256 sum of its identifiable; the document’s contents comprise a single property
data, containing the JSON serialization of the BaSyx Python SDK object. The adapter.json package is used for serialization and deserialization of objects.- classmethod commit_object(committed_object: Referable, store_object: Referable, relative_path: List[str]) None
Function (class method) to be called when an object shall be committed (local changes pushed to the external data source) via this backend implementation.
It is automatically called by the
commit()implementation, when the source URI of the object or the source URI one of its ancestors in the AAS object containment hierarchy include an URI schema for which this backend has been registered. Both of the objects are passed to this function: the one which shall be committed (committed_object) and its ancestor with the relevant source URI (store_object). They may be the same, the committed object has a source with the relevant schema itself. Additionally, therelative_pathfrom thestore_objectdown to thecommitted_objectis provided.The backend MUST ensure to commit all local changes of at least the
committed_objectand all objects contained within it (if any) to the data source. It MAY additionally commit changes to other objects (i.e. thestore_objectand any additional contained object).For this purpose a concrete implementation of this method would typically use the
sourceattribute of thestore_objectto identify the data source. If the data source supports fine-grained access to contained objects, therelative_pathmay become handy to compose the committed object’s address within the data source’s interface.- Parameters:
committed_object – The object which shall be synced to the external data source
store_object – The object which originates from the relevant data source (i.e. has the relevant source attribute). It may be the
committed_objector one of its ancestors in the AAS object hierarchy.relative_path – List of idShort strings to resolve the
committed_objectstarting at thestore_object, such that obj = store_object; for i in relative_path: obj = obj.get_referable(i) resolves to thecommitted_object. In case thatstore_object is committed_object, it is an empty list.
- Raises:
BackendNotAvailableException – when the external data source cannot be reached
- classmethod update_object(updated_object: Referable, store_object: Referable, relative_path: List[str]) None
Function (class method) to be called when an object shall be updated (local object updated with changes from the external data source) via this backend implementation.
It is automatically called by the
update()implementation, when the source URI of the object or the source URI one of its ancestors in the AAS object containment hierarchy include an URI schema for which this backend has been registered. Both of the objects are passed to this function: the one which shall be update (updated_object) and its ancestor with the relevant source URI (store_object). They may be the same, the updated object has a source with the relevant schema itself. Additionally, therelative_pathfrom thestore_objectdown to theupdated_objectis provided.The backend MUST ensure to update at least the
updated_objectand all objects contained within it (if any) with any changes from the data source. It MAY additionally update other objects (i.e. thestore_objectand any additional contained object).For this purpose a concrete implementation of this method would typically use the
sourceattribute of thestore_objectto identify the data source. If the data source supports fine-grained access to contained objects, therelative_pathmay become handy to compose the updated object’s address within the data source’s interface.- Parameters:
updated_object – The object which shall be synced from the external data source
store_object – The object which originates from the relevant data source (i.e. has the relevant source attribute). It may be the
committed_objector one of its ancestors in the AAS object hierarchy.relative_path – List of idShort strings to resolve the
updated_objectstarting at thestore_object, such that obj = store_object; for i in relative_path: obj = obj.get_referable(i) resolves to theupdated_object. In case thatstore_object is updated_object, it is an empty list.
- Raises:
BackendNotAvailableException – when the external data source cannot be reached
- class LocalFileObjectStore(directory_path: str)
An ObjectStore implementation for
IdentifiableBaSyx Python SDK objects backed by a local file based local backend- add(x: Identifiable) None
Add an object to the store
- Raises:
KeyError – If an object with the same id exists already in the object store
- check_directory(create=False)
Check if the directory exists and created it if not (and requested to do so)
- Parameters:
create – If True and the database does not exist, try to create it
- discard(x: Identifiable) None
Delete an
IdentifiableAAS object from the local file store- Parameters:
x – The object to be deleted
- Raises:
KeyError – If the object does not exist in the database
- generate_source(identifiable: Identifiable) str
Generates the source string for an
Identifiableobject that is backed by the File- Parameters:
identifiable – Identifiable object
- get_identifiable(identifier: str) Identifiable
Retrieve an AAS object from the local file by its
Identifier- Raises:
KeyError – If the respective file could not be found
- get_identifiable_by_hash(hash_: str) Identifiable
Retrieve an AAS object from the local file by its identifier hash
- Raises:
KeyError – If the respective file could not be found