Skip to main content

A configuration management toolkit

Project description

configuror

Pypi version Build Status Windows Build Status Coverage Status Documentation Status License Apache 2

Your configuration management toolkit!

Why?

While using Flask, 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. If you look the section 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

pip install configuror

configuror works starting from python 3.6. It has a few dependencies:

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

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

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.

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.

  • 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.

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

configuror-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

configuror-0.1.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file configuror-0.1.1.tar.gz.

File metadata

  • Download URL: configuror-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

File hashes

Hashes for configuror-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4f646d97458306d8c7fee27f9f02ea50c7d9553eaf3fd812d0aa7bed779d10f1
MD5 78e09654409c5772fc80ce64698497d6
BLAKE2b-256 fe632487f26615b8353a0e3ed77e417774614e85bda9f358c81ed1470cd672b3

See more details on using hashes here.

File details

Details for the file configuror-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: configuror-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

File hashes

Hashes for configuror-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0cff41a3a5c9df92f6a7f256c3721c18559553fbd8302c4f4e3e82802503590b
MD5 db913ff83b746769ed8a27b31062c1e9
BLAKE2b-256 0ab7076d04cb3873070a88ea248680dec4643912db9fbc8c3920f93f0edbf6bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page