---
title: Charmhub | Deploy Harness Extensions using Charmhub - The Open Operator Collection
description: Deploy the latest version of Harness Extensions on any cloud.
url: https://charmhub.io/harness-extensions/libraries/harness_ctx
---

# Harness Extensions

[Pietro Pasotti](https://charmhub.io/publisher/ppasotti "View all packages from Pietro Pasotti")

* [Pietro Pasotti](https://charmhub.io/publisher/ppasotti "View all packages from Pietro Pasotti")

Platform:

20.04

beta 3

```
juju deploy harness-extensions --channel beta
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/harness-extensions/libraries/harness_ctx#drawer)

## charms.harness\_extensions.v0.harness\_ctx

* [*Docstrings*Docstrings](https://charmhub.io/harness-extensions/libraries/harness_ctx)
  [*Code*Source code](https://charmhub.io/harness-extensions/libraries/harness_ctx/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.harness_extensions.v0.harness_ctx
    ```

    [Download harness\_ctx.py](https://charmhub.io/harness-extensions/libraries/harness_ctx/download)
  + *Last updated* 30 Jun 2022
  + *Revision* Library version 0.1

This is a library providing a sample implementation for more realistic Harness-event interactions.
First and foremost, it generates a single-use Harness that gets discarded after each event execution, thereby forcing a charm init at each event.
Secondly, it emits framework.commit() events after each hook, which Harness doesn't (yet).
This lib will likely become redundant once https://github.com/canonical/operator/issues/736 is solved; until then, this allows us to experiment with the concept.

Basic usage:

> > > class MyCharm(CharmBase):
> > > def **init**(self, framework: Framework, key: typing.Optional = None):
> > > super().**init**(framework, key)
> > > self.framework.observe(self.on.update\_status, self.\_listen)
> > > self.framework.observe(self.framework.on.commit, self.\_listen)
> > >
> > > ```
> > > def _listen(self, e):
> > >     self.event = e
> > > ```
> > >
> > > with HarnessCtx(MyCharm, "update-status") as h:
> > > event = h.emit()
> > > assert event.handle.kind == "update\_status"
> > >
> > > assert h.harness.charm.event.handle.kind == "commit"

---

Index

* [class Emitter](https://charmhub.io/harness-extensions/libraries/harness_ctx#emitter)
* + [def \_\_init\_\_(
    self,
    harness,
    emit)](https://charmhub.io/harness-extensions/libraries/harness_ctx#emitter-__init__)
* + [def emitted(
    self)](https://charmhub.io/harness-extensions/libraries/harness_ctx#emitter-emitted)
* + [def emit(
    self)](https://charmhub.io/harness-extensions/libraries/harness_ctx#emitter-emit)
* [class HarnessCtx](https://charmhub.io/harness-extensions/libraries/harness_ctx#harnessctx)
* + [def \_\_init\_\_(
    self,
    charm,
    event\_name,
    emitter)](https://charmhub.io/harness-extensions/libraries/harness_ctx#harnessctx-__init__)
* + [def \_\_enter\_\_(
    self)](https://charmhub.io/harness-extensions/libraries/harness_ctx#harnessctx-__enter__)
* + [def \_\_exit\_\_(
    self,
    exc\_type,
    exc\_val,
    exc\_tb)](https://charmhub.io/harness-extensions/libraries/harness_ctx#harnessctx-__exit__)

#### class Emitter

Description

Event emitter. None

Methods

Emitter.
\_\_init\_\_(

*self*

,
harness: Harness

,
emit
)

Emitter.
emitted(

*self*
)

Description

Has the event been emitted already? None

Emitter.
emit(

*self*
)

Emit the event.

Description

Will get called automatically when HarnessCtx exits if you didn't call it already.

#### class HarnessCtx

Harness-based context for emitting a single event.

Description

Example usage:

> > > class MyCharm(CharmBase):
> > > def **init**(self, framework: Framework, key: typing.Optional = None):
> > > super().**init**(framework, key)
> > > self.framework.observe(self.on.update\_status, self.\_listen)
> > > self.framework.observe(self.framework.on.commit, self.\_listen)
> > >
> > > ```
> > > def _listen(self, e):
> > >     self.event = e
> > > ```
> > >
> > > with HarnessCtx(MyCharm, "update-status") as h:
> > > event = h.emit()
> > > assert event.handle.kind == "update\_status"
> > >
> > > assert h.harness.charm.event.handle.kind == "commit"

Methods

HarnessCtx.
\_\_init\_\_(

*self*

,
charm

,
event\_name: str

,
emitter
)

HarnessCtx.
\_\_enter\_\_(

*self*
)

HarnessCtx.
\_\_exit\_\_(

*self*

,
exc\_type

,
exc\_val

,
exc\_tb
)
