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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file charmlibs_pathops-1.2.0.tar.gz.
File metadata
- Download URL: charmlibs_pathops-1.2.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9c4594a42b9bb7b2e4a8672fcc4398fbb6153b15d21b0f9bdbc8bf3e737cf06
|
|
| MD5 |
0862090c7c33ec59aef206daebc5c4af
|
|
| BLAKE2b-256 |
d29d30ca0d94ce5d72362637e44170fc8bde5ded21839d2d2aa1ca65ef313af5
|
Provenance
The following attestation bundles were made for charmlibs_pathops-1.2.0.tar.gz:
Publisher:
publish.yaml on canonical/charmlibs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
charmlibs_pathops-1.2.0.tar.gz -
Subject digest:
e9c4594a42b9bb7b2e4a8672fcc4398fbb6153b15d21b0f9bdbc8bf3e737cf06 - Sigstore transparency entry: 483184511
- Sigstore integration time:
-
Permalink:
canonical/charmlibs@7c79c8c2b8f93d109a87649cbe30100bfc885212 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7c79c8c2b8f93d109a87649cbe30100bfc885212 -
Trigger Event:
push
-
Statement type:
File details
Details for the file charmlibs_pathops-1.2.0-py3-none-any.whl.
File metadata
- Download URL: charmlibs_pathops-1.2.0-py3-none-any.whl
- Upload date:
- Size: 27.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86d2b7e81cfac63222f0b9c9bc933024303ec1212debd3305d0179667dfd9806
|
|
| MD5 |
543017bf4f7e5bdbe3909c13567c6a75
|
|
| BLAKE2b-256 |
c9ff03db3fe17f78b71f8559aa30b098d1f5dc81e73e83f3c1498704142717e1
|
Provenance
The following attestation bundles were made for charmlibs_pathops-1.2.0-py3-none-any.whl:
Publisher:
publish.yaml on canonical/charmlibs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
charmlibs_pathops-1.2.0-py3-none-any.whl -
Subject digest:
86d2b7e81cfac63222f0b9c9bc933024303ec1212debd3305d0179667dfd9806 - Sigstore transparency entry: 483184533
- Sigstore integration time:
-
Permalink:
canonical/charmlibs@7c79c8c2b8f93d109a87649cbe30100bfc885212 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7c79c8c2b8f93d109a87649cbe30100bfc885212 -
Trigger Event:
push
-
Statement type: