typerdrive Settings modules
typerdrive.settings.attach
Provide a decorator that attaches the typerdrive settings to a typer command function.
Attributes
Classes
Functions:
attach_settings
attach_settings(
settings_model: type[BaseModel],
*,
validation: Validation = Validation.BEFORE,
persist: bool = False,
show: bool = False,
) -> Callable[
[ContextFunction[P, T]], ContextFunction[P, T]
]
Attach the typerdrive settings to the decorated typer command function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation
|
Validation
|
Indicates when validation should happen in the decorator: |
BEFORE
|
persist
|
bool
|
If set, the settings values will be stored in the settings file |
False
|
show
|
bool
|
If set, show the current settings after running the function |
False
|
Source code in src/typerdrive/settings/attach.py
get_settings
Get the settings instance from the TyperdriveContext.
Source code in src/typerdrive/settings/attach.py
get_settings_manager
Retrieve the SettingsManager from the TyperdriveContext.
Source code in src/typerdrive/settings/attach.py
get_settings_value
Get a particular settings value from the TyperdriveContext.
Source code in src/typerdrive/settings/attach.py
typerdrive.settings.commands
Provide commands that can be added to a typer app to manage settings.
Classes
Functions:
add_bind
Add the bind command to the given typer app.
Source code in src/typerdrive/settings/commands.py
add_encryption_reset
Add the reset command to the given encryption subcommand app.
Source code in src/typerdrive/settings/commands.py
add_reset
Add the reset command to the given typer app.
Source code in src/typerdrive/settings/commands.py
add_settings_subcommand
Add all settings commands to the given app.
Source code in src/typerdrive/settings/commands.py
add_show
Add the show command to the given typer app.
Source code in src/typerdrive/settings/commands.py
add_unset
Add the unset command to the given typer app.
Source code in src/typerdrive/settings/commands.py
add_update
Add the update command to the given typer app.
Source code in src/typerdrive/settings/commands.py
bind
Bind all settings for the app.
Source code in src/typerdrive/settings/commands.py
encryption_change_passphrase
Change the secret encryption passphrase.
Source code in src/typerdrive/settings/commands.py
encryption_lock
Remove the cached secret encryption identity.
Source code in src/typerdrive/settings/commands.py
encryption_setup
encryption_setup(
no_passphrase: Annotated[
bool,
Option(
--no - passphrase,
help="Store the identity without passphrase protection.",
),
] = False,
) -> None
Create secret encryption key material.
Source code in src/typerdrive/settings/commands.py
encryption_unlock
Unlock the secret encryption identity.
Source code in src/typerdrive/settings/commands.py
reset
Remove all settings from the app.
Source code in src/typerdrive/settings/commands.py
show
unset
Remove some settings from the app.
Source code in src/typerdrive/settings/commands.py
update
Update some settings for the app.
Source code in src/typerdrive/settings/commands.py
typerdrive.settings.exceptions
Provide exceptions specific to the settings feature of typerdrive.
Classes
SecretEncryptionError
Bases: SettingsError
Indicates invalid or unavailable encryption material.
Source code in src/typerdrive/settings/exceptions.py
SecretValueError
Bases: SettingsError, ValueError
Indicates an input value that cannot be an encrypted secret.
Subclasses ValueError so that pydantic reports it as a validation failure.
Source code in src/typerdrive/settings/exceptions.py
SettingsDisplayError
Bases: SettingsError
Indicates that there was a problem showing the settings.
Source code in src/typerdrive/settings/exceptions.py
SettingsError
Bases: TyperdriveError
The base exception for settings errors.
Source code in src/typerdrive/settings/exceptions.py
SettingsInitError
Bases: SettingsError
Indicates that there was a problem initializing the settings.
Source code in src/typerdrive/settings/exceptions.py
SettingsResetError
Bases: SettingsError
Indicates that there was a problem resetting a settings value.
Source code in src/typerdrive/settings/exceptions.py
SettingsSaveError
Bases: SettingsError
Indicates that there was a problem saving the settings to disk.
Source code in src/typerdrive/settings/exceptions.py
SettingsUnsetError
Bases: SettingsError
Indicates that there was a problem unsetting a settings value.
Source code in src/typerdrive/settings/exceptions.py
SettingsUpdateError
Bases: SettingsError
Indicates that there was a problem updating a settings value.
Source code in src/typerdrive/settings/exceptions.py
typerdrive.settings.manager
Provide a class for managing the typerdrive settings feature.
Attributes
Classes
SettingsManager
Manage settings for the typerdrive app.
Source code in src/typerdrive/settings/manager.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
Attributes
invalid_warnings
instance-attribute
Tracks which fields of the settings instance are invalid
settings_instance
instance-attribute
An instance of the settings_model that holds the app's current settings
settings_model
instance-attribute
A pydantic model type that defines the app's settings
settings_path
instance-attribute
The path to the file where settings are persisted
Methods:
__init__
Source code in src/typerdrive/settings/manager.py
construct_instance
dump
Dump settings as plain JSON-compatible data, with all secrets unwrapped.
pretty
Return a pretty representation of the settings.
Source code in src/typerdrive/settings/manager.py
reset
Reset the settings back to defaults.
save
Write the current settings to disk.
Source code in src/typerdrive/settings/manager.py
set_warnings
Extract field names and messages from a validation error.
Source code in src/typerdrive/settings/manager.py
to_dict
unset
Remove the settings corresponding to the provided keys.
Source code in src/typerdrive/settings/manager.py
update
Update settings and retain warnings for invalid values.
Source code in src/typerdrive/settings/manager.py
validate
Validate the current settings values.
If invalid, ValidationError exceptions will be raised
Functions:
typerdrive.settings.secrets.models
Provide the secret string types for settings models.
PlaintextSecretStr is persisted as plaintext, and EncryptedSecretStr is persisted as age ciphertext. Both mask
themselves in str() and repr(). Pydantic's own SecretStr is not supported in settings models because it
serializes to a mask.
Attributes
Classes
EncryptedSecretStr
Bases: SecretStr
A secret string that is plaintext in memory and age-encrypted on disk.
The value is encrypted only when settings are saved, and decrypted only when settings are loaded from disk. Like
SecretStr, it masks its value in str() and repr().
Source code in src/typerdrive/settings/secrets/models.py
Attributes
typerdrive_encrypted
class-attribute
instance-attribute
Marks this type as encrypted for settings utilities that must not import this optional module.
Methods:
__get_pydantic_core_schema__
classmethod
Source code in src/typerdrive/settings/secrets/models.py
encrypt
Return the age ciphertext of this secret for the configured recipient.
Source code in src/typerdrive/settings/secrets/models.py
from_ciphertext
classmethod
Hold ciphertext without decrypting it. It is decrypted on first access to the value.
get_secret_value
serialize_value
classmethod
Serialize as ciphertext or plaintext when the dump context asks for it. Otherwise, mask the value.
Source code in src/typerdrive/settings/secrets/models.py
validate_value
classmethod
Wrap a string as a secret, decrypting it first when it is being loaded from disk.
Source code in src/typerdrive/settings/secrets/models.py
PlaintextSecretStr
Bases: SecretStr
A SecretStr that is masked for display, but is written as plaintext when serialized to JSON.
Pydantic's SecretStr serializes to a mask (**********) in JSON mode, which would corrupt a settings file if it
were persisted. This type behaves exactly like SecretStr (including masking in str() and repr()), except
that its JSON serialization is the real value. This allows settings to be persisted with the normal pydantic
serializers instead of walking the model by hand.
Use this type (or EncryptedSecretStr) for secrets in a settings model, instead of SecretStr.
Note
Because the plaintext is written to the settings file, protect that file with appropriate filesystem
permissions or use EncryptedSecretStr.
Source code in src/typerdrive/settings/secrets/models.py
Methods:
__get_pydantic_core_schema__
classmethod
Source code in src/typerdrive/settings/secrets/models.py
serialize_value
classmethod
Return the value itself in python mode, and the plaintext in JSON mode.
Source code in src/typerdrive/settings/secrets/models.py
validate_value
classmethod
Wrap a str or any SecretStr as this type.