Metadata-Version: 2.4
Name: config-core
Version: 0.1.0
Summary: Unified way to load a config file to python obj.
Author: jarneamerlinck-do
License: Proprietary
Project-URL: Documentation, https://pyscaffold.org/
Platform: any
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Description-Content-Type: text/x-rst; charset=UTF-8
License-File: LICENSE.txt
Requires-Dist: importlib-metadata; python_version < "3.9"
Requires-Dist: pydantic>=2.12.0
Requires-Dist: python-dotenv<=1.0.1
Provides-Extra: testing
Requires-Dist: setuptools; extra == "testing"
Requires-Dist: pytest; extra == "testing"
Requires-Dist: pytest-cov; extra == "testing"
Requires-Dist: coverage; extra == "testing"
Requires-Dist: parameterized; extra == "testing"
Requires-Dist: pyyaml; extra == "testing"
Dynamic: license-file

.. image:: https://img.shields.io/badge/-PyScaffold-005CA0?logo=pyscaffold
    :alt: Project generated with PyScaffold
    :target: https://pyscaffold.org/



===========
config-core
===========


    This is a opinionated way to parse a config.


This is a opinionated way to parse a config.
It requires `Pydantic BaseModel <https://docs.pydantic.dev/latest/api/base_model/>`_ config models.



Usage
*****

Below are some examples on how to use the `config_core.load_config` function from this package.


For `load_config` to work you'll need a data structure. These can be created in python with `Pydantic BaseModel <https://docs.pydantic.dev/latest/api/base_model/>`_


Basic example
=============

For `load_config` to work you'll need a data structure. These can be created in python with `Pydantic BaseModel <https://docs.pydantic.dev/latest/api/base_model/>`_


Simple data stucture example.

.. code-block:: python

   from pydantic import AfterValidator, BaseModel, ValidationError
   from typing import Set

   class NestingConfig(BaseModel):
       """Main config for node."""

       unique_int_set: Set = []
       example_boolean: bool = False


   class ConfigFile(BaseModel):
       """Top config level."""

       # Required top-level field: parsing should fail if missing
       service_id: int

       node_config: NestingConfig

Example config file to be parsed (for this example use "./path_to_config.yaml")


.. code-block:: yaml

   service_id: 1211
   node_config:
     unique_int_set:
       - 2
       - 8

This config file can be parsed with `config_core.load_config`


.. code-block:: python

   from config_core import load_config

   config: ConfigFile = load_config("./path_to_config.yaml", {}, ConfigFile)


This will load the config and parse it to an `ConfigFile` object.


Camel case example
==================

Camel case for inside the configs is also possible. Python can continue to use pascal_case.

.. code-block:: yaml

   serviceId: 1211
   nodeConfig:
     uniqueIntSet:
       - 2
       - 8

Key replacement
===============

You can also some keys in the data stucture with the `key_replacement` argument.


.. code-block:: yaml

   service_id: 1211
   type_of_node_running_this_config:
     unique_int_set:
       - 2
       - 8

And then in python

.. code-block:: python

   from config_core import load_config

   config: ConfigFile = load_config("./path_to_config.yaml", {"type_of_node_running_this_config": "node_config"}, ConfigFile)


Env variables
=============

Env variables inside the config is also possible

- Env variables here will be `SERVICE_ID`


.. code-block:: yaml

   service_id: ${SERVICE_ID}
   type_of_node_running_this_config:
     unique_int_set:
       - 2
       - 8

or


.. code-block:: yaml

   service_id: $SERVICE_ID
   type_of_node_running_this_config:
     unique_int_set:
       - 2
       - 8


Making Changes & Contributing
*****************************

This project uses `pre-commit`_, please make sure to install it before making any
changes::

    pip install pre-commit
    cd config-core
    pre-commit install

It is a good idea to update the hooks to the latest version::

    pre-commit autoupdate

.. _pre-commit: https://pre-commit.com/
