snaphelpers

Helpers for interacting with the Snap system within a Snap.

class snaphelpers.Snap(environ=None)

Top-level wrapper for a Snap.

config: snaphelpers._conf.SnapConfig
environ: snaphelpers._env.SnapEnviron
health: snaphelpers._health.SnapHealth
instance_name

Wrapper to get properties from a SanpEnviron instance.

metadata_files: snaphelpers._meta.SnapMetadataFiles
name

Wrapper to get properties from a SanpEnviron instance.

paths: snaphelpers._path.SnapPaths
revision

Wrapper to get properties from a SanpEnviron instance.

services: snaphelpers._service.SnapServices
version

Wrapper to get properties from a SanpEnviron instance.

class snaphelpers.SnapConfig(snapctl=None)

Interact with the snap configuration.

It allows getting and setting configuration options.

get(key)

Return value for a single key.

Parameters

key (str) – key to get config for, possibly with dotted notation.

Raises

UnknownConfigKey – if the option doesn’t exist.

Return type

Any

get_options(*keys)

Return a SnapConfigOptions for the specified keys.

Parameters

keys (str) – keys to read configuration for.

Return type

SnapConfigOptions

set(options)

Set config options.

Parameters

options (Dict[str, Any]) – a dict with configs. Keys can use dotted notation.

class snaphelpers.SnapConfigOptions(keys, snapctl=None)

Allow accessing a set of Snap config options with a dict-like interface.

Nested keys can be accessed using dotted notation:

config['foo.bar.baz']
Parameters

keys (Sequence[str]) – the top-level configuration keys.

as_dict()

Return the configuration as a dict.

Return type

Dict[str, Any]

fetch()

Fetch (or refresh) configuration for the set of keys.

get(key, default=None)

Return value for a key, with a default.

Parameters
  • key (str) – name of the key to return.

  • default (Optional[Any]) – value to return if the key is not found.

Return type

Any

class snaphelpers.SnapCtl(executable='/usr/bin/snapctl', env=None)

Run the snapctl command.

config_get(*keys)

Return the snap configuration.

Parameters

keys (str) – a list of config keys to return.

Return type

Dict[str, Any]

config_set(configs)

Set snap configuration.

Parameters

configs (Dict[str, Any]) – a dict with configs. Keys can use dotted notation.

config_unset(*keys)

Unset snap configuration keys.

Parameters

keys (str) – config keys to unset.

connection_set(name, configs)

Set plug or slot configuration.

Parameters
  • name (str) – the plug/slot name.

  • configs (Dict[str, Any]) – a dict with configs. Keys can use dotted notation.

connection_unset(name, *keys)

Unset plug or slot configuration.

Parameters
  • name (str) – the plug/slot name.

  • keys (str) – keys to unset. Dotted notation can be used.

is_connected(name)

Return whether a plug or slot is connected.

Parameters

name (str) – the plug or slot name.

Return type

bool

plug_get(name, *keys, remote=False)

Return plug configuration.

Parameters
  • name (str) – the plug name.

  • keys (str) – a list of config keys to return.

  • remote (bool) – if True, return configs from the remote end.

Return type

Dict[str, Any]

restart(*services, reload=False)

Restart all or specified services in the snap.

Parameters
  • services (str) – a list of services defined in the snap to restart. If not specified, all services will be restarted.

  • reload (bool) – whether to reload services if supported.

run(*args)

Execute the command and return its output.

Parameters

args (str) – command args.

Return type

str

services(*services)

Return info about services in the snap.

Parameters

services (str) – a list of services to return info for. If not specified, all services are returned.

Return type

List[ServiceInfo]

set_health(status, message=None, code=None)

Set snap health.

Parameters
  • status (SnapHealthStatus) – the status to set

  • message (Optional[str]) – an optional message string

  • code (Optional[str]) – an optional code string

slot_get(name, *keys, remote=False)

Return slot configuration.

Parameters
  • name (str) – the slot name.

  • keys (str) – a list of config keys to return.

  • remote (bool) – if True, return configs from the remote end.

Return type

Dict[str, Any]

start(*services, enable=False)

Start all or specified services in the snap.

Parameters
  • services (str) – a list of services defined in the snap to start. If not specified, all services will be started.

  • enable (bool) – whether to also enable services at startup.

stop(*services, disable=False)

Stop all or specified services in the snap.

Parameters
  • services (str) – a list of services defined in the snap to stop. If not specified, all services will be stopped.

  • disable (bool) – whether to also disable services at startup.

exception snaphelpers.SnapCtlError(process)

A snapctl command failed.

error: str
returncode: int
class snaphelpers.SnapEnviron(environ=None)

Environment variables related to the Snap.

This provides read-only access to environment variables starting with SNAP_.

These can be accessed either as a dict or as attributes, without the SNAP_ prefix. E.g.:

env = SnapEnviron()
env.NAME     # -> 'mysnap'
env['NAME']  # -> 'mysnap'

Note: The SNAP environment variable is also included.

class snaphelpers.SnapHealth(snapctl=None)

Snap health.

This provides an interfaces for setting health for the snap.

blocked(message, code=None)

Set the status of the snap to “blocked”.

Parameters
  • message (str) – a message string for the status

  • code (Optional[str]) – an optional code string

error(message, code=None)

Set the status of the snap to “error”.

Parameters
  • message (str) – a message string for the status

  • code (Optional[str]) – an optional code string

okay()

Set the status of the snap to “okay”.

waiting(message, code=None)

Set the status of the snap to “waiting”.

Parameters
  • message (str) – a message string for the status

  • code (Optional[str]) – an optional code string

class snaphelpers.SnapPaths(env=None)

Paths related to the snap.

common

the SNAP_COMMON path

data

the SNAP_DATA path

snap

the SNAP path

user_common

the SNAP_USER_COMMON path

user_data

the SNAP_USER_DATA path

class snaphelpers.SnapServices(snapctl=None)

Manage services in the snap.

list()

Return services by name.

Return type

Dict[str, SnapService]

restart(reload=False)

Restart all services.

Parameters

reload (bool) – whether to reload services if supported.

start(enable=False)

Start all services.

Parameters

enable (bool) – whether to also enable services at startup.

stop(disable=False)

Stop all services.

Parameters

disable (bool) – whether to also disable services at startup.

snaphelpers.is_snap(environ=None)

Return whether running in a Snap.

Return type

bool