Skip to main content

sasvi.profiles

Named connection profiles kept in profiles.toml.

A profile names a tenant and how to reach it, so a user with several tenants can switch between them with sasvi --profile NAME or SVIClient.from_env(profile=NAME) instead of editing variables.

The file lives at $XDG_CONFIG_HOME/sasvi/profiles.toml (default ~/.config/sasvi/profiles.toml); SASVI_PROFILES_FILE names another one. It looks like this::

default = "prod"

[profiles.prod] base_url = "https://viya.example.com" verify_ssl = true # or false, or the path of a CA bundle client_id = "sas.cli" auth = "login" # login, client-credentials, password or token

A profile holds no secret. The file is plain text that tends to end up in backups and dotfile repositories, so a key that looks like a credential (password, secret, token and the like) makes the whole file unreadable with ProfileFileError. Secrets stay in the environment, a .env file, or the login cache that sasvi auth login writes.

The file is written 0600 in a 0700 directory, and a symbolic link at its path is refused rather than written through.

AuthMode​

The credential a profile expects: see Profile.auth.

AUTH_MODES​

Every accepted value of Profile.auth.

PROFILE_KEYS​

The keys a profile table may hold.

ProfileFileError Objects​

class ProfileFileError(SVIConfigurationError)

The profiles file cannot be read, holds a forbidden key, or cannot be written.

Arguments:

  • message - What is wrong, naming the file and the key, never a value.
  • path - The profiles file.

ProfileNotFoundError Objects​

class ProfileNotFoundError(SVIConfigurationError)

A profile was asked for by name and the profiles file does not define it.

Arguments:

  • name - The profile name that was asked for.
  • path - The profiles file that was read.

Profile Objects​

@dataclass(frozen=True)
class Profile()

One named profile.

Arguments:

  • name - The profile name.
  • base_url - The tenant base URL, normalized as the client normalizes it.
  • verify_ssl - True, False, or the path of a CA bundle file or certificate directory. None leaves TLS verification to the environment, which defaults to on.
  • client_id - The OAuth client id: the one sasvi auth login uses, and SASVI_CLIENT_ID for a client-credentials or password grant.
  • auth - The credential the profile expects. It does not change the order in which credentials are chosen; a mismatch is reported in sasvi.settings.ResolvedSettings.warnings.

name​

base_url​

verify_ssl​

client_id​

auth​

to_dict​

def to_dict() -> dict[str, str | bool]

Return the profile's fields, leaving out unset ones, plus its name.

Returns:

The name under name, and each set field under its own key.

settings​

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

Return the SASVI_* settings this profile supplies.

Returns:

SASVI_BASE_URL, SASVI_VERIFY_SSL and SASVI_CLIENT_ID for the fields that are set, as the strings the environment would hold.

ProfileFile Objects​

@dataclass(frozen=True)
class ProfileFile()

The parsed profiles file.

Arguments:

  • path - The file that was read.
  • profiles - Every defined profile, by name.
  • default - The profile sasvi profile use selected, or None.

path​

profiles​

default​

profiles_path​

def profiles_path(environ: Mapping[str, str] | None = None) -> Path

Return the path of the profiles file.

Precedence: SASVI_PROFILES_FILE, then $XDG_CONFIG_HOME/sasvi/profiles.toml, then ~/.config/sasvi/profiles.toml.

Arguments:

  • environ - The environment to read; defaults to os.environ.

Returns:

The path. The file need not exist.

validate_profile_name​

def validate_profile_name(name: str) -> str

Check that a profile name is safe as a TOML key and a file-name part.

Arguments:

  • name - The name to check.

Returns:

The name, unchanged.

Raises:

  • SVIConfigurationError - When the name is empty, longer than 64 characters, or has a character other than letters, digits, _, . and - (or starts with one of the last three).

load_profiles​

def load_profiles(path: Path | None = None) -> ProfileFile

Read and validate the profiles file.

A missing file is an empty one. Any problem (bad TOML, an unknown or secret-looking key, a base URL the client would refuse) raises, naming the file and the key but never a value.

Arguments:

  • path - The file to read; defaults to profiles_path.

Returns:

The parsed file.

Raises:

  • ProfileFileError - When the file cannot be read or is not valid.

get_profile​

def get_profile(name: str, path: Path | None = None) -> Profile

Return one profile by name.

Arguments:

  • name - The profile name.
  • path - The profiles file; defaults to profiles_path.

Returns:

The profile.

Raises:

  • ProfileNotFoundError - When the file does not define it.
  • ProfileFileError - When the file is not valid.

active_profile_name​

def active_profile_name(explicit: str | None = None,
*,
environ: Mapping[str, str] | None = None,
path: Path | None = None) -> str | None

Return the profile the CLI selects: the explicit one, SASVI_PROFILE, or the default.

The SDK never calls this on its own. SVIClient.from_env uses a profile only when it is named; pass profile=active_profile_name() to select one the way the sasvi command does.

Arguments:

  • explicit - A name given on the command line or in code.
  • environ - The environment to read SASVI_PROFILE from; defaults to os.environ.
  • path - The profiles file; defaults to profiles_path.

Returns:

The profile name, or None when none is selected.

Raises:

  • ProfileFileError - When the default has to be read from a file that is not valid.

render_profiles​

def render_profiles(profiles: Mapping[str, Profile],
default: str | None) -> str

Render profiles as the TOML text save_profiles writes.

Arguments:

  • profiles - The profiles, by name.
  • default - The default profile name, or None.

Returns:

The file content.

save_profiles​

def save_profiles(profiles: Mapping[str, Profile],
*,
default: str | None,
path: Path | None = None) -> Path

Write the whole profiles file, 0600 in a 0700 directory.

Arguments:

  • profiles - Every profile to keep, by name.
  • default - The default profile name, or None.
  • path - The file; defaults to profiles_path.

Returns:

The file that was written.

Raises:

  • ProfileFileError - When the path is a symbolic link, or the default names no profile.
  • OSError - When the file cannot be written.

set_profile​

def set_profile(profile: Profile, *, path: Path | None = None) -> Path

Add a profile, or replace the one with its name.

The profile is validated as load_profiles would read it back.

Arguments:

  • profile - The profile to store.
  • path - The profiles file; defaults to profiles_path.

Returns:

The file that was written.

Raises:

  • ProfileFileError - When the existing file is not valid, the profile would not read back, or the path is a symbolic link.
  • SVIConfigurationError - When the profile name is not valid.

delete_profile​

def delete_profile(name: str, *, path: Path | None = None) -> bool

Remove a profile. A default that named it is cleared.

Arguments:

  • name - The profile name.
  • path - The profiles file; defaults to profiles_path.

Returns:

True when a profile was removed, False when there was none.

Raises:

  • ProfileFileError - When the file is not valid or the path is a symbolic link.

set_default_profile​

def set_default_profile(name: str | None, *, path: Path | None = None) -> Path

Make a profile the default, or clear the default with None.

Arguments:

  • name - The profile name, or None.
  • path - The profiles file; defaults to profiles_path.

Returns:

The file that was written.

Raises:

  • ProfileNotFoundError - When the file does not define the profile.
  • ProfileFileError - When the file is not valid or the path is a symbolic link.