ssb_timeseries.config

Configurations for the SSB timeseries library.

An environment variable ENV_VAR_NAME is expected to point to a JSON file with configurations. If these exist, they will be loaded and put into a Config object when the configuration module is loaded.

In most cases, this would happen behind the scene when ssb_timeseries.dataset or ssb_timeseries.catalog are imported.

Directly accessing the configuration module should only be required when manipulating configurations from Python code.

Example

>>> from ssb_timeseries.config import Config
>>>
>>> cfg = Config.active()

… modify, eg. disable library logging (leave the responsibility to the application using it): >>> cfg.logging = {}

>>>
>>> cfg.save()
>>>
>>> cfg.activate()

For switching between preset configurations, use the timeseries-config command from a terminal:

poetry run timeseries-config <option>

which is equivalent to:

python ./config.py <option>

See ssb_timeseries.config.main() for details on the named options.

class Config(**kwargs)

Bases: object

Configuration for reading, modifying, saving, and activating timeseries configurations.

A configuration can be loaded from a specified file, from the file identified by ENV_VAR_NAME, or from the default configuration preset. Configuration values can also be provided or overridden as keyword arguments.

A newly created configuration exists only in memory. Use save() to persist it to a file and activate() to make it the active configuration. The active configuration can be retrieved with active() or reloaded from its file with refresh().

__eq__(other)

Equality test.

Return type:

bool

Parameters:

other (Self | dict)

__getitem__(item)

Get the value of a configuration.

Return type:

Optional[typing.Any]

Parameters:

item (str)

__init__(**kwargs)

Initialize Config object from keyword arguments.

Keyword Arguments:
  • preset (str) – Optional. Name of a preset configuration. If provided, the preset configuration is loaded, and no other parameters are considered.

  • configuration_file (str) – Path to the configuration file. If the parameter is not provided, the environment variable ENV_VAR_NAME is used. If the environment variable is not set, the default configuration file location is used.

  • repositories (list[FileBasedRepository]) – New in version 0.5.0. Replaces bucket, timeseries_root and catalog.

  • log_file (str) – Path to the log file.

  • bucket (str) – Name of the GCS bucket.

  • ignore_file (bool)

Raises:
  • FileNotFoundError – If the configuration file as implied by provided or not provided parameters does not exist. # noqa: DAR402

  • ValidationError – If the resulting configuration is not valid. # noqa: DAR402

  • EnvVarNotDefinedeError – If the environment variable ENV_VAR_NAME is not defined.

Return type:

None

Examples

To load an existing preset configuration:

>>> from ssb_timeseries.config import Config
>>> config = Config(preset='defaults')

… or (specific to Statistics Norway and Dapla):

>>> from ssb_timeseries.config import Config
>>> config = Config(preset='daplalab')
__str__()

Return timeseries configurations as JSON string.

Return type:

str

activate()

Update the process wide active in-memory configuration, and if its configuration file exists, update the environment variable ENV_VAR_NAME to point to that file.

Note that this does not save the file. See .save().

Return type:

Self

classmethod active()

Return the (in-memory) active configuration.

This does not read from file, use .refresh() to reload it.

Return type:

Config

apply(configuration)

Set configuration values from a dictionary.

Return type:

None

Parameters:

configuration (dict)

configuration_file: PathStr

The path to the configuRation file.

io_handlers: dict[str, Any]

IO handlers for repository, snapshotts and sharing.

property is_valid: bool

Check if the configuration has all required fields.

property log_file: str

Get file name from logging configuration, if a file based log handler is defined.

logging: dict[str, Any]

Logging configuration as a valid logging.dictConfig.

classmethod refresh()

Reload the configuration from the file identified by ENV_VAR_NAME, activate it and return it.

Return type:

Self

repositories: dict[str, Repository]

Defines storage locations for time series data and metadata.

save(path='')

Saves configurations to the JSON file defined by path or configuration_file.

If path is provided, it takes presence and configuration_file will be updated accordingly.

Note that .save() does not activate the configuration instance. Use .activate() to make it the active configuration, or .refresh() to reload the active configuration from its file.

Parameters:

path (PathStr) – Full path of the JSON file to save to. If not specified, it will attempt to use the environment variable ENV_VAR_NAME before falling back to the default location $HOME/.config/ssb_timeseries/timeseries_config.json.

Raises:

ValueError – If path is not provided and configuration_file is not set.

Return type:

None

sharing: dict[str, Repository]

Defines the storage locations for shared data.

snapshots: dict[str, Repository]

Defines the storage locations for persisting (archiving) data in stable states.