All Projects → sayanarijit → expandvars

sayanarijit / expandvars

Licence: MIT License
Expand system variables Unix style

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to expandvars

Cross Env
🔀 Cross platform setting of environment scripts
Stars: ✭ 5,623 (+32976.47%)
Mutual labels:  unix, variables
OpenBSDFirewall
Simple OpenBSD Home Firewall Config for ALIX Board
Stars: ✭ 41 (+141.18%)
Mutual labels:  unix
kernel
Main kernel tree
Stars: ✭ 28 (+64.71%)
Mutual labels:  unix
dotfiles
Dotfiles repo
Stars: ✭ 12 (-29.41%)
Mutual labels:  unix
aero
Aero is a new modern, experimental, unix-like operating system following the monolithic kernel design. Supporting modern PC features such as long mode, 5-level paging, and SMP (multicore), to name a few.
Stars: ✭ 407 (+2294.12%)
Mutual labels:  unix
Narthex
Modular personalized dictionary generator.
Stars: ✭ 156 (+817.65%)
Mutual labels:  unix
lldbg
A lightweight native GUI for LLDB.
Stars: ✭ 83 (+388.24%)
Mutual labels:  unix
richvariables
DEPRECATED Allows you to easily use Craft Globals as variables in Rich Text fields
Stars: ✭ 44 (+158.82%)
Mutual labels:  variables
pv
Unix Pipe Viewer (pv) utility in Node.js
Stars: ✭ 20 (+17.65%)
Mutual labels:  unix
JamesM
me going through JamesM's kernel development tutorials
Stars: ✭ 35 (+105.88%)
Mutual labels:  unix
sixarm unix shell scripts
SixArm.com » Unix » shell scripts for command line programs in sh, bash, etc.
Stars: ✭ 49 (+188.24%)
Mutual labels:  unix
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (+329.41%)
Mutual labels:  variables
osIROSE-new
Open Source ROSE online server
Stars: ✭ 49 (+188.24%)
Mutual labels:  unix
environment-variables
🌳 Provides an abstraction of environment variables.
Stars: ✭ 33 (+94.12%)
Mutual labels:  variables
jobflow
runs stuff in parallel (like GNU parallel, but much faster and memory-efficient)
Stars: ✭ 67 (+294.12%)
Mutual labels:  unix
quickhist
quickly plot a histogram on the CLI
Stars: ✭ 45 (+164.71%)
Mutual labels:  unix
react-collapsed
A React custom-hook for creating flexible and accessible expand/collapse components.
Stars: ✭ 392 (+2205.88%)
Mutual labels:  expand
rlimit
Resource limits
Stars: ✭ 13 (-23.53%)
Mutual labels:  unix
mrouted
The original DVMRP (dynamic multicast routing) implementation for UNIX
Stars: ✭ 58 (+241.18%)
Mutual labels:  unix
bash-streams-handbook
💻 Learn Bash streams, pipelines and redirection, from beginner to advanced.
Stars: ✭ 153 (+800%)
Mutual labels:  unix

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.

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:

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.

Alternatives

  • environs - simplified environment variable parsing.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].