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: Exception

Signal that the data does not represent a valid AAS package.

exception aas_package3.NoOriginError[source]

Bases: InvalidPackageError

Signal that the package lacks the origin part.

exception aas_package3.DanglingRelationshipError[source]

Bases: InvalidPackageError

Signal that a relationship points to a part which does not exist.

exception aas_package3.PartNotFoundError[source]

Bases: LookupError

Signal that the requested part does not exist in the package.

class aas_package3.Part(uri: str, content_type: str, content: bytes)[source]

Bases: object

Represent a part of an AAS package.

You obtain parts from a package, e.g., with PackageRead.specs() or PackageReadWrite.put_part(). Do not construct them yourself.

property uri: str

URI of the part within the package, e.g., /aasx/data.json.

property content_type: str

MIME type of the part.

stream() → BinaryIO[source]

Open a read stream over the content of the part.

read_all_bytes() → bytes[source]

Read the whole content of the part as bytes.

read_all_text(encoding: str = 'utf-8-sig') → str[source]

Read the content of the part as text.

By default, the content is decoded as UTF-8, and an optional byte order mark is stripped.

class aas_package3.SupplementaryRelationship(spec: Part, supplementary: Part)[source]

Bases: object

Represent a relationship between a spec and its supplementary part.

aas_package3.is_valid_part_uri(uri: str) → bool[source]

Check that uri can 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: object

Read from an AAS package.

Use Packaging to 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:

DanglingRelationshipError if 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:

DanglingRelationshipError if a spec part is missing

is_spec(part: Part) → bool[source]

Check that part is 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:

DanglingRelationshipError if a supplementary is missing

supplementary_relationships() → List[SupplementaryRelationship][source]

List all the supplementary relationships from all the specs.

Raise:

DanglingRelationshipError if 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:

PartNotFoundError if 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:

DanglingRelationshipError if the thumbnail relationship exists, but the part does not

class aas_package3.PackageReadWrite(path: Path | None, stream: BinaryIO | None, content: _Content)[source]

Bases: PackageRead

Read from and write to an AAS package.

The changes are kept in memory until you call flush() or close().

If you use the package as a context manager, it is closed, and hence flushed, when the with block finishes normally. If the with block 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 content to the package as a part with content_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() or relate_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 stream to the package as a part.

The stream is read from its current position till its end. See put_part() for more details.

Returns:

the written part

delete_part(part: Part) → None[source]

Remove the part from 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 part to 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 part as 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 supplementary to the spec as 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 supplementary and spec.

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 part to 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.

close() → None[source]

Flush the package if it has been modified, and close it.

Closing the package more than once has no effect.

The streams which you passed in are not closed, as they are owned by you.

Raise:

OSError if the file or the stream can not be written

discard() → None[source]

Close the package without flushing it.

The changes which have not been flushed yet are lost. Closing the package afterwards has no effect.

class aas_package3.Packaging[source]

Bases: object

Open 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:

OSError if the file can not be written

create_in_stream(stream: BinaryIO) → PackageReadWrite[source]

Create an empty AAS package in the given stream.

The stream needs to be writable and seekable. The empty package is immediately written to it.

The stream is not closed by the package; you need to close it yourself.

Raise:

OSError if the stream can not be written or sought

open_read(path: str | PathLike[str]) → PackageRead[source]

Open the package at path for reading.

Raise:

OSError if the file can not be read, and InvalidPackageError if the file is not a valid package

open_read_from_stream(stream: BinaryIO) → PackageRead[source]

Open the package in stream for reading.

The whole stream is read in memory. If the stream is seekable, it is read from its beginning. The stream is not closed.

Raise:

InvalidPackageError if the stream is not a valid package

open_read_write(path: str | PathLike[str]) → PackageReadWrite[source]

Open the package at path for reading and writing.

Raise:

OSError if the file can not be read, and InvalidPackageError if the file is not a valid package

open_read_write_from_stream(stream: BinaryIO) → PackageReadWrite[source]

Open the package in stream for reading and writing.

The whole stream is read in memory. The stream needs to be seekable and writable so that PackageReadWrite.flush() can overwrite it. The stream is not closed.

Raise:

InvalidPackageError if the stream is not a valid package