Substitute variables in a string with a given resolver. Act as a render template.
Project description
Varsubst is a rewrite of envsubst.
Varsubst render templates from a string with a given resolver.
Currently varsubst support shell-like variables.
Entry-point has currently been removed. It may be added in the future.
Resolvers provided are :
- EnvResolver : provide value based on environnement variables.
- DictResolver : provide value based on a given python dictionary.
Supported template variables
Varsubst support shell-like variables which are defined as follows:
- $MY_SIMPLE_VAR or ${MY_SIMPLE_VAR}: will resolve the variable MY_SIMPLE_VAR
- ${USER-default} : will resolve the variable USER. If variable is unset then default string is returned.
- ${USER-$DEFAULT_USER} : will resolve the variable USER. If variable is unset, resolve the variable DEFAULT_USER.
- ${USER:-default} : will resolve the variable USER. If variable is unset or empty then default string is returned.
- ${USER-$DEFAULT_USER} : will resolve the variable USER. If variable is unset or empty, resolve the variable DEFAULT_USER.
Usage
from varsubst import varsubst
from varsubst.resolvers import DictResolver
from varsubst.interpolators.jinja_interpolator import JinjaInterpolator
print(varsubst('$USER')) # result : 'tiboun'
print(varsubst('$UNDEFINED')) # result : KeyUnresolvedException('UNDEFINED')
print(varsubst('$UNDEFINED', fail_on_unresolved=False)) # result : ''
print(varsubst('$UNDEFINED', fail_on_unresolved=False)) # result : ''
print(varsubst('$USER', resolver=DictResolver({'USER': 'tiboun'})))
print(varsubst('{{ USER }}', interpolator=JinjaInterpolator()))
Extras
You may install varsubst[jinja2] as well if you intend to interpolate template with Jinja. If you plan to use jinja2 only, you may install it yourself in your project instead of using this one.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file varsubst-2.0.0.tar.gz
.
File metadata
- Download URL: varsubst-2.0.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54ed52fbc99ac085edd43c2d5e31e3bbd5f81fef39d4fd3b8bc180c65a908ec1 |
|
MD5 | 1b25827205dd7e2428d76c3aa533491b |
|
BLAKE2b-256 | 6e5a1a195032b804fc31724c92f40b699366627403816dc92327762d7fc698ec |
File details
Details for the file varsubst-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: varsubst-2.0.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eab0164e903180504143882c89f02576cceb097c29a341d92352171312c854ee |
|
MD5 | c18384ada04896323abf7d11a887f4d0 |
|
BLAKE2b-256 | 4d14d0e330807284bbb151cd5d6ecda611aed99d2e894876ae384dc9b7ff13bd |