seagrid.config#

Configuration management for seagrid.

Settings are handled by Dynaconf in the same way as in tracmass. The package template TEMPLATE (settings_template.toml) is always loaded first and gives the defaults. It is followed by these files, in order of increasing priority (missing files are skipped):

  1. Global settings: /etc/seagrid/seagrid.toml

  2. User settings: ~/.config/seagrid/seagrid.toml

  3. Current directory settings: ./seagrid.toml

  4. Any sg_*.toml file in the current directory, sorted by filename

  5. The file named by the environment variable SEAGRID_SETTINGS_FILE_FOR_DYNACONF

Each of the first three directories may also hold a .secrets.toml for credentials, which must never be committed to version control.

Every [section] is a Dynaconf environment. [default] applies to all of them, and every data source reads its own section ([ostia], [modis_a], …) with source_settings(). Environment variables prefixed with SEAGRID_ override single keys, e.g. SEAGRID_BASEDATADIR=/data/seagrid.

ivar USER_DIR:

User configuration directory, ~/.config/seagrid.

vartype USER_DIR:

pathlib.Path

ivar GLOB_DIR:

Machine-wide configuration directory, /etc/seagrid.

vartype GLOB_DIR:

pathlib.Path

ivar CURR_DIR:

The working directory when seagrid was imported.

vartype CURR_DIR:

pathlib.Path

ivar TEMPLATE:

The commented settings template shipped with the package.

vartype TEMPLATE:

pathlib.Path

ivar settings_files:

Every settings file checked, in loading order (after TEMPLATE).

vartype settings_files:

list of pathlib.Path

ivar settings:

The settings object.

vartype settings:

dynaconf.Dynaconf

Examples

>>> from seagrid import config
>>> config.source_settings("ostia")["doi"]
'10.48670/moi-00168'

Module Attributes

EXTRA_FILE_ENVVAR

Name of the environment variable naming an extra settings file.

DYNACONF_KEYS

Dynaconf's own entries in a settings object, not from any settings file.

CREDENTIAL_KEYS

Credential keys, accepted in any section (they belong in a .secrets.toml).

OPTIONAL_KEYS

Keys any section may set that the template does not show.

Functions

change_env(new_env)

Change the active environment of settings.

check_settings([name, warn])

Find settings keys that seagrid does not know.

init([path, overwrite])

Write the commented settings template to a file.

source_names()

Return the names of all data-source sections in the template.

source_settings(name)

Return the settings of one data source.

seagrid.config.CREDENTIAL_KEYS = frozenset({'appkey', 'password', 'token', 'username'})#

Credential keys, accepted in any section (they belong in a .secrets.toml).

seagrid.config.DYNACONF_KEYS = frozenset({'environments', 'envvar_prefix', 'load_dotenv'})#

Dynaconf’s own entries in a settings object, not from any settings file.

seagrid.config.change_env(new_env)[source]#

Change the active environment of settings.

Parameters:

new_env (str) – The environment (section) to switch to.

Return type:

None

Notes

Data sources read their own section with source_settings() and are not affected by the active environment.

seagrid.config.check_settings(name=None, *, warn=True)[source]#

Find settings keys that seagrid does not know.

A misspelt key does nothing, silently, because the default from the template is used instead. This compares the keys of each section with the keys the template defines for that section and for [default]. Keys listed in a section’s extra_keys are accepted too.

Parameters:
  • name (str, optional) – Section to check. Default: every section in the template.

  • warn (bool, optional) – Issue a UserWarning for each unknown key. Default True.

Returns:

list of str – Unknown keys as "section.key".

Warns:

UserWarning – For each unknown key, if warn is True.

Return type:

list[str]

seagrid.config.init(path='seagrid.toml', overwrite=False)[source]#

Write the commented settings template to a file.

Parameters:
  • path (str or os.PathLike, optional) – File to write (a .toml name), or a directory to write seagrid.toml into. ~ is expanded and missing directories are created. Default "seagrid.toml" in the current directory. Use "~/.config/seagrid" (USER_DIR) for settings that apply to every project.

  • overwrite (bool, optional) – Replace an existing file. Default False.

Returns:

pathlib.Path – The file written.

Raises:

FileExistsError – If the file exists and overwrite is False.

Return type:

Path

Examples

>>> import seagrid
>>> seagrid.config.init("~/.config/seagrid")
PosixPath('/home/user/.config/seagrid/seagrid.toml')
seagrid.config.source_names()[source]#

Return the names of all data-source sections in the template.

Returns:

list of str – Section names (Dynaconf environments) other than default, in the order they appear in TEMPLATE.

Return type:

list[str]

Examples

>>> "ostia" in source_names()
True
seagrid.config.source_settings(name)[source]#

Return the settings of one data source.

The result is an independent Dynaconf object for environment name: the [default] keys overlaid with the keys of [name] from every settings file. Changing its active environment does not affect settings or any other source.

Parameters:

name (str) – Section (environment) name, e.g. "ostia" or "modis_a".

Returns:

dynaconf.LazySettings – The settings of name. Read keys with s["key"] or s.get("key", default).

Return type:

Any

Examples

>>> source_settings("acspo")["host"]
'https://coastwatch.noaa.gov'

Notes

Dynaconf caches one object per environment. The cache is cleared when a SEAGRID_* environment variable or a settings file changes, so such changes take effect at the next call.