expandvars
Expand system variables Unix style
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"
my_process_id = "$$"
my_nested_variable = "${!NESTED}"
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.
Installation
Pip
pip install expandvars
Conda
conda install -c conda-forge expandvars
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_NULLbecause that will disable strict parsing permanently until you log out.
Customization
You can customize the variable symbol, escape character, whether to expand non-surrounded variables and data used for the expansion by using the more general expand function.
from expandvars import expand
print(expand("%PATH:$HOME/bin:%{SOME_UNDEFINED_PATH:-D:\\default\\path}", environ={"PATH": "/example"}, var_symbol="%", surrounded_vars_only=True, escape_char=""))
# %PATH:$HOME/bin:D:\default\path
Contributing
To contribute, setup environment following way:
Then
# Clone repo
git clone https://github.com/sayanarijit/expandvars && cd expandvars
# Setup virtualenv
python -m venv .venv
source ./.venv/bin/activate
# Install as editable including test dependencies
pip install -e ".[tests]"
- Follow general git guidelines.
- Keep it simple. Run
black .to auto format the code. - Test your changes locally by running
pytest(pass--cov --cov-report htmlfor browsable coverage report). - If you are familiar with tox, you may want to use it for testing in different python versions.
Alternatives
- environs - simplified environment variable parsing.
Release files for expandvars 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| expandvars-1.1.2.tar.gz | 70.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| expandvars-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 78.4 kB
Release files / expandvars-1.1.2.tar.gz
| Download URL | expandvars-1.1.2.tar.gz |
|---|---|
| Size | 70.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c5822b7b756a99a356b915dd1267f52ab8a4efaa135963bd7f4bd5d368f71d7
|
|
BLAKE2b-256 checksum How to use checksums |
9c64a9d8ea289d663a44b346203a24bf798507463db1e76679eaa72ee6de1c7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / expandvars-1.1.2-py3-none-any.whl
| Download URL | expandvars-1.1.2-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1652fe4e61914f5b88ada93aaedb396446f55ae4621de45c8cb9f66e5712526
|
|
BLAKE2b-256 checksum How to use checksums |
7fe679c43f7a55264e479a9fbf21ddba6a73530b3ea8439a8bb7fa5a281721af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|