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 path to settings 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.2.3.tar.gz (8.8 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.2.3-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ConfigFramework-1.2.3.tar.gz
  • Upload date:
  • Size: 8.8 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.1 CPython/3.9.0

File hashes

Hashes for ConfigFramework-1.2.3.tar.gz
Algorithm Hash digest
SHA256 8b590c0076a937947f7580b870dee9db8eb3126b348d93a0b165bbd3eac2baba
MD5 559bb8baac68211806037ba0cf63e333
BLAKE2b-256 83b2592525be8dd2f3f8393c80618421953f12497d432c9989437b396531f899

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ConfigFramework-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 23.5 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.1 CPython/3.9.0

File hashes

Hashes for ConfigFramework-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 90ec5f6ddbcd3dd381515eabb7d16fbb3b44998a63b9884d9534e76675e1868f
MD5 7788322ca5b103e5a04c4fec9e2ffb5f
BLAKE2b-256 d68b5bd6c1acb00fadf92aa09cdcd57cfc5d34e129b82ace73085744c637506a

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