Skip to main content

sasvi.settings

Resolve the SASVI_* connection settings and say where each came from.

resolve_settings is the one place that decides which tenant, which TLS setting and which credential a client gets. sasvi.SVIClient.from_env calls it, and so does every sasvi command, so the two cannot disagree.

Precedence, highest first:

  1. explicit overrides (keyword arguments, command-line options);
  2. the process environment;
  3. the keys of a .env file, when one is named;
  4. a named profile from profiles.toml (see sasvi.profiles);
  5. the sasvi auth login cache, when asked for. It supplies the credential (and the base URL and TLS setting when nothing above sets them), and only for the origin the login was made for.

The SDK reads a .env file, a profile or the login cache only when the caller asks for it. With no options the result is the SASVI_* part of the process environment, which is what from_env has always read. Nothing here writes to os.environ.

SettingSource Objects​

class SettingSource(StrEnum)

Where an effective setting came from.

OVERRIDE​

ENVIRONMENT​

ENV_FILE​

PROFILE​

LOGIN_CACHE​

SECRET_SETTINGS​

Settings whose value is a credential. ResolvedSettings.redacted hides them.

LoginCacheDecision Objects​

@dataclass(frozen=True)
class LoginCacheDecision()

What the sasvi auth login cache contributed, and why.

Arguments:

  • reason - One of used; not-requested (the caller did not ask for the cache); token-preset (a higher layer sets SASVI_ACCESS_TOKEN); no-cache; unreadable (the file exists but does not load); expired; or origin-mismatch (the base URL in use is not the one the login was made for).
  • supplied_keys - The SASVI_* settings the cache supplied.
  • cached_base_url - The origin of the cached login, for used and origin-mismatch.
  • path - The cache file that was consulted, or None when none was.

reason​

supplied_keys​

cached_base_url​

path​

used​

@property
def used() -> bool

Whether the cached login supplies the credential.

ResolvedSettings Objects​

@dataclass(frozen=True)
class ResolvedSettings()

The effective SASVI_* settings and the source of each.

Arguments:

  • values - Setting name to value. It holds credentials, so it is left out of repr; use redacted to show it.
  • sources - Setting name to the layer that supplied it.
  • env_file - The .env file that was read, or None.
  • profile - The profile that was applied, or None.
  • login_cache - What the login cache contributed.
  • warnings - Sentences about settings that were ignored or do not match the profile, already logged at WARNING.

values​

sources​

env_file​

profile​

login_cache​

warnings​

get​

def get(key: str, default: str | None = None) -> str | None

Return the effective value of one setting.

Arguments:

  • key - A SASVI_* name.
  • default - Returned when no layer sets it.

Returns:

The value, or default.

source_of​

def source_of(key: str) -> SettingSource | None

Return the layer that supplied a setting.

Arguments:

  • key - A SASVI_* name.

Returns:

The source, or None when no layer sets it.

credential_mode​

@property
def credential_mode() -> str | None

The credential from_env builds from these settings, or None.

The order is the one from_env uses: an access token (token, or login when the login cache supplied it), then client-credentials, then password.

redacted​

def redacted() -> dict[str, str]

Return the values with every credential replaced by <set>.

Returns:

A copy that is safe to print.

to_dict​

def to_dict() -> PythonObject

Return a printable summary: redacted values, sources, file, profile and cache.

Returns:

A JSON-serializable mapping that holds no credential.

read_env_file​

def read_env_file(path: str | os.PathLike[str]) -> dict[str, str]

Parse a .env file without touching the environment.

The accepted grammar is one KEY=value per line. A leading export is dropped, blank lines and lines starting with # are skipped, a value may be wrapped in single or double quotes, and an unquoted value ends at the first #. When a key repeats, the first one wins.

Arguments:

  • path - The file to read.

Returns:

Every key the file sets, with its value.

Raises:

  • SVIConfigurationError - When the path is not a readable regular file.

resolve_settings​

def resolve_settings(
*,
env_file: str | os.PathLike[str] | None = None,
use_login_cache: bool = False,
profile: str | None = None,
environ: Mapping[str, str] | None = None,
overrides: Mapping[str, str] | None = None,
base_url: str | None = None,
login_cache_path: str | os.PathLike[str] | None = None,
profiles_file: str | os.PathLike[str] | None = None
) -> ResolvedSettings

Resolve the effective SASVI_* settings and their sources.

See the module docstring for the precedence. With no arguments the result is the SASVI_* part of os.environ.

Arguments:

  • env_file - A .env file whose keys sit below the process environment.
  • use_login_cache - Read the sasvi auth login cache. It supplies SASVI_ACCESS_TOKEN when no layer above sets one and the base URL in use is the cached origin, plus SASVI_BASE_URL and SASVI_VERIFY_SSL when no layer above sets them.
  • profile - A profile name from profiles.toml. The SDK does not read SASVI_PROFILE or the file's default on its own; see sasvi.profiles.active_profile_name.
  • environ - The process environment to read; defaults to os.environ.
  • overrides - SASVI_* values that win over every layer.
  • base_url - The base URL the client will use when it is passed in code rather than as SASVI_BASE_URL. The login cache is used only when it is the cached origin.
  • login_cache_path - The cache file; defaults to the profile's cache, or the default one without a profile.
  • profiles_file - The profiles file; defaults to sasvi.profiles.profiles_path.

Returns:

The settings, their sources, and what the profile and cache contributed.

Raises:

  • SVIConfigurationError - When the .env file is missing or unreadable.
  • sasvi.profiles.ProfileNotFoundError - When the profile is not defined.
  • sasvi.profiles.ProfileFileError - When the profiles file is not valid.