Pyjo.AWS.Config - Shared configuration files of AWS

from Pyjo.AWS.Config import Config

config = Config()
print(config.profile, config.region(), config.get('role_arn'))

# Or import the module, as in Google Python Style Guide

import Pyjo.AWS.Config

config = Pyjo.AWS.Config.new(profile='dev')
print(config.settings())
print(config.endpoint_url('S3'))

Pyjo.AWS.Config reads the shared configuration files of AWS, ~/.aws/config and ~/.aws/credentials, as the SDKs of AWS and the AWS CLI read them: the settings of a profile, the sections sso-session and services, the region and the endpoint of a service.

The files are those of the AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE environment variables, or of the home directory, and the profile is the one of the AWS_PROFILE or AWS_DEFAULT_PROFILE environment variable, or default. A profile is [profile name] or [default] in the configuration file, and [name] in the file of the credentials, whose settings come first. The names of the settings are in lower case, as the SDKs of AWS read them, and a setting without a value on its line has the settings of the indented lines below it, as s3:

[profile dev]
region = eu-central-1
s3 =
    addressing_style = path

The files are read when a method needs them, once, and a file that does not exist has no sections.

See also

Pyjo.AWS, https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html.

Classes

class Pyjo.AWS.Config.Pyjo_AWS_Config(*, profile: str | None = None, config_file: str | None = None, credentials_file: str | None = None)[source]

Pyjo.AWS.Config inherits all attributes and methods from Pyjo.Base and implements the following new ones.

__init__(*, profile: str | None = None, config_file: str | None = None, credentials_file: str | None = None) → None[source]
config = Pyjo.AWS.Config.new()
config = Pyjo.AWS.Config.new(profile='dev', config_file='/etc/aws/config')

Construct a new Pyjo.AWS.Config object.

__weakref__

list of weak references to the object

config_file
path = config.config_file
config.config_file = '/etc/aws/config'

The Pyjo.File object of the configuration file, defaults to the value of the AWS_CONFIG_FILE environment variable or ~/.aws/config.

credentials_file
path = config.credentials_file
config.credentials_file = '/etc/aws/credentials'

The Pyjo.File object of the file of the credentials, defaults to the value of the AWS_SHARED_CREDENTIALS_FILE environment variable or ~/.aws/credentials.

endpoint_url(service: str, profile: str | None = None) → str | None[source]
url = config.endpoint_url('S3')
url = config.endpoint_url('SSO OIDC', 'dev')

The endpoint of a service of its ID, as S3, Lambda or SSO OIDC, which the environment or a profile, profile by default, configures, or None: the value of the AWS_ENDPOINT_URL_<SERVICE> environment variable, of the setting endpoint_url of the service in the section services of the profile, of AWS_ENDPOINT_URL, or of the setting endpoint_url of the profile, unless AWS_IGNORE_CONFIGURED_ENDPOINT_URLS or the setting ignore_configured_endpoint_urls is true.

explicit_profile
boolean = config.explicit_profile

Whether the profile is the one of the argument profile, as of the option --profile of the AWS CLI, not of the environment, so that Pyjo.AWS.Credentials.Chain takes the credentials of the profile, and not the ones of the environment, as botocore does.

get(name: str, profile: str | None = None) → Any[source]
value = config.get('role_arn')
s3 = config.get('s3', 'dev')

The value of a setting of a profile, profile by default, a string, or a dict of the settings of its indented lines, or None.

has_profile(profile: str | None = None) → bool[source]
boolean = config.has_profile('dev')

Whether a profile, profile by default, is in one of the files.

profile
name = config.profile
config.profile = 'dev'

The name of the profile, defaults to the value of the AWS_PROFILE environment variable, or of AWS_DEFAULT_PROFILE, which the AWS CLI reads too, or default.

profiles() → list[str][source]
names = config.profiles()

The names of the profiles of both files, sorted.

region(profile: str | None = None) → str | None[source]
region = config.region()
region = config.region('dev')

The region, of the value of the AWS_REGION environment variable, of AWS_DEFAULT_REGION, or of the setting region of a profile, profile by default, or None.

services(name: str) → dict[str, Any][source]
services = config.services('local')

The settings of a section [services name] of the configuration file, of a service of its ID in lower case, with _ in the place of the spaces, as sso_oidc, or an empty dict.

settings(profile: str | None = None) → dict[str, Any][source]
settings = config.settings()
settings = config.settings('dev')

The settings of a profile, profile by default, of the configuration file, with those of the file of the credentials over them, or an empty dict.

sso_session(name: str) → dict[str, Any][source]
session = config.sso_session('company')

The settings of a section [sso-session name] of the configuration file, or an empty dict.