Skip to main content

Piny

Piny logo

PyPI Codecov PyPI - Downloads MIT License

Piny is YAML config loader with environment variables interpolation for Python.

Keep your app’s configuration in a YAML file. Mark up sensitive data in the config as environment variables. Set environment variables on application deployment. Now let the piny load your config and substitute environment variables in it with their values.

Piny is developed with Docker and Kubernetes in mind, though it’s not limited to any deployment system.

Rationale

Piny combines readability and versioning you get when using config files, and security that environment variables provide. Read more about this approach in the blog post.

Help

See documentation for more details.

Installation

Just run:

pip install -U piny

Usage

Set your environment variables, add them to your YAML configuration file:

db:
  login: user
  password: ${DB_PASSWORD}
mail:
  login: user
  password: ${MAIL_PASSWORD:-my_default_password}
sentry:
  dsn: ${VAR_NOT_SET}

Then load your config:

from piny import YamlLoader

config = YamlLoader(path="config.yaml").load()
print(config)
# {'db': {'login': 'user', 'password': 'my_db_password'},
# 'mail': {'login': 'user', 'password': 'my_default_password'},
# 'sentry': {'dsn': None}}

You may want to discourage Bash-style envs with defaults in your configs. In such case, use a StrictMatcher:

from piny import YamlLoader, StrictMatcher

config = YamlLoader(path="config.yaml", matcher=StrictMatcher).load()

Both strict and default matchers produce None value if environment variable matched is not set in the system (and no default syntax used in the case of default matcher).

Piny also comes with command line utility that works both with files and standard input and output:

$ export PASSWORD=mySecretPassword
$ echo "db: \${PASSWORD}" | piny
db: mySecretPassword

Validation

Piny supports optional data validation using third-party libraries: Marshmallow, Pydantic, Trafaret.

import marshmallow as ma
from piny import MarshmallowValidator, StrictMatcher, YamlLoader

class DBSchema(ma.Schema):
    login = ma.fields.String(required=True)
    password = ma.fields.String()

class ConfigSchema(ma.Schema):
    db = ma.fields.Nested(DBSchema)

config = YamlLoader(
    path="database.yaml",
    matcher=StrictMatcher,
    validator=MarshmallowValidator,
    schema=ConfigSchema,
).load(many=False)

Exceptions

LoadingError is thrown when something goes wrong with reading or parsing YAML-file. ValidationError is a wrapper for exceptions raised by the libraries for optional data validation. Original exception can be accessed by origin attribute. It comes in handy when you need more than just an original exception message (e.g. a dictionary of validation errors).

Both exceptions inherit from the ConfigError.

Best practices

  • Maintain a healthy security/convenience balance for your config

  • Mark up entity as an environment variable in your YAML if and only if it really is a secret (login/passwords, private API keys, crypto keys, certificates, or maybe DB hostname too? You decide)

  • When loading config file, validate your data. Piny supports a few popular data validation tools.

  • Store your config files in the version control system along with your app’s code.

  • Environment variables are set by whoever is responsible for the deployment. Modern orchestration systems like Kubernetes make it easier to keep envs secure (see Kubernetes Secrets).

Fun facts

Piny is a recursive acronym for Piny Is Not YAML. Not only it’s a library name, but also a name for YAML marked up with environment variables.

Changelog

See CHANGELOG.rst.

Contributing

See CONTRIBUTING.rst.

Metadata

Release files for piny 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for piny 1.1.0
File Size Uploaded
piny-1.1.0.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for piny 1.1.0
File Interpreter ABI Platform
piny-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.3 kB

Release files / piny-1.1.0.tar.gz

Download URL piny-1.1.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
18a005aeccb2af6f55790e539698865be4ab546661c27f410ec6871afe10118a
BLAKE2b-256 checksum
How to use checksums
16fd2bee5e63b6bd59f98af82870c9d488478814b6ae6490c990151687c4476b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / piny-1.1.0-py3-none-any.whl

Download URL piny-1.1.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
adc6873192dead13118e588901348cbb94c8163b128a34ed277694a875e05993
BLAKE2b-256 checksum
How to use checksums
7d49470cc549c81f0d7e18fab2fc7c49fb01b4b244ba3825cac2abbec9ab9824
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page