configurator


Nameconfigurator JSON
Version 3.2.0 PyPI version JSON
download
home_pagehttps://github.com/Simplistix/configurator
SummaryA library for building a configuration store from one or more layered configuration sources
upload_time2023-09-13 07:03:15
maintainer
docs_urlNone
authorChris Withers
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
configurator
============

|CircleCI|_  |Docs|_

.. |CircleCI| image:: https://circleci.com/gh/simplistix/configurator/tree/master.svg?style=shield
.. _CircleCI: https://circleci.com/gh/simplistix/configurator/tree/master

.. |Docs| image:: https://readthedocs.org/projects/configurator/badge/?version=latest
.. _Docs: http://configurator.readthedocs.org/en/latest/

This is a Python library for building a configuration store
from one or more layered configuration sources.
These are most commonly files, with YAML, TOML and JSON support included
and other formats easily added.
The sources don't have to be files, and support is included for both environment
variables and command line options.

In addition to an easy to use interface, configuration information is also made available
as nested, simple python data types so that you can validate the schema of your configuration
using the tool of your choice.

Quickstart
~~~~~~~~~~

.. invisible-code-block: python

    fs.create_file('/etc/my_app/config.yaml',
                   contents='cache:\n  location: /var/my_app/\n')
    fs.create_dir('/var/logs/myapp/')
    replace('os.environ.MYAPP_THREADS', '2', strict=False)
    replace('os.environ.MYAPP_CACHE_DIRECTORY', '/var/logs/myapp/', strict=False)
    replace('sys.argv', ['myapp.py', '--threads', '3', '--max-files', '200'])
    from pprint import pprint

To install the library, go for:

.. code-block:: bash

  pip install configurator[yaml,toml]

Here's how you would handle a layered set of defaults, system-wide config
and then optional per-user config:

.. code-block:: python


    from configurator import Config

    defaults = Config({
        'cache': {
            'location': '/tmp/my_app',
            'max_files': 100,
        },
        'banner': 'default banner',
        'threads': 1,
    })
    system = Config.from_path('/etc/my_app/config.yaml')
    user = Config.from_path('~/.my_app.yaml', optional=True)
    config = defaults + system + user

Now, if we wanted configuration from the environment and command line
arguments to override those provided in configuration files, we could do so
as follows:

.. code-block:: python

    import os
    from argparse import ArgumentParser
    from configurator import convert, target, required

    config.merge(os.environ, {
        convert('MYAPP_THREADS', int): 'threads',
        required('MYAPP_CACHE_DIRECTORY'): 'cache.location',
    })

    parser = ArgumentParser()
    parser.add_argument('--threads', type=int)
    parser.add_argument('--max-files', type=int)
    args = parser.parse_args()

    config.merge(args, {
        'threads': 'threads',
        'max_files': 'cache.max_files',
    })

To check the configuration we've accumulated is sensible we can use a data validation library
such as `Voluptuous`__:

__ https://github.com/alecthomas/voluptuous

.. code-block:: python

    from os.path import exists
    from voluptuous import Schema, All, Required, PathExists

    schema = Schema({
        'cache': {'location': All(str, PathExists()), 'max_files': int},
        'banner': Required(str),
        'threads': Required(int),
        })

    schema(config.data)

So, with all of the above, we could use the following sources of configuration:

>>> import os, sys
>>> print(open('/etc/my_app/config.yaml').read())
cache:
  location: /var/my_app/
<BLANKLINE>
>>> os.environ['MYAPP_THREADS']
'2'
>>> os.environ['MYAPP_CACHE_DIRECTORY']
'/var/logs/myapp/'
>>> sys.argv
['myapp.py', '--threads', '3', '--max-files', '200']

With the above sources of configuration, we'd end up with a configuration store that we can use as
follows:

