Skip to main content

Expand system variables Unix style

Project description

expandvars

Expand system variables Unix style

PyPI version 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:

First you need to install poetry.

Then

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

# Install poetry dependencies
poetry install
  • Follow general git guidelines.
  • Keep it simple. Run poetry run black to auto format the code.
  • Test your changes locally by running poetry run pytest (pass --cov --cov-report html for browsable coverage report).
  • 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.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

expandvars-0.6.2-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file expandvars-0.6.2.tar.gz.

File metadata

  • Download URL: expandvars-0.6.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Darwin/19.6.0

File hashes

Hashes for expandvars-0.6.2.tar.gz
Algorithm Hash digest
SHA256 293b3b1731e501a4abac7101748615d866325959874aaf9439bcb25ba6172c05
MD5 754a5e6571ca2ebd45389bb14da2ccd8
BLAKE2b-256 706fc3172d0639e6699d902ee8df1f60439c3c3538164f5f6c05b90500d303a4

See more details on using hashes here.

File details

Details for the file expandvars-0.6.2-py2.py3-none-any.whl.

File metadata

  • Download URL: expandvars-0.6.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Darwin/19.6.0

File hashes

Hashes for expandvars-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a8e01953d4365fe334eccf1f67e1db911d7a17d45327d2b87c40c1be81519eae
MD5 2556569acebff3499f99106b1044e864
BLAKE2b-256 a8b5c2e8253ddc1acfed26a19627690bb011b51b2884b66f6f9ae7b211a30e34

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