Getting Started
Installation
Install the library with pip:
pip3 install aas-package3
The library requires Python 3.10 or later, and has no further dependencies.
Concepts
An AASX package is a ZIP archive following the Open Packaging Conventions.
The package consists of parts, each with its URI within the package (e.g., /aasx/data.json) and a MIME content type.
The parts are tied together with relationships:
The origin is the entry point of the package. It is created for you.
The specs are related to the origin and contain the AAS environments serialized as JSON or XML.
The supplementaries are related to a spec, and hold the files that the spec refers to, such as manuals or images.
The thumbnail is related to the package as a whole.
Entry Point
You open and create the packages through aas_package3.Packaging:
import aas_package3
packaging = aas_package3.Packaging()
Packaging gives you either a aas_package3.PackageRead, which only reads the package, or a aas_package3.PackageReadWrite, which also writes to it.
Both can be used as context managers.
Create a Package
The whole package lives in memory.
The changes are written to the file when the with block finishes (see Saving the Changes).
with packaging.create("some-file.aasx") as pkg:
spec = pkg.put_part(
"/aasx/some-company/data.json",
"application/json",
b'{"assetAdministrationShells": []}',
)
pkg.make_spec(spec)
supplementary = pkg.put_part(
"/aasx-suppl/some-company/some-manual.pdf", "application/pdf", b"%PDF"
)
pkg.relate_supplementary_to_spec(supplementary, spec)
thumbnail = pkg.put_part("/some-thumbnail.png", "image/png", b"PNG")
pkg.set_thumbnail(thumbnail)
Read a Package
with packaging.open_read("some-file.aasx") as pkg:
for content_type, specs in pkg.specs_by_content_type().items():
for spec in specs:
print(content_type, spec.uri, spec.read_all_text())
for relationship in pkg.supplementary_relationships():
print(relationship.spec.uri, "->", relationship.supplementary.uri)
thumbnail = pkg.thumbnail()
if thumbnail is not None:
print("Thumbnail:", thumbnail.uri)
application/json /aasx/some-company/data.json {"assetAdministrationShells": []}
/aasx/some-company/data.json -> /aasx-suppl/some-company/some-manual.pdf
Thumbnail: /some-thumbnail.png
To look up a part by its URI, use aas_package3.PackageRead.find_part() (returns None if missing) or aas_package3.PackageRead.must_part() (raises aas_package3.PartNotFoundError).
The URIs are compared case-insensitively and percent-decoded, as prescribed by the Open Packaging Conventions:
with packaging.open_read("some-file.aasx") as pkg:
part = pkg.must_part("/AASX-SUPPL/some-company/some-manual.pdf")
print(part.uri, part.content_type)
/aasx-suppl/some-company/some-manual.pdf application/pdf
Modify a Package
with packaging.open_read_write("some-file.aasx") as pkg:
spec = pkg.must_part("/aasx/some-company/data.json")
supplementary = pkg.must_part("/aasx-suppl/some-company/some-manual.pdf")
# Remove the relationship, and then the part itself.
pkg.unrelate_supplementary_from_spec(supplementary, spec)
pkg.delete_part(supplementary)
# Overwrite the spec in place. The relationships are kept.
pkg.put_part(spec.uri, "application/json", b'{"submodels": []}')
with packaging.open_read("some-file.aasx") as pkg:
print([spec.read_all_text() for spec in pkg.specs()])
print(pkg.supplementary_relationships())
['{"submodels": []}']
[]
Note that aas_package3.PackageReadWrite.delete_part() removes all the relationships from and to the part as well, so you can not end up with a dangling relationship.
Streams
Instead of files, you can work with binary streams such as io.BytesIO:
import io
stream = io.BytesIO()
with packaging.create_in_stream(stream) as pkg:
pkg.make_spec(pkg.put_part("/aasx/data.xml", "text/xml", b"<environment/>"))
with packaging.open_read_from_stream(stream) as pkg:
print([spec.uri for spec in pkg.specs()])
['/aasx/data.xml']
The streams are owned by you: the packages never close them.
Saving the Changes
The changes are kept in memory, and written to the file or the stream when:
the
withblock finishes normally,you call
aas_package3.PackageReadWrite.close(), oryou call
aas_package3.PackageReadWrite.flush()explicitly, e.g., to save the intermediate state.
The package is only written on closing if it has been modified, so opening a file for writing and closing it without changes leaves the file as it is.
If the with block raises an exception, the changes are discarded so that you do not end up with a half-modified package:
try:
with packaging.open_read_write("some-file.aasx") as pkg:
pkg.unmake_spec(pkg.must_part("/aasx/some-company/data.json"))
raise RuntimeError("Something went wrong")
except RuntimeError:
pass
with packaging.open_read("some-file.aasx") as pkg:
print([spec.uri for spec in pkg.specs()])
['/aasx/some-company/data.json']
If you do not use the with statement, call aas_package3.PackageReadWrite.discard() to drop the changes explicitly.
The changes are not written if you neither close nor flush the package, e.g., when it is merely garbage-collected.
Errors
Invalid packages raise aas_package3.InvalidPackageError or one of its subclasses, while I/O errors raise the usual OSError:
try:
packaging.open_read_from_stream(io.BytesIO(b"not a package"))
except aas_package3.InvalidPackageError as exception:
print(type(exception).__name__)
InvalidPackageError
The misuse of the API, such as making a non-existing part a spec, is caught by assert statements, unless you run Python with -O.