Skip to main content

Simple wrapper for cofigparser

Project description

Config wrapper

Simple package that wraps configparser module. It loads configuration file and keeps it in singleton object.

Main requirements are:

  • have type hints in IDE (like PyCharm)
  • easy to add new sections and fields
  • automatic type casting based on on dataclass attribute type

Currently only following field types are supported: str, bool, ini, float, list.

Simple example

Preparing config manager:

# define config section templates
from dataclasses import dataclass

@dataclass
class ExampleSection1:
    field_1: str
    field_2: int

@dataclass
class ExampleSection2:
    field_1: bool
    field_2: float
    field_3: list

# prepare config manager
from config_wrapper import ConfigManager

class AppConfig(ConfigManager):
    section_1: ExampleSection1
    section_2: ExampleSection2

Prepare config.ini file:

[section_1]
field_1 = value 123
field_2 = 42

[section_2]
field_1 = no
field_2 = 3.14
field_3 = item1;item2;item3

Load configuration & access to values loaded from config.ini file:

AppConfig.load('config.ini', list_sep=';')

print(AppConfig.section_1.field_1)
# value 123

print(AppConfig.section_2.field_1)
# False

print(AppConfig.section_2.field_3)
# ['item1', 'item2', 'item3']

Config section template

Config section template is simple dataclass. It's attributes represents fields in configuration in section.

To point type of field one must pass type hint to dataclass attribute (e.g. field_name: int).

Section example:

from dataclasses import dataclass

@dataclass
class MySection:
    field_1: str  # name of field in config file
    field_2: int    

Attention! Default values of attributes are ignored and overwritten by values from configuration file!

Passing sections to config manager

Having config sections class one can create class that inherits from ConfigManager class and add class attribute of section template type. This attribute name is associated with section name from configuration file.

Simple example:

from dataclasses import dataclass

from config_wrapper import ConfigManager

@dataclass
class MySection:
    field_1: str  # name of field in config file
    field_2: int


class AppConfig(ConfigManager):
    my_section_name: MySection    

Above example corresponds to following configuration structure:

[my_section_name]
field_1 =
field_2 =

TODO

  1. Add unit tests for generating raw config file from ConfigManager.
  2. Generating config file from ConfigManager using default values from template sections.
  3. While generating config file if file exist and field has value it will be copied to newly generated file.
  4. Maybe support for JSON and YAML files.
  5. Maybe support for more types.

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

config_wrapper-0.0.12.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

config_wrapper-0.0.12-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file config_wrapper-0.0.12.tar.gz.

File metadata

  • Download URL: config_wrapper-0.0.12.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for config_wrapper-0.0.12.tar.gz
Algorithm Hash digest
SHA256 dee62fbf5ba7f719443204cd08fb5e45ff04705c983353bc9c0656c409893295
MD5 afc34e98068fb6485971af02bb07959b
BLAKE2b-256 7d73d15dc68ebde35a3d4a027104cf400dba71513a282a3001e21119203a5093

See more details on using hashes here.

File details

Details for the file config_wrapper-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: config_wrapper-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for config_wrapper-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 350bea30e98c36e145e2b481036282010a61f02f08e2620afe418ef58e118720
MD5 fb85a973186cdac65a0df418c3a8fd8b
BLAKE2b-256 5246a6dfb2d632c0c264e5f5705eb975efdc0df780484e98443bb69b3d2014ba

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