Skip to main content

ConfigORM

Build Status PyPI - Status PyPI - Python Version PyPI PyPI - License PyPI - Downloads

Heavily inspired by Charles Leifer peewee ORM. This package provides ORM-like interface to interact with *.ini configs and secrets in HashiCorp Vault. And map their data onto object models.

Getting Started

Installation

You can install ConfigORM with pip:

$ pip3 install ConfigORM

Two Sources

The library supports two sources of data storage - ini-file and Vault by HashiCorp. The operation interface in this case will be identical.

Quick Start ini-file way

Let's say we have config like this:

#config.ini

[Database]
server = 10.10.10.10
password = my_password
user = admin
base = test_base

[General]
debug = True
connection port = 5000

Create connection to the source:

#Config.py

import os
from configorm import *

current_dir = os.path.abspath(os.path.dirname(__file__))
connection_string = os.path.join(current_dir, 'config.ini')

connector = IniConnector(connection_string=connection_string)

Quick Start HashiCorp Vault way

Provide connection data for Vault server and KV store in it:

#Config.py

from configorm import VaultConnector

connector = VaultConnector(
    mount_point='SOME_KV_STORE/',
    url='http://some-vault-url.com',
    token='TOKEN_FOR_SECRETS'
) 

Defining models

Defining models is similar to ORM's:

#Config.py


class BaseSection(Section):
    class Meta:
        connector = connector
        
class Database(BaseSection):
    server = StringField()
    password = StringField()
    user = StringField()
    base = StringField()
    
class General(BaseSection):
    debug = BooleanField()
    connection_port = IntegerField()
    
>>> from Config import Database
>>> Database.server
'10.10.10.10'

Section names must match their counterparts in ini file or Vault, but case does not matter at all. All spaces in section or key names of config file will be treated as underlines.

Field Types

Depending on field data will be cast to exact type.

>>> from Config import General
>>> General.debug
True
>>> type(General.debug)
<class 'bool'>

Available Field Types:

  • StringField
  • IntegerField
  • BooleanField
  • FloatField
  • ListField

Most field types are self-explanatory, ListField is a bit tricky. It allows to store and extract data as list of homogeneous objects, such as strings, integers, floats and booleans. You must provide exec type of stored objects.

from configorm import *

class TestSection(Section):
    list_of_int = ListField(var_type=int)
    list_of_str = ListField(var_type=str)
    list_of_float = ListField(var_type=float)
    list_of_bool = ListField(var_type=bool)

Fallback Values

You may provide default fallback values for your fields. If field may return None Type values, null parameter must be set as True

from configorm import *

class Database(Section):
    server = StringField(default='10.10.10.10')
    password = StringField(default='secret')
    user = StringField(default='admin')
    base = StringField(default='development')
    
    possible_none_value = StringField(null=True)

Environment Variables Override


You can override filed values with data from environment variables. Set env_override flag as True and if value is present in environment field will attempt to use that. Environment key is formed from concatenation of section and field name in upper case. If value is missing from environment variables field will use standard approach.

from configorm import *
import os

class SomeSection(Section):
    le_field = StringField(default='value', env_override=True)
    
os.environ['SOMESECTION_LE_FIELD'] = 'env_value'

Model First Approach

Base Section aside from connection to config file also provides tool to create configuration from models, allowing model-first approach. It crates config file, sections from your models names and option based on provided fields. In case if fields have default values, they will be written in config as well. Otherwise, options will be filled with empty values. Works with both ini and Vault connections.

>>> from Config import *
>>> BaseSection.check_config_integrity()

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • Hat tip to Charles Leifer for Inspiration

Metadata

Release files for ConfigORM 1.0.1

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

Source distribution (sdist)

Source distribution for ConfigORM 1.0.1
File Size Uploaded
ConfigORM-1.0.1.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ConfigORM 1.0.1
File Interpreter ABI Platform
ConfigORM-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / ConfigORM-1.0.1.tar.gz

Download URL ConfigORM-1.0.1.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a620827c1446363c984d8430d3c07388dec9e902544940f9f039636c1d79bb4c
BLAKE2b-256 checksum
How to use checksums
e6823f455629fe23c13dc2e4e9db5694e25651ed35f2fd635f81564693bbaddb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / ConfigORM-1.0.1-py3-none-any.whl

Download URL ConfigORM-1.0.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf7efa02b82dc0544c334a552539356c2ba9474575054d7bca5915280e12f1e7
BLAKE2b-256 checksum
How to use checksums
e04a042b944f9834b264bc6a24feeec3d68e2721b26172f1a408812cfc221ff0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

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