API
Read and write the packaged file format (AASX) of an Asset Administration Shell V3.
The package follows the Open Packaging Conventions (OPC, ECMA-376 Part 2).
The whole package is kept in memory. The changes are written to the underlying
file or stream only when you call PackageReadWrite.flush().
The objects of this module are not thread-safe.
- aas_package3.RELATION_TYPE_AASX_ORIGIN: Final[str] = 'http://admin-shell.io/aasx/relationships/aasx-origin'
Relationship type from the package root to the origin part.
- aas_package3.RELATION_TYPE_AASX_SPEC: Final[str] = 'http://admin-shell.io/aasx/relationships/aas-spec'
Relationship type from the origin part to an AAS spec part.
- aas_package3.RELATION_TYPE_AASX_SUPPLEMENTARY: Final[str] = 'http://admin-shell.io/aasx/relationships/aas-suppl'
Relationship type from an AAS spec part to a supplementary part.
- aas_package3.RELATION_TYPE_THUMBNAIL: Final[str] = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail'
Relationship type from the package root to the thumbnail part.
- exception aas_package3.InvalidPackageError[source]
Bases:
ExceptionSignal that the data does not represent a valid AAS package.
- exception aas_package3.NoOriginError[source]
Bases:
InvalidPackageErrorSignal that the package lacks the origin part.
- exception aas_package3.DanglingRelationshipError[source]
Bases:
InvalidPackageErrorSignal that a relationship points to a part which does not exist.
- exception aas_package3.PartNotFoundError[source]
Bases:
LookupErrorSignal that the requested part does not exist in the package.
- class aas_package3.Part(uri: str, content_type: str, content: bytes)[source]
Bases:
objectRepresent a part of an AAS package.
You obtain parts from a package, e.g., with
PackageRead.specs()orPackageReadWrite.put_part(). Do not construct them yourself.
- class aas_package3.SupplementaryRelationship(spec: Part, supplementary: Part)[source]
Bases:
objectRepresent a relationship between a spec and its supplementary part.
- aas_package3.is_valid_part_uri(uri: str) bool[source]
Check that
urican be used as a URI of a new part in a package.The URI must be absolute within the package, must not end with a slash, must not contain empty,
.or..segments, and must not clash with the parts reserved by the Open Packaging Conventions.>>> is_valid_part_uri("/aasx/data.json") True >>> is_valid_part_uri("aasx/data.json") False >>> is_valid_part_uri("/aasx/../data.json") False >>> is_valid_part_uri("/aasx/_rels/data.json.rels") False
- class aas_package3.PackageRead(path: Path | None, content: _Content)[source]
Bases:
objectRead from an AAS package.
Use
Packagingto open a package. The package can be used as a context manager.- property path: Path | None
Path associated with the package.
None if the package does not live in a file (e.g., if it comes from a stream).
- close() None[source]
Close the package.
The streams which you passed in are not closed, as they are owned by you.
- specs() List[Part][source]
List AAS specs contained in the package.
- Raise:
DanglingRelationshipErrorif a spec part is missing
- specs_by_content_type() Mapping[str, List[Part]][source]
List AAS specs grouped by their MIME content type.
Both the content types and the specs within a group are sorted to make the result reproducible.
- Raise:
DanglingRelationshipErrorif a spec part is missing
- is_spec(part: Part) bool[source]
Check that
partis related to the origin of the package as a spec.
- supplementaries_for(spec: Part) List[Part][source]
List the supplementary parts related to the
spec.- Raise:
DanglingRelationshipErrorif a supplementary is missing
- supplementary_relationships() List[SupplementaryRelationship][source]
List all the supplementary relationships from all the specs.
- Raise:
DanglingRelationshipErrorif a related part is missing
- find_part(uri: str) Part | None[source]
Try to find the package part with the given
uri.The URIs are compared case-insensitively and percent-decoded as prescribed by the Open Packaging Conventions.
- Returns:
the part, or None if it does not exist
- must_part(uri: str) Part[source]
Retrieve the package part with the given
uri.- Raise:
PartNotFoundErrorif the part does not exist
- thumbnail() Part | None[source]
Retrieve the thumbnail of the package.
- Returns:
the thumbnail, or None if there is no thumbnail in the package
- Raise:
DanglingRelationshipErrorif the thumbnail relationship exists, but the part does not
- class aas_package3.PackageReadWrite(path: Path | None, stream: BinaryIO | None, content: _Content)[source]
Bases:
PackageReadRead from and write to an AAS package.
The changes are kept in memory until you call
flush()orclose().If you use the package as a context manager, it is closed, and hence flushed, when the
withblock finishes normally. If thewithblock raises an exception, the changes are discarded so that you do not end up with a half-modified package.- put_part(uri: str, content_type: str, content: bytes) Part[source]
Write the
contentto the package as a part withcontent_type.This method needs to be used to put any content into the package. You have to introduce the relationships yourself by calling, e.g.,
make_spec()orrelate_supplementary_to_spec().If a part with an equivalent URI already exists, its content and content type are overwritten, and its relationships are kept. Be careful not to unintentionally overwrite existing parts which are already related to others (e.g., do not overwrite a spec with a supplementary material).
- Returns:
the written part
- put_part_from_stream(uri: str, content_type: str, stream: BinaryIO) Part[source]
Write the content of
streamto the package as a part.The
streamis read from its current position till its end. Seeput_part()for more details.- Returns:
the written part
- delete_part(part: Part) None[source]
Remove the
partfrom the package.All the relationships from and to the part are removed as well so that the package stays consistent.
- make_spec(part: Part) Part[source]
Relate the
partto the package origin as a spec.If the part is already a spec, nothing happens.
- Returns:
the same unchanged
part, so that you can chain the calls
- unmake_spec(part: Part) Part[source]
Remove the relationship from the package origin to the
partas a spec.The part itself is kept. Call
delete_part()if you want to remove it as well.- Returns:
the same unchanged
part, so that you can chain the calls
- relate_supplementary_to_spec(supplementary: Part, spec: Part) Part[source]
Relate the
supplementaryto thespecas a supplementary material.If the relationship already exists, nothing happens.
- Returns:
the same unchanged
supplementary, so that you can chain the calls
- unrelate_supplementary_from_spec(supplementary: Part, spec: Part) Part[source]
Remove the supplementary relationship between
supplementaryandspec.If the relationship has not been established, nothing happens. The part itself is kept. Call
delete_part()if you want to remove it as well.- Returns:
the same unchanged
supplementary, so that you can chain the calls
- set_thumbnail(part: Part) Part[source]
Relate the
partto the package root as the thumbnail.If there is a thumbnail already, its relationship is replaced. The previous thumbnail part itself is kept. Call
delete_part()if you want to remove it as well.- Returns:
the same unchanged
part, so that you can chain the calls
- unset_thumbnail() None[source]
Remove the relationship from the package root to the thumbnail, if any.
The thumbnail part itself is kept. Call
delete_part()if you want to remove it as well.
- flush() None[source]
Write the package to the underlying file or stream.
The stream is overwritten from its beginning and truncated afterwards.
The package is written even if it has not been modified.
- class aas_package3.Packaging[source]
Bases:
objectOpen and create AAS packages.
- create(path: str | PathLike[str]) PackageReadWrite[source]
Create an empty AAS package at the given
path.If the file already exists, it is overwritten. The empty package is immediately written to the file.
- Raise:
OSErrorif the file can not be written
- create_in_stream(stream: BinaryIO) PackageReadWrite[source]
Create an empty AAS package in the given
stream.The
streamneeds to be writable and seekable. The empty package is immediately written to it.The
streamis not closed by the package; you need to close it yourself.- Raise:
OSErrorif the stream can not be written or sought
- open_read(path: str | PathLike[str]) PackageRead[source]
Open the package at
pathfor reading.- Raise:
OSErrorif the file can not be read, andInvalidPackageErrorif the file is not a valid package
- open_read_from_stream(stream: BinaryIO) PackageRead[source]
Open the package in
streamfor reading.The whole
streamis read in memory. If the stream is seekable, it is read from its beginning. Thestreamis not closed.- Raise:
InvalidPackageErrorif the stream is not a valid package
- open_read_write(path: str | PathLike[str]) PackageReadWrite[source]
Open the package at
pathfor reading and writing.- Raise:
OSErrorif the file can not be read, andInvalidPackageErrorif the file is not a valid package
- open_read_write_from_stream(stream: BinaryIO) PackageReadWrite[source]
Open the package in
streamfor reading and writing.The whole
streamis read in memory. Thestreamneeds to be seekable and writable so thatPackageReadWrite.flush()can overwrite it. Thestreamis not closed.- Raise:
InvalidPackageErrorif the stream is not a valid package