>>> config.cache.location
'/var/logs/myapp/'
>>> config.cache.max_files
200
>>> config.banner
'default banner'
>>> config.threads
3

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Simplistix/configurator",
    "name": "configurator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Chris Withers",
    "author_email": "chris@withers.org",
    "download_url": "https://files.pythonhosted.org/packages/62/06/7c27b07622df52e2591ea581485afb84bbc3386493bb47a2f6992fe7b78c/configurator-3.2.0.tar.gz",
    "platform": null,
    "description": "\nconfigurator\n============\n\n|CircleCI|_  |Docs|_\n\n.. |CircleCI| image:: https://circleci.com/gh/simplistix/configurator/tree/master.svg?style=shield\n.. _CircleCI: https://circleci.com/gh/simplistix/configurator/tree/master\n\n.. |Docs| image:: https://readthedocs.org/projects/configurator/badge/?version=latest\n.. _Docs: http://configurator.readthedocs.org/en/latest/\n\nThis is a Python library for building a configuration store\nfrom one or more layered configuration sources.\nThese are most commonly files, with YAML, TOML and JSON support included\nand other formats easily added.\nThe sources don't have to be files, and support is included for both environment\nvariables and command line options.\n\nIn addition to an easy to use interface, configuration information is also made available\nas nested, simple python data types so that you can validate the schema of your configuration\nusing the tool of your choice.\n\nQuickstart\n~~~~~~~~~~\n\n.. invisible-code-block: python\n\n    fs.create_file('/etc/my_app/config.yaml',\n                   contents='cache:\\n  location: /var/my_app/\\n')\n    fs.create_dir('/var/logs/myapp/')\n    replace('os.environ.MYAPP_THREADS', '2', strict=False)\n    replace('os.environ.MYAPP_CACHE_DIRECTORY', '/var/logs/myapp/', strict=False)\n    replace('sys.argv', ['myapp.py', '--threads', '3', '--max-files', '200'])\n    from pprint import pprint\n\nTo install the library, go for:\n\n.. code-block:: bash\n\n  pip install configurator[yaml,toml]\n\nHere's how you would handle a layered set of defaults, system-wide config\nand then optional per-user config:\n\n.. code-block:: python\n\n\n    from configurator import Config\n\n    defaults = Config({\n        'cache': {\n            'location': '/tmp/my_app',\n            'max_files': 100,\n        },\n        'banner': 'default banner',\n        'threads': 1,\n    })\n    system = Config.from_path('/etc/my_app/config.yaml')\n    user = Config.from_path('~/.my_app.yaml', optional=True)\n    config = defaults + system + user\n\nNow, if we wanted configuration from the environment and command line\narguments to override those provided in configuration files, we could do so\nas follows:\n\n.. code-block:: python\n\n    import os\n    from argparse import ArgumentParser\n    from configurator import convert, target, required\n\n    config.merge(os.environ, {\n        convert('MYAPP_THREADS', int): 'threads',\n        required('MYAPP_CACHE_DIRECTORY'): 'cache.location',\n    })\n\n    parser = ArgumentParser()\n    parser.add_argument('--threads', type=int)\n    parser.add_argument('--max-files', type=int)\n    args = parser.parse_args()\n\n    config.merge(args, {\n        'threads': 'threads',\n        'max_files': 'cache.max_files',\n    })\n\nTo check the configuration we've accumulated is sensible we can use a data validation library\nsuch as `Voluptuous`__:\n\n__ https://github.com/alecthomas/voluptuous\n\n.. code-block:: python\n\n    from os.path import exists\n    from voluptuous import Schema, All, Required, PathExists\n\n    schema = Schema({\n        'cache': {'location': All(str, PathExists()), 'max_files': int},\n        'banner': Required(str),\n        'threads': Required(int),\n        })\n\n    schema(config.data)\n\nSo, with all of the above, we could use the following sources of configuration:\n\n>>> import os, sys\n>>> print(open('/etc/my_app/config.yaml').read())\ncache:\n  location: /var/my_app/\n<BLANKLINE>\n>>> os.environ['MYAPP_THREADS']\n'2'\n>>> os.environ['MYAPP_CACHE_DIRECTORY']\n'/var/logs/myapp/'\n>>> sys.argv\n['myapp.py', '--threads', '3', '--max-files', '200']\n\nWith the above sources of configuration, we'd end up with a configuration store that we can use as\nfollows:\n\n>>> config.cache.location\n'/var/logs/myapp/'\n>>> config.cache.max_files\n200\n>>> config.banner\n'default banner'\n>>> config.threads\n3\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A library for building a configuration store from one or more layered configuration sources",
    "version": "3.2.0",
    "project_urls": {
        "Homepage": "https://github.com/Simplistix/configurator"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "99f8dbc0d0b77eb72fbcfc94446d3b67049575d963a99280a3a41079cbce3cd7",
                "md5": "3f0c7059b9a3ce94490fcae405a99453",
                "sha256": "be39c84f9a9aafd09b3c34acdc267ca4a5804e51975bfdd01cb2d433e9856766"
            },
            "downloads": -1,
            "filename": "configurator-3.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3f0c7059b9a3ce94490fcae405a99453",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 12655,
            "upload_time": "2023-09-13T07:03:13",
            "upload_time_iso_8601": "2023-09-13T07:03:13.773429Z",
            "url": "https://files.pythonhosted.org/packages/99/f8/dbc0d0b77eb72fbcfc94446d3b67049575d963a99280a3a41079cbce3cd7/configurator-3.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "62067c27b07622df52e2591ea581485afb84bbc3386493bb47a2f6992fe7b78c",
                "md5": "963b60ca7722d5542f29fd191b2c736a",
                "sha256": "42650ea55e7df5e4601c1c3d137d1b0ea0bec332448947627c368e0254a36043"
            },
            "downloads": -1,
            "filename": "configurator-3.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "963b60ca7722d5542f29fd191b2c736a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 35490,
            "upload_time": "2023-09-13T07:03:15",
            "upload_time_iso_8601": "2023-09-13T07:03:15.373819Z",
            "url": "https://files.pythonhosted.org/packages/62/06/7c27b07622df52e2591ea581485afb84bbc3386493bb47a2f6992fe7b78c/configurator-3.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-13 07:03:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Simplistix",
    "github_project": "configurator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "circle": true,
    "lcname": "configurator"
}
        
Elapsed time: 0.63465s