expandvars


Nameexpandvars JSON
Version 0.12.0 PyPI version JSON
download
home_page
SummaryExpand system variables Unix style
upload_time2023-11-22 09:41:08
maintainer
docs_urlNone
author
requires_python>=3
licenseMIT License Copyright (c) 2019 Arijit Basu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords expand system variables
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # expandvars

Expand system variables Unix style

[![PyPI version](https://img.shields.io/pypi/v/expandvars.svg)](https://pypi.org/project/expandvars)
[![codecov](https://codecov.io/gh/sayanarijit/expandvars/branch/master/graph/badge.svg)](https://codecov.io/gh/sayanarijit/expandvars)

## Inspiration

This module is inspired by [GNU bash's variable expansion features](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html). It can be used as an alternative to Python's [os.path.expandvars](https://docs.python.org/3/library/os.path.html#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:

```toml
[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

```python
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](https://github.com/sayanarijit/expandvars/blob/master/tests) 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`.

```python
# 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.

```bash
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.

```python
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

```bash
# 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](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project).
- 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](https://tox.readthedocs.io), you may want to use it for testing in different python versions.

## Alternatives

- [environs](https://github.com/sloria/environs) - simplified environment variable parsing.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "expandvars",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "Arijit Basu <sayanarijit@gmail.com>",
    "keywords": "expand,system,variables",
    "author": "",
    "author_email": "Arijit Basu <sayanarijit@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/2b/a5/46d1f58edcae1d632fafdfee313e378240e002ae45d26502bac938bd8751/expandvars-0.12.0.tar.gz",
    "platform": null,
    "description": "# expandvars\n\nExpand system variables Unix style\n\n[![PyPI version](https://img.shields.io/pypi/v/expandvars.svg)](https://pypi.org/project/expandvars)\n[![codecov](https://codecov.io/gh/sayanarijit/expandvars/branch/master/graph/badge.svg)](https://codecov.io/gh/sayanarijit/expandvars)\n\n## Inspiration\n\nThis module is inspired by [GNU bash's variable expansion features](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html). It can be used as an alternative to Python's [os.path.expandvars](https://docs.python.org/3/library/os.path.html#os.path.expandvars) function.\n\nA 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.\nFor example:\n\n```toml\n[default]\nmy_secret_access_code = \"${ACCESS_CODE:-default_access_code}\"\nmy_important_variable = \"${IMPORTANT_VARIABLE:?}\"\nmy_updated_path = \"$PATH:$HOME/.bin\"\nmy_process_id = \"$$\"\nmy_nested_variable = \"${!NESTED}\"\n```\n\n> NOTE: Although this module copies most of the common behaviours of bash,\n> it doesn't follow bash strictly. For example, it doesn't work with arrays.\n\n## Installation\n\n### Pip\n\n```\npip install expandvars\n```\n\n### Conda\n\n```\nconda install -c conda-forge expandvars\n```\n\n## Usage\n\n```python\nfrom expandvars import expandvars\n\nprint(expandvars(\"$PATH:${HOME:?}/bin:${SOME_UNDEFINED_PATH:-/default/path}\"))\n# /bin:/sbin:/usr/bin:/usr/sbin:/home/you/bin:/default/path\n```\n\n## Examples\n\nFor now, [refer to the test cases](https://github.com/sayanarijit/expandvars/blob/master/tests) to see how it behaves.\n\n## TIPs\n\n### nounset=True\n\nIf you want to enable strict parsing by default, (similar to `set -u` / `set -o nounset` in bash), pass `nounset=True`.\n\n```python\n# All the variables must be defined.\nexpandvars(\"$VAR1:${VAR2}:$VAR3\", nounset=True)\n\n# Raises UnboundVariable error.\n```\n\n> NOTE: Another way is to use the `${VAR?}` or `${VAR:?}` syntax. See the examples in tests.\n\n### EXPANDVARS_RECOVER_NULL=\"foo\"\n\nIf you want to temporarily disable strict parsing both for `nounset=True` and the `${VAR:?}` syntax, set environment variable `EXPANDVARS_RECOVER_NULL=somevalue`.\nThis 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.\n\ne.g.\n\n```bash\nEXPANDVARS_RECOVER_NULL=foo myapp --config production.ini && echo \"All fine.\"\n```\n\n> WARNING: Try to avoid `export EXPANDVARS_RECOVER_NULL` because that will disable strict parsing permanently until you log out.\n\n### Customization\n\nYou can customize the variable symbol and data used for the expansion by using the more general `expand` function.\n\n```python\nfrom expandvars import expand\n\nprint(expand(\"%PATH:$HOME/bin:%{SOME_UNDEFINED_PATH:-/default/path}\", environ={\"PATH\": \"/example\"}, var_symbol=\"%\"))\n# /example:$HOME/bin:/default/path\n```\n\n## Contributing\n\nTo contribute, setup environment following way:\n\nThen\n\n```bash\n# Clone repo\ngit clone https://github.com/sayanarijit/expandvars && cd expandvars\n\n# Setup virtualenv\npython -m venv .venv\nsource ./.venv/bin/activate\n\n# Install as editable including test dependencies\npip install -e \".[tests]\"\n```\n\n- Follow [general git guidelines](https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project).\n- Keep it simple. Run `black .` to auto format the code.\n- Test your changes locally by running `pytest` (pass `--cov --cov-report html` for browsable coverage report).\n- If you are familiar with [tox](https://tox.readthedocs.io), you may want to use it for testing in different python versions.\n\n## Alternatives\n\n- [environs](https://github.com/sloria/environs) - simplified environment variable parsing.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2019 Arijit Basu  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Expand system variables Unix style",
    "version": "0.12.0",
    "project_urls": {
        "Homepage": "https://github.com/sayanarijit/expandvars"
    },
    "split_keywords": [
        "expand",
        "system",
        "variables"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dfb3072c28eace372ba7630ea187b7efd7f09cc8bcebf847a96b5e03e9cc0828",
                "md5": "29470eefb7ef23104a97f9803bce0e1b",
                "sha256": "7432c1c2ae50c671a8146583177d60020dd210ada7d940e52af91f1f84f753b2"
            },
            "downloads": -1,
            "filename": "expandvars-0.12.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "29470eefb7ef23104a97f9803bce0e1b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 7306,
            "upload_time": "2023-11-22T09:41:05",
            "upload_time_iso_8601": "2023-11-22T09:41:05.733176Z",
            "url": "https://files.pythonhosted.org/packages/df/b3/072c28eace372ba7630ea187b7efd7f09cc8bcebf847a96b5e03e9cc0828/expandvars-0.12.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2ba546d1f58edcae1d632fafdfee313e378240e002ae45d26502bac938bd8751",
                "md5": "8091f578a547e2b4b9df59100a179266",
                "sha256": "7d1adfa55728cf4b5d812ece3d087703faea953e0c0a1a78415de9df5024d844"
            },
            "downloads": -1,
            "filename": "expandvars-0.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8091f578a547e2b4b9df59100a179266",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 10909,
            "upload_time": "2023-11-22T09:41:08",
            "upload_time_iso_8601": "2023-11-22T09:41:08.174249Z",
            "url": "https://files.pythonhosted.org/packages/2b/a5/46d1f58edcae1d632fafdfee313e378240e002ae45d26502bac938bd8751/expandvars-0.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-22 09:41:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sayanarijit",
    "github_project": "expandvars",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "expandvars"
}
        
Elapsed time: 0.14822s