snaphelpers

Helpers for interacting with the Snap system within a Snap.

snaphelpers.is_snap(environ=None)

Return whether running in a Snap.

Return type

bool

class snaphelpers.Snap(environ=None)

Top-level wrapper for a Snap.

config: SnapConfig = None
environ: SnapEnviron = None
health: SnapHealth = None
instance_name

Wrapper to get properties from a SanpEnviron instance.

metadata_files: SnapMetadataFiles = None
name

Wrapper to get properties from a SanpEnviron instance.

paths: SnapPaths = None
revision

Wrapper to get properties from a SanpEnviron instance.

services: SnapServices = None
version

Wrapper to get properties from a SanpEnviron instance.

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

Run the snapctl command.

config_get(*keys)

Return the snap config.

Parameters

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

Return type

Dict[str, Any]

config_set(configs)

Set snap configs.

Parameters

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

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

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 = None
returncode: int = None
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.

It raises UnknownConfigKey if the option doesn’t exist.

Return type

Any

get_options(*keys)

Return a SnapConfigOptions for the specified keys.

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.

Return type

Any

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.