Skip to main content

python-config-parser


Tests PyPI version Coverage Status

python-config-parser lets you create runtime configuration objects using json or yaml files.

MAIN FEATURES


  • Declarative configurations without using .ini files
  • Access using OOP or subscriptable, which means that you can iterate the config object items
  • Runtime validation using schema
  • Automatic environment variables interpolation
  • Automatic parser selecting using config file extension

HOW TO INSTALL


Use pip to install it.

pip install python-config-parser

HOW TO USE


By default, the config file will look for any of the following config files in the config directory: config.json/config.yaml/config.yml. You can change the config directory and or config file according to your preference (assuming your current directory).

from pyconfigparser import configparser

configparser.get_config(CONFIG_SCHEMA, config_dir='your_config_dir_path', file_name='your_config_file_name')

Schema validation

You may or not use schema validation. If you want to use it, it will validate and apply rules to the whole config object before returning it. If you choose to not use it, it won't validate the config object before returning it, and it may generate runtime access inconsistencies. How to use schema

from schema import Use, And

SCHEMA_CONFIG = {
    'core': {
        'logging': {
            'format': And(Use(str), lambda string: len(string) > 0),
            'date_fmt': And(Use(str), lambda string: len(string) > 0),
            'random_env_variable': str
        },
        'allowed_clients': [{
                'ip': str, # <- Here you can use regex to validate the ip format
                'timeout': int
            }
        ]
    }
}

The config.yml file

core:
  random_env_variable: ${RANDOM_ENV_VARIABLE}
  logging:
    format: "[%(asctime)s][%(levelname)s]: %(message)s"
    date_fmt: "%d-%b-%y %H:%M:%S"
  allowed_clients:
  - ip: 192.168.0.10
    timeout: 60
  - ip: 192.168.0.11
    timeout: 100

A json config file would be something like:

{
  "core": {
    "random_env_variable": "${RANDOM_ENV_VARIABLE}",
    "logging": {
      "format": "[%(asctime)s][%(levelname)s]: %(message)s",
      "date_fmt": "%d-%b-%y %H:%M:%S"
    },
    "allowed_clients": [
      {
        "ip": "192.168.0.10",
        "timeout": 60
      },
      {
        "ip": "192.168.0.11",
        "timeout": 100
      }
    ]
  }
}

The config instance

from pyconfigparser import configparser, ConfigError
import logging

try:
    config = configparser.get_config(SCHEMA_CONFIG)  # <- Here I'm using that SCHEMA_CONFIG we've already declared
except ConfigError as e:
    print(e)
    exit()

# to access your config you just need to:

fmt = config.core.logging.format #at this point I'm already using the config variables
date_fmt = config['core']['logging']['date_fmt'] # here subscriptable access

logging.getLogger(__name__)

logging.basicConfig(
    format=fmt,
    datefmt=date_fmt,
    level=logging.INFO
)

# the list of object example:

for client in config.core.allowed_clients:
    print(client.ip)
    print(client.timeout)
    
# You can also iterate objects, but instead of giving the property it'll give you the property's name
# And then you can access the values by subscriptale access
for logging_section_attr_key in config.core.logging:
    print(config.core.logging[logging_section_attr_key])

# Accessing the environment variable already resolved
print(config.random_env_variable)

Assuming you've already created the first Config's instance this instance will be cached inside Config class, so after this first creation you just need to re-invoke Config.get_config() without any argument

from pyconfigparser import configparser

config = configparser.get_config()

You can also disable this caching behavior

from pyconfigparser import configparser

configparser.hold_an_instance = False

Environment Variables Interpolation

If the process does not find a value already set to your env variables It will raise a ConfigError. But you can disable this behavior, and the parser will set None to these unresolved env vars

from pyconfigparser import configparser

configparser.ignore_unset_env_vars = True
config = configparser.get_config()

CONTRIBUTE


Fork https://github.com/BrunoSilvaAndrade/python-config-parser/ , create commit and pull request to develop.

Metadata

Release files for python-config-parser 3.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-config-parser 3.1.6
File Size Uploaded
python_config_parser-3.1.6.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-config-parser 3.1.6
File Interpreter ABI Platform
python_config_parser-3.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / python_config_parser-3.1.6.tar.gz

Download URL python_config_parser-3.1.6.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ab92817179804c8e1d4b30b4c76d1f9e9a974d39de64a9e10a4d44f834355ff3
BLAKE2b-256 checksum
How to use checksums
db3ab686863d3c7b8c57a63d7e98ce43a0b7c8858e9b1578b041b5fe19d432a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / python_config_parser-3.1.6-py3-none-any.whl

Download URL python_config_parser-3.1.6-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5801cf0bf7c425584e4c465a853f66c8f94c8cbb27697ca5bdb22689f532c8d
BLAKE2b-256 checksum
How to use checksums
2850ee709580f8dee009d0cdc335deeb67639883d612557d9abd610f7772f445
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

3.1.6 This release

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.3.9

2 release files

2.3.8

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page