Code Monkey home page Code Monkey logo

expandvars's Introduction

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"
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_NULL because that will disable strict parsing permanently until you log out.

Customization

You can customize the variable symbol 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:-/default/path}", environ={"PATH": "/example"}, var_symbol="%"))
# /example:$HOME/bin:/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 html for 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.