Skip to main content

A python module to easily read from and write to yaml config files.

Project description

yaml_configuration

This module offers easy configuration for a other modules or startup scripts. It offers an easy way to load and save config files and read and write config values to it. Also trying to read a config value by passing an optional default value if the real config value is not present is supported.

Usage

The class DefaultConfig provides the basic functionality of the package. A custom configuration class is meant to derive from it.

import os
import logging
from pytest import raises
from yaml_configuration.config import DefaultConfig, ConfigError


def read_file(file_path, filename):
    file_path = os.path.join(file_path, filename)
    with open(file_path, 'r') as file_pointer:
        file_content = file_pointer.read()
    return file_content


class BasicConfig(DefaultConfig):

    def __init__(self, config_string, config_file, logger_object=None):
        super(BasicConfig, self).__init__(config_string, logger_object)
        # this is already done in the init
        # self.load(config_file, path=os.path.dirname(__file__))


if __name__ == '__main__':
    config_file = "basic_config.yaml"
    config_string = read_file(os.path.dirname(__file__), config_file)
    basic_config = BasicConfig(config_string, config_file, logging.getLogger("TestLogger"))
    basic_config.set_config_value("number_value", 10)
    basic_config.set_config_value("string_value", "test_string")
    assert basic_config.get_config_value("string_value") == "test_string"
    assert basic_config.get_config_value("not_existing_config_value", default=42) == 42
    with raises(ConfigError):
        if not basic_config.get_config_value("value_that_should_exist"):
            raise ConfigError("The config value with key 'value_that_should_exist' should exist")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yaml_configuration-0.3.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yaml_configuration-0.3.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file yaml_configuration-0.3.2.tar.gz.

File metadata

  • Download URL: yaml_configuration-0.3.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for yaml_configuration-0.3.2.tar.gz
Algorithm Hash digest
SHA256 507377ecd6a7347557f3ae4caabcfa79a106d7b75e5acead11a91b2c0b878a9f
MD5 e5de1787903b786457d1ba0591debaee
BLAKE2b-256 4c7e101ab7c470b4c0255f679524f2fa83dd6162d0cef2a3be32f5c4bae724e5

See more details on using hashes here.

File details

Details for the file yaml_configuration-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for yaml_configuration-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fc0c10a8b0b9535d2617ad8b2f30711f33c802baa6b38b5c125630d407164a3
MD5 ad017ecff35cf52961e2cc65a8b896ce
BLAKE2b-256 8f4ec0f575587c9f89d3bcb1cb64be352f0fd818531d21b225dd7ba3ab24d2ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page