Skip to main content

A small framework to build your flexible project configurations

Project description

ConfigFramework

A small and simple framework to build your configs.

This project been created mostly because of me myself needing some simplistic and same time powerful enough tool to create configs, validate them somewhat and to have easy interface.

Here's basic example:

from ConfigFramework import ConfigVariable, JSONFileConfigLoader, BaseConfig

json_loader = JSONFileConfigLoader.load("example_config.json", defaults={"sample_default": 2*2})

class Config(BaseConfig):
    field1 = ConfigVariable.variable("var1", json_loader)
    field2 = ConfigVariable.variable("var23", json_loader, caster=str, dump_caster=int)
    sample_default = ConfigVariable.variable("sample_default", json_loader)

    def __post_init__(self, *args, **kwargs):
        print(self.field2)
        self.field2.value = "123"

conf = Config()
conf.dump()

See full example with explanation here

Installing

Pypi link: https://pypi.org/project/ConfigFramework

pip install ConfigFramework

Build your own config loaders and casters

The way ConfigFramework been built allowing you to just inherit your own loaders like that

from ConfigFramework import AbstractConfigLoader

def load_from_internet() -> dict:
    ...

def upload_to_cloud(data):
    ...

class NewCustomConfigLoader(AbstractConfigLoader):
    @classmethod
    def load(cls, defaults=None):
        data: dict = load_from_internet()
        return cls(data, defaults)

    def dump(self):
        upload_to_cloud(self.data)

If you feel like you need some custom data type for your loader - why not to make it? You will only need to write functions that will cast data to pythons type and back to savable format!

from ConfigFramework import BaseConfig, ConfigVariable, JSONFileConfigLoader

loader = JSONFileConfigLoader.load("example_json.json")

class MyCustomType:
    def __init__(self, data):
        self.your_data = data

    @classmethod
    def load_from_string(cls, serialized: str):
        ...
        data = serialized
        return cls(data)

    def dump(self) -> str:
        ...


class Config(BaseConfig):
    variable_1 = ConfigVariable.variable(
        "example_var", loader,
        caster=MyCustomType.load_from_string, dump_caster=MyCustomType.dump
    )    

You may also want to cast one config to other type and i allow you to! Look at how easy it is:

from ConfigFramework import YAMLConfigLoader, JSONFileConfigLoader

yaml_loader = YAMLConfigLoader.load("testing.yaml")
json_loader = JSONFileConfigLoader.load("testing.json")

new_loader = json_loader.dump_to_other_loader(yaml_loader)

But keep in mind that not all types are easily translated over formats (for example arrays and mappings) but you may write serializers if you need!

Out of the box you also may combine multiple ConfigLoaders: see in combined config loaders example

If you need some default type casters (for example if you loading ini file) - you may use one's from ConfigFramework.custom_types.casters.Casters

Some more examples here

Configuring

ConfigFramework have its own config, but there's not too much things you might need at the end. Yet if you won't set EnvironmentConfigLoader.mute_warning to False - you'll be getting warning when creating EnvironmentConfigLoader about need in casters for everything and not being able to dump loader itself somewhere. Also it can help you set a path for log files.

If you need to change location of config file - just set an environment variable CONFIGFRAMEWORK_SETTINGS_PATH to your liking with full path to logs file.

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

ConfigFramework-1.1.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

ConfigFramework-1.1.2-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file ConfigFramework-1.1.2.tar.gz.

File metadata

  • Download URL: ConfigFramework-1.1.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for ConfigFramework-1.1.2.tar.gz
Algorithm Hash digest
SHA256 eb0141cf97db84c5e78dfb310b443ec76317b63287e5dc7bad0e7a960edf4209
MD5 652a7cff949346a55742c9171546c0a1
BLAKE2b-256 008f487d31e5caa6bebae967d0fdc3d3bf92352a840b021955fb13fbcf2f5183

See more details on using hashes here.

File details

Details for the file ConfigFramework-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: ConfigFramework-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for ConfigFramework-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d75d5e59e94bbc4641a873586e19374cd06cd859a574db2f4bec39c4732624bc
MD5 afc0d6b58f83a7b8f08519198ad0213f
BLAKE2b-256 96e31d08243fa43b62982fa927812d6576a55c8a16d68e3a4e8de44404217558

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