Skip to main content

Expand system variables Unix style

Reason this release was yanked:

This build was incomplete (outdated setuptools)

Project description

expandvars

Expand system variables Unix style

PyPI version CircleCI codecov

Inspiration

This module is inspired by GNU bash's variable expansion features. It can be used as an alternative to Python's os.path.expandvars function.

A good use case is reading config files with the flexibility of reading values from environment variables using advanced features like returning a default value if some variable is not defined. For example:

[default]
my_secret_access_code = "${ACCESS_CODE:-default_access_code}"
my_important_variable = "${IMPORTANT_VARIABLE:?}"
my_updated_path = "$PATH:$HOME/.bin"

NOTE: Although this module copies most of the common behaviours of bash, it doesn't follow bash strictly. For example, it doesn't work with arrays.

Usage

from expandvars import expandvars

print(expandvars("$PATH:${HOME:?}/bin:${SOME_UNDEFINED_PATH:-/default/path}"))
# /bin:/sbin:/usr/bin:/usr/sbin:/home/you/bin:/default/path

Examples

For now, refer to the test cases to see how it behaves.

TIPs

nounset=True

If you want to enable strict parsing by default, (similar to set -u / set -o nounset in bash), pass nounset=True.

# All the variables must be defined.
expandvars("$VAR1:${VAR2}:$VAR3", nounset=True)

# Raises UnboundVariable error.

NOTE: Another way is to use the ${VAR?} or ${VAR:?} syntax. See the examples in tests.

EXPANDVARS_RECOVER_NULL="foo"

If you want to temporarily disable strict parsing both for nounset=True and the ${VAR:?} syntax, set environment variable EXPANDVARS_RECOVER_NULL=somevalue. This helps with certain use cases where you need to temporarily disable strict parsing of critical env vars, e.g. in testing environment, without modifying the code.

e.g.

EXPANDVARS_RECOVER_NULL=foo myapp --config production.ini && echo "All fine."

WARNING: Try to avoid export EXPANDVARS_RECOVER_NULL because that will disable strict parsing permanently until you log out.

Contributing

To contribute, setup environment following way:

# Clone repo
git clone https://github.com/sayanarijit/expandvars && cd expandvars

# Create virtualenv
virtualenv .venv && source .venv/bin/activate

# Install library in edit mode along with other handy dev tools
pip install -r dev-requirements.txt
  • Follow general git guidelines.
  • Keep it simple. Use black to format code.
  • Test your changes locally by running pytest.
  • If you are familiar with tox, you may want to use it for testing in different python versions.

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

expandvars-0.6.0.macosx-10.15-x86_64.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

expandvars-0.6.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file expandvars-0.6.0.macosx-10.15-x86_64.tar.gz.

File metadata

  • Download URL: expandvars-0.6.0.macosx-10.15-x86_64.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for expandvars-0.6.0.macosx-10.15-x86_64.tar.gz
Algorithm Hash digest
SHA256 33987f10fd210e60e63f3079fd12a03341bf7fc40a75f9a284f7f14b033c1b9b
MD5 d1e99e991c6190c269917d5b6c66fdb6
BLAKE2b-256 1bcb7901b5b75bfe9a852ab831b98f350af68c7112dc3e9f640175cc96514fdb

See more details on using hashes here.

File details

Details for the file expandvars-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: expandvars-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

File hashes

Hashes for expandvars-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e227469cbd6ed9a1e15c604207830245e8e45d2cc8dcc4f5b036f5b85c8f728
MD5 f63a79b6eeb4c711686a0895f491c9a3
BLAKE2b-256 0c6c69a6c1e482daf9a1facaa806392071c7747c77b2f7db5b6fd1c93666babd

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