Skip to main content

A pathlib-like interface for Juju K8s charms to interact with files in their workload container.

Project description

charmlibs-pathops

pathops provides a pathlib-like interface for local filesystem and workload container paths for Juju Charms.

To install, add charmlibs-pathops to your requirements. Then in your Python code, import as:

from charmlibs import pathops

Check out the reference docs on the charmlibs docsite.

Getting started

To get started, you can create an attribute self.root with the root directory for the local or remote paths you'll be working with.

import ops
from charmlibs import pathops

class KubernetesCharm(ops.CharmBase):
    def __init__(self, framework: ops.Framework):
        super().__init__(framework)
        container = self.unit.get_container('container-name')
        self.root = pathops.ContainerPath('/', container=container)

# or

class MachineCharm(ops.CharmBase):
    def __init__(self, framework: ops.Framework):
        super().__init__(framework)
        self.root = pathops.LocalPath('/')

You can then write code that could be used by either charm, using the pathops.PathProtocol type, which describes the operations supported by both local and container paths.

CONFIG_FILE_LOCATION = '/foo/bar'
CONFIG_FILE_CONTENTS = '...'

def write_config_file(root: pathops.PathProtocol) -> None:
    path = root / CONFIG_FILE_LOCATION
    path.write_text(CONFIG_FILE_CONTENTS)

For a Kubernetes charm, this also allows you to use the same API to work with paths in the charm container or the workload container.

import ops
from charmlibs import pathops

class KubernetesCharm(ops.CharmBase):
    def __init__(self, framework: ops.Framework):
        super().__init__(framework)
        self.charm_root = pathops.LocalPath('/')
        container = self.unit.get_container('container-name')
        self.workload_root = pathops.ContainerPath('/', container=container)
        ...

    def copy_config(self):
        source = self.charm_root / 'foo/bar'
        dest = self.workload_root / 'foo/bar/baz'
        dest.parent.mkdir(parents=True, exist_ok=True)
        dest.write_bytes(source.read_bytes())

pathops also provides a helper function for ensuring that a file is present with specified content, which returns whether changes were made.

import ops
from charmlibs import pathops

class KubernetesCharm(ops.CharmBase):
    ...

    def copy_config(self):
        changed = pathops.ensure_contents(
            path=self.workload_root / 'foo/bar/baz',
            source=(self.charm_root / 'foo/bar').read_bytes(),
        )
        if changed:
            # make the workload reload its configuration
            ...

pathops.PathProtocol provides a subset of the pathlib.Path API. PathProtocol doesn't support relative paths, open, or manipulating hardlinks and symlinks.

pathops doesn't provide a separate chmod method, as Pebble doesn't currently support this. Instead, mkdir, write_bytes, and write_text have arguments mode, user, and group to set directory or file permissions and ownership. ensure_contents also has these arguments.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

charmlibs_pathops-1.2.0.post0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

charmlibs_pathops-1.2.0.post0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file charmlibs_pathops-1.2.0.post0.tar.gz.

File metadata

  • Download URL: charmlibs_pathops-1.2.0.post0.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for charmlibs_pathops-1.2.0.post0.tar.gz
Algorithm Hash digest
SHA256 692c38688bbd54c00009c0a9562750cd39fa24a80d04be013554f6d9aabd35b8
MD5 3a4abeeb9aeeda10c7774c6c6d2554c5
BLAKE2b-256 5c9660f4f5dbc4a959297b6b36999d6afdd1d86224d4563e4b3a9a52214646db

See more details on using hashes here.

Provenance

The following attestation bundles were made for charmlibs_pathops-1.2.0.post0.tar.gz:

Publisher: publish.yaml on canonical/charmlibs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file charmlibs_pathops-1.2.0.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for charmlibs_pathops-1.2.0.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c9dac4d37c248ec587759e1b5aafa3ba1a6a8818cbbcf38d7c44c2af84deaac
MD5 5f2fbae07af82287f32088c70a24eb84
BLAKE2b-256 4c7069a777d086166bacea085de8edd4cf815bcea17edf9b7f8c71a84026f804

See more details on using hashes here.

Provenance

The following attestation bundles were made for charmlibs_pathops-1.2.0.post0-py3-none-any.whl:

Publisher: publish.yaml on canonical/charmlibs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page