configuror


Nameconfiguror JSON
Version 0.3.0 PyPI version JSON
download
home_pagehttps://configuror.readthedocs.io/en/stable
SummaryA configuration management toolkit
upload_time2023-11-28 08:01:51
maintainer
docs_urlNone
authorlewoudar
requires_python>=3.8,<4.0
licenseApache-2.0
keywords configuration yaml toml dotenv ini
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # configuror

[![Pypi version](https://img.shields.io/pypi/v/configuror.svg)](https://pypi.org/project/configuror/)
[![](https://github.com/lewoudar/configuror/workflows/CI/badge.svg)](https://github.com/lewoudar/configuror/actions)
[![Coverage Status](https://codecov.io/gh/lewoudar/configuror/branch/master/graphs/badge.svg?branch=master)](https://codecov.io/gh/lewoudar/configuror)
[![Documentation Status](https://readthedocs.org/projects/configuror/badge/?version=latest)](https://configuror.readthedocs.io/en/latest/?badge=latest)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/lewoudar/configuror)
[![License Apache 2](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://github.com/lewoudar/configuror)

Your configuration management toolkit!

## Why?

While using [Flask](http://flask.pocoo.org/docs/1.0/), I realized that their Config class could be useful for any type
of project. And the utility became more and more obvious to me when I looked at a project like
[Ansible](https://docs.ansible.com/ansible/latest/index.html). If you look the
[section](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)
where they define variable precedence, you will notice that there may be several locations for the configuration files,
and these configuration files can be written in different formats (json, yaml..).

What if there was a simple tool that would aggregate information from different sources in the order you wanted? This
is **why** the configuror project exists!

## Installation

```bash
pip install configuror
```

configuror works starting from **python 3.7**. It has a few dependencies:
- [pyyaml](https://pypi.org/project/PyYAML/) >= 5.1
- [toml](https://pypi.org/project/toml/)

## Documentation

The documentation is available at https://configuror.readthedocs.io/en/latest/.

## Usage

The main class provided by configuror is `Config`. It is an extension of a regular dict object. There are two main ways
to initialize it.

### Using mapping files

```python
from configuror import Config

mapping_files = {
    'ini': ['foo.ini', 'bar.ini'],
    'toml': ['foo.toml', 'bar.toml'],
    'python': ['/path/to/python/file']
}
config = Config(mapping_files=mapping_files, ignore_file_absence=True)
```

You can define a mapping of `file_type: <files>` where the `file_type` is the type of configuration file and `<files>`
is the list of files from the lowest to the highest priority where values will be loaded.

Since dictionaries are sorted starting from python3.6, the order of the keys is important as it will become the order of
importance of your files. For example in the example above, configuror will load values from files in the following order:
- foo.ini
- bar.ini
- foo.toml
- bar.toml
- /path/to/python/file

For python files, only **uppercase** variables will be loaded.

You will notice the keyword argument `ignore_file_absence` in `Config` class initialization. If it is set to `True`, all
files that does not exist will not raised `FileNotFoundError`. It comes in handy when you want to retrieve variables
from files *that may or may not potentially exist*. By default this parameter is set to `False`.

File extension is not necessary when you use mapping files since the key is already telling which files we work with.
This is not the case with the second way to initialize `Config` class.

### Using a list of files

```python
from configuror import Config

files = [
    'foo.yml',
    'bar.toml',
    'foobar.json',
    '/path/to/python/file'
    '.env'
]
config = Config(files=files)
```

In this second form of initialization, you pass a list of files you want to retrieve values from the lowest to the
highest priority. File extension is **mandatory** here to help configuror to load the files properly.

To know file extensions supported by configuror, you can use the variable `EXTENSIONS`. it is a mapping
`file_type: <extensions>` where `file_type` is a type of file supported like *yaml* and `extensions` is a list of
recognized extensions for this type of file, e.g: `[yml, yaml]`

Today the file types supported are *toml*, *yaml*, *dotenv*, *ini*, *python* and *json*.

### Other usages

Since `Config` object is a dict-like object, you can pass arbitrary keyword arguments to initialize default values.

```python
from configuror import Config

config = Config(FOO=2, BAR='a')
print(config)  # will print {'FOO': 2, 'BAR': 'a'}
```

You can combine keyword arguments, mapping files and list of files at initialization. The order in which values will be
initialized is the following:
- values from keyword arguments
- values from mapping files
- values from list of files

You can also add values from files after initialization. There are several practical methods for this:
- `load_from_mapping_files(self, mapping_files: Dict[str, List[str]], ignore_file_absence: bool)`: It is in fact the
method used under the hood when you initialized `Config` object by passing the parameter `mapping_files`.

- `load_from_files(self, files: List[str], ignore_file_absence: bool)`: It is the method used under the hood when you
initialized `Config` objects by passing the parameter `files`.

- `load_from_object(self, obj: Union[Object, str])`: `obj` can be an object or a path to a project module
(with dotted notation). Only uppercase attributes of the corresponding object will be retrieved.

- `load_from_python_file(self, filename: str, ignore_file_absence: bool)`: Loads values from an arbitrary python
file. It would be preferable if it were not a file related to your project (i.e a module). Only uppercase variables
are considered.

- `load_from_json(self, filename: str, ignore_file_absence: bool)`: Loads values from a json file.

- `load_from_yaml(self, filename: str, ignore_file_absence: bool)`: Loads values from a yaml file.

- `load_from_toml(self, filenames: Union[str, List[str]], ignore_file_absence: bool)`: Loads values from a toml file
or a list of toml files.

- `load_from_ini(self, filenames: Union[str, List], ignore_file_absence: bool, interpolation_method: str = 'basic')`:
Loads values from an ini file or a list of ini files. There are two interpolation methods that can be used: **basic**
or **extended** like explained in the
[documentation](https://docs.python.org/3/library/configparser.html#interpolation-of-values).

- `load_from_dotenv(self, filename: str, ignore_file_absence: bool)`: Loads values from a dotenv file.

Bonus: You also have the `update` method of a dict to add/update values.

            

Raw data

            {
    "_id": null,
    "home_page": "https://configuror.readthedocs.io/en/stable",
    "name": "configuror",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "configuration,yaml,toml,dotenv,ini",
    "author": "lewoudar",
    "author_email": "lewoudar@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8b/76/c17f26b797a73e4cb241d7a233cf89656137ed770e621efefaae03173e9a/configuror-0.3.0.tar.gz",
    "platform": null,
    "description": "# configuror\n\n[![Pypi version](https://img.shields.io/pypi/v/configuror.svg)](https://pypi.org/project/configuror/)\n[![](https://github.com/lewoudar/configuror/workflows/CI/badge.svg)](https://github.com/lewoudar/configuror/actions)\n[![Coverage Status](https://codecov.io/gh/lewoudar/configuror/branch/master/graphs/badge.svg?branch=master)](https://codecov.io/gh/lewoudar/configuror)\n[![Documentation Status](https://readthedocs.org/projects/configuror/badge/?version=latest)](https://configuror.readthedocs.io/en/latest/?badge=latest)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/lewoudar/configuror)\n[![License Apache 2](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://github.com/lewoudar/configuror)\n\nYour configuration management toolkit!\n\n## Why?\n\nWhile using [Flask](http://flask.pocoo.org/docs/1.0/), I realized that their Config class could be useful for any type\nof project. And the utility became more and more obvious to me when I looked at a project like\n[Ansible](https://docs.ansible.com/ansible/latest/index.html). If you look the\n[section](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)\nwhere they define variable precedence, you will notice that there may be several locations for the configuration files,\nand these configuration files can be written in different formats (json, yaml..).\n\nWhat if there was a simple tool that would aggregate information from different sources in the order you wanted? This\nis **why** the configuror project exists!\n\n## Installation\n\n```bash\npip install configuror\n```\n\nconfiguror works starting from **python 3.7**. It has a few dependencies:\n- [pyyaml](https://pypi.org/project/PyYAML/) >= 5.1\n- [toml](https://pypi.org/project/toml/)\n\n## Documentation\n\nThe documentation is available at https://configuror.readthedocs.io/en/latest/.\n\n## Usage\n\nThe main class provided by configuror is `Config`. It is an extension of a regular dict object. There are two main ways\nto initialize it.\n\n### Using mapping files\n\n```python\nfrom configuror import Config\n\nmapping_files = {\n    'ini': ['foo.ini', 'bar.ini'],\n    'toml': ['foo.toml', 'bar.toml'],\n    'python': ['/path/to/python/file']\n}\nconfig = Config(mapping_files=mapping_files, ignore_file_absence=True)\n```\n\nYou can define a mapping of `file_type: <files>` where the `file_type` is the type of configuration file and `<files>`\nis the list of files from the lowest to the highest priority where values will be loaded.\n\nSince dictionaries are sorted starting from python3.6, the order of the keys is important as it will become the order of\nimportance of your files. For example in the example above, configuror will load values from files in the following order:\n- foo.ini\n- bar.ini\n- foo.toml\n- bar.toml\n- /path/to/python/file\n\nFor python files, only **uppercase** variables will be loaded.\n\nYou will notice the keyword argument `ignore_file_absence` in `Config` class initialization. If it is set to `True`, all\nfiles that does not exist will not raised `FileNotFoundError`. It comes in handy when you want to retrieve variables\nfrom files *that may or may not potentially exist*. By default this parameter is set to `False`.\n\nFile extension is not necessary when you use mapping files since the key is already telling which files we work with.\nThis is not the case with the second way to initialize `Config` class.\n\n### Using a list of files\n\n```python\nfrom configuror import Config\n\nfiles = [\n    'foo.yml',\n    'bar.toml',\n    'foobar.json',\n    '/path/to/python/file'\n    '.env'\n]\nconfig = Config(files=files)\n```\n\nIn this second form of initialization, you pass a list of files you want to retrieve values from the lowest to the\nhighest priority. File extension is **mandatory** here to help configuror to load the files properly.\n\nTo know file extensions supported by configuror, you can use the variable `EXTENSIONS`. it is a mapping\n`file_type: <extensions>` where `file_type` is a type of file supported like *yaml* and `extensions` is a list of\nrecognized extensions for this type of file, e.g: `[yml, yaml]`\n\nToday the file types supported are *toml*, *yaml*, *dotenv*, *ini*, *python* and *json*.\n\n### Other usages\n\nSince `Config` object is a dict-like object, you can pass arbitrary keyword arguments to initialize default values.\n\n```python\nfrom configuror import Config\n\nconfig = Config(FOO=2, BAR='a')\nprint(config)  # will print {'FOO': 2, 'BAR': 'a'}\n```\n\nYou can combine keyword arguments, mapping files and list of files at initialization. The order in which values will be\ninitialized is the following:\n- values from keyword arguments\n- values from mapping files\n- values from list of files\n\nYou can also add values from files after initialization. There are several practical methods for this:\n- `load_from_mapping_files(self, mapping_files: Dict[str, List[str]], ignore_file_absence: bool)`: It is in fact the\nmethod used under the hood when you initialized `Config` object by passing the parameter `mapping_files`.\n\n- `load_from_files(self, files: List[str], ignore_file_absence: bool)`: It is the method used under the hood when you\ninitialized `Config` objects by passing the parameter `files`.\n\n- `load_from_object(self, obj: Union[Object, str])`: `obj` can be an object or a path to a project module\n(with dotted notation). Only uppercase attributes of the corresponding object will be retrieved.\n\n- `load_from_python_file(self, filename: str, ignore_file_absence: bool)`: Loads values from an arbitrary python\nfile. It would be preferable if it were not a file related to your project (i.e a module). Only uppercase variables\nare considered.\n\n- `load_from_json(self, filename: str, ignore_file_absence: bool)`: Loads values from a json file.\n\n- `load_from_yaml(self, filename: str, ignore_file_absence: bool)`: Loads values from a yaml file.\n\n- `load_from_toml(self, filenames: Union[str, List[str]], ignore_file_absence: bool)`: Loads values from a toml file\nor a list of toml files.\n\n- `load_from_ini(self, filenames: Union[str, List], ignore_file_absence: bool, interpolation_method: str = 'basic')`:\nLoads values from an ini file or a list of ini files. There are two interpolation methods that can be used: **basic**\nor **extended** like explained in the\n[documentation](https://docs.python.org/3/library/configparser.html#interpolation-of-values).\n\n- `load_from_dotenv(self, filename: str, ignore_file_absence: bool)`: Loads values from a dotenv file.\n\nBonus: You also have the `update` method of a dict to add/update values.\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "A configuration management toolkit",
    "version": "0.3.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/lewoudar/configuror/issues",
        "Documentation": "https://configuror.readthedocs.io/en/stable/usage",
        "Homepage": "https://configuror.readthedocs.io/en/stable",
        "Repository": "https://github.com/lewoudar/configuror"
    },
    "split_keywords": [
        "configuration",
        "yaml",
        "toml",
        "dotenv",
        "ini"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "60b85d8a43babef450728892cb84505ae3738eea02180226eb848891d3ace7be",
                "md5": "31510c4691a393fc202bdaac20f870c8",
                "sha256": "42bab4800fa5651947a358616504528329f2f2294e01ae63fbb5300729b5a205"
            },
            "downloads": -1,
            "filename": "configuror-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "31510c4691a393fc202bdaac20f870c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 12996,
            "upload_time": "2023-11-28T08:01:49",
            "upload_time_iso_8601": "2023-11-28T08:01:49.203758Z",
            "url": "https://files.pythonhosted.org/packages/60/b8/5d8a43babef450728892cb84505ae3738eea02180226eb848891d3ace7be/configuror-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b76c17f26b797a73e4cb241d7a233cf89656137ed770e621efefaae03173e9a",
                "md5": "69d74ba3a9f2e552458635e107d510b1",
                "sha256": "0c65a20d90bd2b0e9653251c3404b8354dbdf328c070acf4842393ce1ca4953f"
            },
            "downloads": -1,
            "filename": "configuror-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "69d74ba3a9f2e552458635e107d510b1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 13946,
            "upload_time": "2023-11-28T08:01:51",
            "upload_time_iso_8601": "2023-11-28T08:01:51.781921Z",
            "url": "https://files.pythonhosted.org/packages/8b/76/c17f26b797a73e4cb241d7a233cf89656137ed770e621efefaae03173e9a/configuror-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-28 08:01:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lewoudar",
    "github_project": "configuror",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "configuror"
}
        
Elapsed time: 0.14412s