Module API - Loaders
superconf.loaders
Loaders library
AbstractConfigurationLoader
Abstract base class for configuration loaders.
This class defines the interface that all configuration loaders must implement. Configuration loaders are responsible for loading configuration values from different sources (files, environment, etc.).
Source code in superconf/loaders.py
__contains__(item)
__getitem__(item)
check()
Verify if the configuration source is valid and accessible.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the configuration source is valid, False otherwise. |
contains(config, item)
Check if a configuration key exists in this loader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
The configuration object. |
required | |
item
|
str
|
The configuration key to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the key exists, False otherwise. |
Source code in superconf/loaders.py
getitem(config, item)
Retrieve a configuration value by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
The configuration object. |
required | |
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Type | Description |
---|---|
The configuration value for the given key. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist. |
Source code in superconf/loaders.py
CommandLine
Bases: AbstractConfigurationLoader
Configuration loader that extracts settings from command line arguments.
This loader uses an argparse.ArgumentParser to extract configuration values from command line arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser
|
ArgumentParser
|
The parser instance to extract variables from. |
required |
get_args
|
callable
|
Optional function to extract args from the parser. Defaults to the get_args function. |
_get_args
|
Source code in superconf/loaders.py
__contains__(item)
__getitem__(item)
__init__(parser, get_args=_get_args)
:param parser: An argparse
parser instance to extract variables from.
:param function get_args: A function to extract args from the parser.
:type parser: argparse.ArgumentParser
Source code in superconf/loaders.py
Dict
Bases: AbstractConfigurationLoader
Configuration loader that uses a dictionary as the configuration source.
This loader is useful for providing hardcoded default values or for testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values_mapping
|
dict
|
A dictionary of configuration key-value pairs. |
required |
Example
config = Dict({"debug": "true", "port": "8000"}) "debug" in config # True config["port"] # "8000"
Source code in superconf/loaders.py
__contains__(item)
__getitem__(item)
Retrieve a configuration value from the dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Type | Description |
---|---|
The configuration value. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist in the dictionary. |
Source code in superconf/loaders.py
EnvFile
Bases: AbstractConfigurationLoader
Configuration loader that reads settings from a .env file.
This loader reads configuration values from a file in environment variable format (KEY=value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Path to the .env file. Defaults to ".env". |
'.env'
|
keyfmt
|
callable
|
Optional function to pre-format variable names. Defaults to EnvPrefix() which converts keys to uppercase. |
EnvPrefix()
|
Example
env = EnvFile(".env", keyfmt=EnvPrefix("MYAPP_"))
With .env containing: DEBUG=true
"debug" in env
Returns True
env["debug"]
Return "true"
Source code in superconf/loaders.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 |
|
__contains__(item)
__getitem__(item)
Retrieve a configuration value from the .env file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The configuration value. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist in the file. |
Source code in superconf/loaders.py
check()
Verify if the .env file exists and is valid.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the file exists and is valid, False otherwise. |
Source code in superconf/loaders.py
EnvPrefix
A utility class for namespacing environment variables with a prefix.
Since the environment is a global dictionary, it is a good practice to
namespace your settings by using a unique prefix like MY_APP_
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
The prefix to prepend to environment variable names. Defaults to "". |
''
|
Example
prefix = EnvPrefix("MYAPP_") prefix("database_url") 'MYAPP_DATABASE_URL'
Source code in superconf/loaders.py
__call__(value)
Transform a configuration key into an environment variable name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
The configuration key to transform. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The environment variable name with the prefix applied and converted to uppercase. |
Source code in superconf/loaders.py
Environment
Bases: AbstractConfigurationLoader
Configuration loader that reads settings from environment variables.
This loader retrieves configuration values from the system's environment variables (os.environ), optionally transforming the keys using a formatting function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keyfmt
|
callable
|
Optional function to pre-format variable names. Defaults to EnvPrefix() which converts keys to uppercase. |
EnvPrefix()
|
Example
env = Environment(keyfmt=EnvPrefix("MYAPP_")) os.environ["MYAPP_DEBUG"] = "true" "debug" in env # True env["debug"] # "true"
Source code in superconf/loaders.py
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
__contains__(item)
__getitem__(item)
Retrieve a configuration value from environment variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The environment variable value. |
Raises:
Type | Description |
---|---|
KeyError
|
If the environment variable doesn't exist. |
Source code in superconf/loaders.py
__init__(keyfmt=EnvPrefix(), prefix=None, sep='__')
:param function keyfmt: A function to pre-format variable names.
get_env_name(config, item)
Return the envirnonment name for a given key in config
Source code in superconf/loaders.py
getitem(config, item)
Retrieve a configuration value by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
The configuration object. |
required | |
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Type | Description |
---|---|
The configuration value for the given key. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist. |
Source code in superconf/loaders.py
IniFile
Bases: AbstractConfigurationLoader
Configuration loader that reads settings from an INI/CFG file.
This loader reads configuration from a specified section in an INI-style configuration file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Path to the .ini/.cfg file. |
required |
section
|
str
|
Section name inside the config file. Defaults to "settings". |
'settings'
|
keyfmt
|
callable
|
Optional function to pre-format variable names. |
lambda x: x
|
Raises:
Type | Description |
---|---|
InvalidConfigurationFile
|
If the file is not a valid INI file. |
MissingSettingsSection
|
If the specified section is not found in the file. |
Source code in superconf/loaders.py
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
__contains__(item)
Check if a configuration key exists in the INI file section.
__getitem__(item)
Retrieve a configuration value from the INI file section.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The configuration value. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist in the section. |
Source code in superconf/loaders.py
check()
Verify if the INI file exists and is valid.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the file exists and is valid, False otherwise. |
Source code in superconf/loaders.py
RecursiveSearch
Bases: AbstractConfigurationLoader
Configuration loader that recursively searches for configuration files.
This loader looks for configuration files in the current directory and all parent directories up to a specified root path. It supports multiple file types and loaders.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
starting_path
|
str
|
The path to begin looking for configuration files. |
None
|
filetypes
|
tuple
|
Tuple of (pattern, loader_class) pairs defining which files to look for and how to load them. Defaults to .env, .ini, and .cfg files. |
(('.env', EnvFile), (('*.ini', '*.cfg'), IniFile))
|
root_path
|
str
|
The path where the search will stop. Defaults to "/". |
'/'
|
Example
search = RecursiveSearch("/home/user/project")
Will look for .env, .ini, and .cfg files in: /home/user/project /home/user /home /
Source code in superconf/loaders.py
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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
|
config_files
property
Get all discovered configuration files.
Returns:
Name | Type | Description |
---|---|---|
list |
List of configuration loader instances. |
starting_path
property
writable
Get the current starting path for the search.
__contains__(item)
Check if a configuration key exists in any of the discovered files.
__getitem__(item)
Retrieve a configuration value from the first file that contains it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item
|
str
|
The configuration key to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The configuration value. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key doesn't exist in any configuration file. |
Source code in superconf/loaders.py
get_filenames(path, patterns)
staticmethod
Get all filenames in a directory matching the given patterns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Directory to search in. |
required |
patterns
|
str or tuple
|
Glob pattern(s) to match against. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List of matching filenames. |