Skip to main content

Substitute variables in a string with a given resolver. Act as a render template.

Project description

Tests

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

varsubst-2.0.0.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

varsubst-2.0.0-py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 3

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