Package: piny
Pinyis 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 asenvironment variables.
Set environment variables on application deployment.
Now let thepinyload 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.RationalePiny combinesreadability and versioningyou get when using config files,
andsecuritythat environment variables provide. Read more about this approach
in theblog post.HelpSeedocumentationfor more details.InstallationJust run:pip install -U pinyUsageSet your environment variables, add them to your YAML configuration file:db:login:userpassword:${DB_PASSWORD}mail:login:userpassword:${MAIL_PASSWORD:-my_default_password}sentry:dsn:${VAR_NOT_SET}Then load your config:frompinyimportYamlLoaderconfig=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 aStrictMatcher:frompinyimportYamlLoader,StrictMatcherconfig=YamlLoader(path="config.yaml",matcher=StrictMatcher).load()Both strict and default matchers produceNonevalue if environment variable
matched is not set in the system (and no default syntax used in the case of
default matcher).Piny also comes withcommand line utilitythat works both with files and standard
input and output:$exportPASSWORD=mySecretPassword$echo"db: \${PASSWORD}"|pinydb:mySecretPasswordValidationPiny supportsoptionaldata validation using third-party libraries:Marshmallow,Pydantic,Trafaret.importmarshmallowasmafrompinyimportMarshmallowValidator,StrictMatcher,YamlLoaderclassDBSchema(ma.Schema):login=ma.fields.String(required=True)password=ma.fields.String()classConfigSchema(ma.Schema):db=ma.fields.Nested(DBSchema)config=YamlLoader(path="database.yaml",matcher=StrictMatcher,validator=MarshmallowValidator,schema=ConfigSchema,).load(many=False)ExceptionsLoadingErroris thrown when something goes wrong with reading or parsing YAML-file.ValidationErroris a wrapper for exceptions raised by the libraries for optional data validation.
Original exception can be accessed byoriginattribute. 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 theConfigError.Best practicesMaintain a healthy security/convenience balance for your configMark up entity as an environment variable in your YAML if and only if
it really is asecret(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 likeKubernetesmake it easier to keep envs secure
(seeKubernetes Secrets).Fun factsPinyis a recursive acronym forPiny Is Not YAML.
Not only it’s a library name, but also a name for YAML marked up
with environment variables.ChangelogSeeCHANGELOG.rst.ContributingSeeCONTRIBUTING.rst.