Skip to main content

YAML configuration parser with validation.

Project description

.. image:: https://img.shields.io/pypi/v/aio_yamlconfig.svg
:target: https://pypi.org/project/aio_yamlconfig

.. image:: https://img.shields.io/travis/rrader/aio_yamlconfig/master.svg
:target: http://travis-ci.org/rrader/aio_yamlconfig

.. image:: https://img.shields.io/pypi/pyversions/aio_yamlconfig.svg

.. image:: https://img.shields.io/pypi/dm/aio_yamlconfig.svg


aio_yamlconfig
========

Quick Start
------------------

Install from PYPI:

.. code:: shell

pip install aio_yamlconfig

OR (less popular) via setup.py:

.. code:: shell

python -m setup install

YAML configuration parser with validation using `Trafaret <http://trafaret.readthedocs.org/en/latest/>`_.

In the easiest setup without config validation, configure your ``aiohttp`` application with:

.. code:: python
:number-lines:

CONFIG_FILE = os.path.join(os.path.dirname(__file__), "config.yaml")
loop.run_until_complete(aio_yamlconfig.setup(app,
config_files=[CONFIG_FILE],
base_dir=os.path.dirname(__file__)))

Assume you have ``config.yaml``:

.. code:: yaml
:number-lines:

DEBUG: True
TEMPLATES_DIR: !BaseDir path/to/templates

Then you can access your config as:

.. code:: python
:number-lines:

if app.config['DEBUG']:
print('some debug information')

Notice the ``!BaseDir`` tag. ``aio_yamlconfig`` can do some config transformations for you, in this case it will prepend
the base directory (passed as ``base_dir`` to ``setup()``) to your path. The variable ``app.config['TEMPLATES_DIR']`` will contain
the full path to directory with your templates.

Validation
--------------------

To validate your config we use the great library Trafaret. You can read more about it `in the docs <http://trafaret.readthedocs.org/en/latest/>`_.
Here I'll give you a simple example of its usage.

Let's write the validator for ``config.yaml`` above. We'd like to ensure that ``DEBUG`` value is boolean, and that
directory of the path ``TEMPLATES_DIR`` really exists:

.. code:: python
:number-lines:

import trafaret as t
from aio_yamlconfig.trafarets import ExistingDirectory

CONFIG_TRAFARET = t.Dict({
t.Key('TEMPLATES_DIR'): ExistingDirectory,
t.Key('DEBUG'): t.Bool
})


To enable such validation pass the ``trafaret_validator`` to ``setup()`` function:

.. code:: python
:number-lines:

loop.run_until_complete(aio_yamlconfig.setup(app,
config_files=[CONFIG_FILE],
trafaret_validator=CONFIG_TRAFARET,
base_dir=os.path.dirname(__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

aio_yamlconfig-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

aio_yamlconfig-0.2.0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aio_yamlconfig-0.2.0.tar.gz.

File metadata

File hashes

Hashes for aio_yamlconfig-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e5812675c2c9003b84940d84721a0267e29673c41234de6513f9e766c8cf06ea
MD5 eb038b1e70cae09410755d4f28bdca98
BLAKE2b-256 fef2e6650a5eac42893eb0d68bc9fe91283c4d0dee5535d446b91f2064e7dc28

See more details on using hashes here.

File details

Details for the file aio_yamlconfig-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for aio_yamlconfig-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 69d034a4f8e68d2c2e1413bba266752e691ccf671ef3b9edf8990b79f11eb613
MD5 7a36be9cae14e66b9ddae80386d8c72e
BLAKE2b-256 d0d5cb4ca872e8bc0330674e200454b6980dfd3dd63a4676f52aef5d5cc70636

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