Skip to main content

Python CNAB Library

Work-in-progress library for working with CNAB in Python.

There are probably three main areas of interest for a CNAB client:

  1. Handling the bundle.json format (101)
  2. Building invocation images (102)
  3. Running actions against a CNAB (103)

Claims and Signing are optional but will be worked on once the above are stable.

Installation

The module is published on PyPi and can be installed from there.

pip install cnab

Parsing bundle.json

Nothing too fancy here, the Bundle class has a from_dict static method which builds a full Bundle object.

import json
from cnab import Bundle

with open("bundle.json") as f:
    data = json.load(f)

bundle = Bundle.from_dict(data)

This could for example be used for validation purposes, or for building user interfaces for bundle.json files.

Describing bundle.json in Python

You can also describe the bundle.json file in Python. This will correctly validate the structure based on the current specification and would allow for building a custom DSL or other user interface for generating bundle.json files.

from cnab import Bundle, InvocationImage

bundle = Bundle(
    name="hello",
    version="0.1.0",
    invocation_images=[
        InvocationImage(
            image_type="docker",
            image="technosophos/helloworld:0.1.0",
            digest="sha256:aaaaaaa...",
        )
    ],
)

print(bundle.to_json())

Running CNABs

The module supports running actions on a CNAB, using the docker driver.

from cnab import CNAB

# The first argument can be a path to a bundle.json file, a dictionary
# or a full `Bundle` object
app = CNAB("fixtures/helloworld/bundle.json")

# list available actions
print(app.actions)

# list available parameters
print(app.parameter)

# run the install action
print(app.run("install"))

# run the install action specifying a parameters
print(app.run("install", parameters={"port": 9090}))

# Many applications will require credentials
app = CNAB("fixtures/hellohelm/bundle.json")

# list required credentials
print(app.credentials)

# Here we pass the value for the required credential
# in this case by reading the existing configuration from disk
with open("/home/garethr/.kube/config") as f:
    print(app.run("status", credentials={"kubeconfig": f.read()}))

Note that error handling for this is very work-in-progress.

Working with invocation images

pycnab also has a class for working with invocation images.

from cnab import CNABDirectory

directory = CNABDirectory("fixtures/invocationimage")

# Check whether the directory is valid
# Raises `InvalidCNABDirectory` exception if invalid
directory.valid()

# Returns the text of the associated README file if present
directory.readme()

# Returns the text of the associated LICENSE file if present
directory.license()

Thanks

Thanks to QuickType for bootstrapping the creation of the Python code for manipulating bundle.json based on the current JSON Schema.

Metadata

Release files for cnab 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cnab 0.1.7
File Size Uploaded Provenance
cnab-0.1.7.tar.gz 10.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cnab 0.1.7
File Interpreter ABI Platform
cnab-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 48.1 kB

Release files / cnab-0.1.7.tar.gz

Download URL cnab-0.1.7.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
031a6361eaec6a6c4f6c04d5f43d3d57d97221683a14cb223bf592a9022c71b9
BLAKE2b-256 checksum
How to use checksums
313b412323db6748c52fcbbf8f6c58070c367fa25a696bbada77a1e329c688ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.10 CPython/3.7.2 Linux/4.18.0-15-generic

Release files / cnab-0.1.7-py3-none-any.whl

Download URL cnab-0.1.7-py3-none-any.whl
Size 37.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59bd1f60905a4e5133d2adc4dac2bd917e4af09a73eb3c260ee64286c6a42c90
BLAKE2b-256 checksum
How to use checksums
52451da9b50443e2bfeefe65c03364a58ca19da646bdfbb27d92d17e901dde4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.10 CPython/3.7.2 Linux/4.18.0-15-generic

Security for cnab 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page