charms.reactive.helpers

Summary

all_states Assert that all desired_states are active
any_file_changed Check if any of the given files have changed since the last time this was called.
any_hook Assert that the currently executing hook matches one of the given patterns.
any_states Assert that any of the desired_states are active
data_changed Check if the given set of data has changed since the previous call.
is_state Assert that a desired_state is active
mark_invoked Mark the given ID as having been invoked, for use with was_invoked().
toggle_state Helper that calls either set_state() or remove_state(), depending on the value of should_set.
was_invoked Returns whether the given ID has been invoked before, as per mark_invoked().

Reference

charms.reactive.helpers.all_states(*desired_states)

Assert that all desired_states are active

charms.reactive.helpers.any_file_changed(filenames, hash_type='md5')

Check if any of the given files have changed since the last time this was called.

Parameters:
  • filenames (list) – Names of files to check. Accepts callables returning the filename.
  • hash_type (str) – Algorithm to use to check the files.
charms.reactive.helpers.any_hook(*hook_patterns)

Assert that the currently executing hook matches one of the given patterns.

Each pattern will match one or more hooks, and can use the following special syntax:

  • db-relation-{joined,changed} can be used to match multiple hooks (in this case, db-relation-joined and db-relation-changed).
  • {provides:mysql}-relation-joined can be used to match a relation hook by the role and interface instead of the relation name. The role must be one of provides, requires, or peer.
  • The previous two can be combined, of course: {provides:mysql}-relation-{joined,changed}
charms.reactive.helpers.any_states(*desired_states)

Assert that any of the desired_states are active

charms.reactive.helpers.data_changed(data_id, data, hash_type='md5')

Check if the given set of data has changed since the previous call.

This works by hashing the JSON-serialization of the data. Note that, while the data will be serialized using sort_keys=True, some types of data structures, such as sets, may lead to false positivies.

Parameters:
  • data_id (str) – Unique identifier for this set of data.
  • data – JSON-serializable data.
  • hash_type (str) – Any hash algorithm supported by hashlib.
charms.reactive.helpers.is_state(desired_state)

Assert that a desired_state is active

charms.reactive.helpers.mark_invoked(invocation_id)

Mark the given ID as having been invoked, for use with was_invoked().

charms.reactive.helpers.toggle_state(state, should_set)

Helper that calls either set_state() or remove_state(), depending on the value of should_set.

Equivalent to:

if should_set:
    set_state(state)
else:
    remove_state(state)
Parameters:
  • state (str) – Name of state to toggle.
  • should_set (bool) – Whether to set the state, or remove it.
charms.reactive.helpers.was_invoked(invocation_id)

Returns whether the given ID has been invoked before, as per mark_invoked().

This is useful for ensuring that a given block only runs one time:

def foo():
    if was_invoked('foo'):
        return
    do_something()
    mark_invoked('foo')

This is also available as a decorator at only_once().