Environment
plateforme.core.environment
This module contains utilities for managing environment variables and files within the Plateforme framework.
The Environment
class simplifies the management of environment variables, it
offers methods to load variables from dictionaries and files, set and retrieve
environment variables with support for default values and type casting, and
delete variables. It also ensures that variable names adhere to valid patterns
and provides internal mechanisms to avoid circular references when resolving
proxy variables. Additionally, the class includes methods for casting
environment variables to specific data types such as boolean, bytes,
dictionaries, floats, integers, JSON, lists, sets, strings, tuples, and URLs.
Examples:
Initialize with optional environment mode and variables:
>>> from plateforme import Environment
>>> env = Environment(mode='dev', variables={'TITLE': 'My Plateforme'})
Retrieve an environment variable with optional casting and default:
Set an environment variable:
Delete an environment variable:
This demonstrates the basic functionality of getting, setting, and
deleting environment variables using the Environment
class. Note that
changes to environment variables using set_variable
and delete_variable
will affect the environment for the current process and any new child
processes spawned after the change.
Note
It loads environment files using the external library python-dotenv
.
Environment
Environment(
mode: str | None = None,
/,
files: list[str] | None = None,
variables: dict[str, str | None] | None = None,
encoding: str | None = "utf-8",
overwrite: bool = False,
)
An environment class to manage environment variables and files.
Examples:
Note
The environment class can be used as a namespace to get, set and delete
environment variables. Initializing the environment class will load the
environment variables from the os environment, the .env
common file
and the .env.{mode}
specific mode file, where {mode}
is the
environment mode (e.g. dev
, development
, prod
,
production
, etc.), found in the process root directory.
Initialize the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
str | None
|
The environment mode. If the mode is not provided, the
environment mode will be loaded with the value of the
|
None
|
files
|
list[str] | None
|
The default environment files to load. The files will be
loaded in the order they are provided, from lowest to highest
priority. Defaults to |
None
|
variables
|
dict[str, str | None] | None
|
The default environment variables to load.
Defaults to |
None
|
encoding
|
str | None
|
The encoding of the environment files to load. Defaults
to |
'utf-8'
|
overwrite
|
bool
|
Whether to overwrite existing environment variables in
the namespace with the same key. The overwrite parameter is
used to determine whether to overwrite existing environment
variables in the namespace with the same key. If the overwrite
parameter is set to |
False
|
Note
The environment variables will be loaded with the following
order priority (from highest to lowest):
- The os environment variables
- The environment .env.{mode}
specific mode file
- The environment .env
common file
- The default environment files provided in the constructor
- The default environment variables provided in the constructor
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
clear
load
load(
mode: str | None = None,
/,
files: list[str] | None = None,
variables: dict[str, str | None] | None = None,
encoding: str | None = "utf-8",
overwrite: bool = False,
reset: bool = False,
) -> None
Load the environment variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
str | None
|
The environment mode. If the mode is not provided, the
environment mode will be loaded with the value of the
|
None
|
files
|
list[str] | None
|
The default environment files to load. The files will be
loaded in the order they are provided, from lowest to highest
priority. Defaults to |
None
|
variables
|
dict[str, str | None] | None
|
The default environment variables to load.
Defaults to |
None
|
encoding
|
str | None
|
The encoding of the environment files to load. Defaults
to |
'utf-8'
|
overwrite
|
bool
|
Whether to overwrite existing environment variables in
the namespace with the same key. The overwrite parameter is
used to determine whether to overwrite existing environment
variables in the namespace with the same key. If the overwrite
parameter is set to |
False
|
reset
|
bool
|
Whether to clear the environment namespace before loading
the environment variables. Defaults to |
False
|
Note
The environment variables will be loaded with the following
order priority (from highest to lowest):
- The os environment variables
- The environment .env.{mode}
specific mode file
- The environment .env
common file
- The default environment files provided in the constructor
- The default environment variables provided in the constructor
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
get_variable
get_variable(
key: str,
_guard: frozenset[str] = frozenset(),
/,
default: Any = Undefined,
parse_default: bool = False,
cast: CastObject | None = None,
) -> Any
Get an environment variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
_guard
|
frozenset[str]
|
The environment guard set to avoid circular references when resolving accessed variables. Defaults to an empty set. |
frozenset()
|
default
|
Any
|
The environment variable default value.
Defaults to |
Undefined
|
parse_default
|
bool
|
Whether to parse the default value with the
environment variable cast object. Defaults to |
False
|
cast
|
CastObject | None
|
The environment variable cast object.
Defaults to |
None
|
Note
If the environment variable is not found and no default value is
provided, a KeyError
will be raised, else the defaultvalue will
be returned. If the environment variable is found but no cast
object is provided, the environment variable value will be returned
as a string.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
set_variable
Set an environment variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to set. |
required |
value
|
str
|
The environment variable value to set. |
required |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
set_default
Set an environment variable if it is not already set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to set. |
required |
default
|
str
|
The environment variable default value to set. |
required |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
delete_variable
delete_variable(key: str) -> None
Delete an environment variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to delete. |
required |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
load_from_variables
Load environment variables from an environment dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variables
|
dict[str, str | None]
|
The environment variables to load into the namespace. |
required |
overwrite
|
bool
|
Whether to overwrite existing environment variables in
the namespace with the same key. Defaults to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
load_from_files
load_from_files(
files: list[str],
/,
encoding: str | None = "utf-8",
overwrite: bool = False,
raise_errors: bool = False,
) -> None
Load environment variables from multiple environment files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files
|
list[str]
|
The environment files path to load into the namespace. |
required |
encoding
|
str | None
|
The encoding of the environment files to load. Defaults
to |
'utf-8'
|
overwrite
|
bool
|
Whether to overwrite existing environment variables in
the namespace with the same key. Defaults to |
False
|
raise_errors
|
bool
|
Whether to raise an error if a file is not found.
Defaults to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
check_key
classmethod
Check if an environment variable key is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to check. |
required |
raise_errors
|
bool
|
Whether to raise an error if the key is invalid.
Defaults to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
parse
classmethod
Parse a string value with the provided cast object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
The value to parse. |
required |
cast
|
CastObject | None
|
The cast object. Defaults to |
None
|
Note
If no cast object is provided, the value will be returned as is. Otherwise, the value will be casted using the cast object if it a function, else the cast object will be analyzed as an iterable instance. The value will be split using the comma separator and each resulting value will be casted using the cast function provided by the first element of the iterable cast object.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
|
as_bool
Get an environment variable as a boolean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
bool | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_bytes
as_bytes(
key: str,
/,
default: bytes | None | UndefinedType = Undefined,
encoding: str = "utf-8",
) -> bytes
Get an environment variable as bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
bytes | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
encoding
|
str
|
The encoding of the bytes string. Defaults to |
'utf-8'
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_dict
as_dict(
key: str,
/,
default: dict[Any, Any]
| None
| UndefinedType = Undefined,
cast: CastFunc | None = None,
) -> dict[Any, Any]
Get an environment variable as a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
dict[Any, Any] | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
cast
|
CastFunc | None
|
The environment variable cast function.
Defaults to |
None
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_float
Get an environment variable as a float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
float | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_int
Get an environment variable as an integer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
int | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_json
Get an environment variable as a boolean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
Any
|
The environment variable default value.
Defaults to |
Undefined
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_list
as_list(
key: str,
/,
default: list[Any] | None | UndefinedType = Undefined,
cast: CastFunc | None = None,
) -> list[Any]
Get an environment variable as a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
list[Any] | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
cast
|
CastFunc | None
|
The environment variable cast function.
Defaults to |
None
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_set
as_set(
key: str,
/,
default: set[Any] | None | UndefinedType = Undefined,
cast: CastFunc | None = None,
) -> set[Any]
Get an environment variable as a set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
set[Any] | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
cast
|
CastFunc | None
|
The environment variable cast function.
Defaults to |
None
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_str
as_str(
key: str,
/,
default: str | None | UndefinedType = Undefined,
multiline: bool = False,
) -> str | None
Get an environment variable as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
str | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
multiline
|
bool
|
Whether to return the string as a multiline string. If
the multiline parameter is set to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_tuple
as_tuple(
key: str,
/,
default: tuple[Any, ...]
| None
| UndefinedType = Undefined,
cast: CastFunc | None = None,
) -> tuple[Any, ...]
Get an environment variable as a tuple.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
tuple[Any, ...] | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|
cast
|
CastFunc | None
|
The environment variable cast function.
Defaults to |
None
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/environment.py
as_url
as_url(
key: str,
/,
default: ParseResult | None | UndefinedType = Undefined,
) -> ParseResult
Get an environment variable as a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The environment variable key to get. |
required |
default
|
ParseResult | None | UndefinedType
|
The environment variable default value.
Defaults to |
Undefined
|