Skip to content

Commit

Permalink
feat: migrate to pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
onjin committed Aug 4, 2024
1 parent b43a6eb commit c4d212c
Show file tree
Hide file tree
Showing 12 changed files with 594 additions and 480 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: test

on:
push:
branches:
- master
pull_request:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true

env:
STABLE_PYTHON_VERSION: '3.11'
PYTHONUNBUFFERED: "1"
FORCE_COLOR: "1"

jobs:
run:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Ensure latest pip
run: python -m pip install --upgrade pip

- name: Ensure latest hatch
run: python -m pip install --upgrade hatch

- name: Install ourself
run: |
pip install -e .
- if: ${{ !startsWith(matrix.python-version, 'pypy') }}
name: Lint
run: hatch run lint:style

- name: Run tests
run: hatch run all:test
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
language: python

python:
- "3.12"
- "3.11"
- "3.10"
- "3.9"
- "3.8"
- "3.7"
- "3.6"
- "2.7"
- "pypy"

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
Expand Down
13 changes: 5 additions & 8 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
Copyright (c) 2015, Marek Wywiał
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
MIT License

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Copyright (c) 2015, Marek Wywiał

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
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:

* Neither the name of runenv nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.
166 changes: 166 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# runenv

<div align="center">

| | |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| CI/CD | [![CI - Test](https://github.com/onjin/runenv/actions/workflows/test.yml/badge.svg)](https://github.com/onjin/runenv/actions/workflows/test.yml) |
| Package | [![PyPI - Version](https://img.shields.io/pypi/v/runenv.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/runenv/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/runenv.svg?color=blue&label=Downloads&logo=pypi&logoColor=gold)](https://pypi.org/project/runenv/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/runenv.svg?logo=python&label=Python&logoColor=gold)](https://pypi.org/project/runenv/) |
| Meta | [![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![code style - Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![types - Mypy](https://img.shields.io/badge/types-Mypy-blue.svg)](https://github.com/python/mypy) [![License - MIT](https://img.shields.io/badge/license-MIT-9400d3.svg)](https://spdx.org/licenses/) |

</div>

---

Wrapper to run programs with modified environment variables loaded from
given file. You can use *runenv* to manage your app settings using
[12-factor](http://12factor.net/) principles.

You can use same environment file with **runenv** and with **docker**
using [env-file](https://docs.docker.com/reference/commandline/cli/)
parameter

- Free software: BSD license
- Documentation: <https://runenv.readthedocs.org>.

# Features

CLI:

- command-line tool to load environment variables from given file

Python API:

- load variables from a file (.env or passed filename)
- load only variables with given prefix
- prefix can be stripped during load
- detect whether environment was loaded by runenv CLI
- force load even if runenv CLI was used
- search_parent option which allows to look for env_file in parent
dirs

# Installation

In order to install use pip

``` console
$ pip install -U runenv
```

# Usage

Run from shell

``` console
$ runenv env.development ./manage.py runserver
```

example env.development file

``` python
BASE_URL=http://127.0.0.1:8000
DATABASE_URI=postgres://postgres:password@localhost/dbname
SECRET_KEY=y7W8pbRcuPuAmgTHsJtEpKocb7XPcV0u

# email settings
EMAIL_HOST=smtp.mandrillapp.com
EMAIL_PORT=587
EMAIL_HOST_USER=someuser
EMAIL_HOST_PASSWORD=hardpassword
EMAIL_FROM=dev@local.host
EMAIL_USE_TLS=1
```

# Python API

**load_env(env_file=\'.env\', prefix=None, strip_prefix=True,
force=False, search_parent=0)**

Loads environment from given `env_file`(default.env\`).

Options:

--------------------------------------------------------------------
option default description
----------- --------------- ----------------------------------------
env_file .env relative or absolute path to file with
environment variables

prefix None prefix to match variables e.g. APP\_

str True should the prefix be stripped during loa
ip_prefix

force False load env_file, even though runenv CLI
command was used

sear 0 To what level traverse parents in search
ch_parent of file
--------------------------------------------------------------------

If `prefix` option is provided only variables starting with it will be
loaded to environment, with their keys stripped of that prefix. To
preserve prefix, you can set `strip_prefix` to `False`.

Example

``` console
$ echo 'APP_SECRET_KEY=bzemAG0xfdMgFrHBT3tJBbiYIoY6EeAj' > .env
```

``` python
$ python
>>> import os
>>> from runenv import load_env
>>> load_env(prefix='APP_')
>>> 'APP_SECRET_KEY' in os.environ
False
>>> 'SECRET_KEY' in os.environ
True
>>> load_env(prefix='APP_', strip_prefix=False)
>>> 'APP_SECRET_KEY' in os.environ
True
```

**Notice**: Environment will not be loaded if command was fired by
runenv wrapper, unless you set the **force** parameter to **True**

`load_env` does not load variables when wrapper `runenv` is used. Also
`_RUNENV_WRAPPED` is set to `1`

Example

``` console
$ echo 'APP_SECRET_KEY=bzemAG0xfdMgFrHBT3tJBbiYIoY6EeAj' > .env
```

``` python
$ python
>>> import os
>>> from runenv import load_env
>>> os.environ['_RUNENV_WRAPPED'] = '1'
>>> load_env()
>>> 'APP_SECRET_KEY' in os.environ
False
>>> load_env(force=True)
>>> 'APP_SECRET_KEY' in os.environ
True
```

## Django/Flask integration

To use `load_env` with [Django](http://djangoproject.com/) or
[Flask](http://flask.pocoo.org/), put the followin in `manage.py` and
`wsgi.py`

``` python
from runenv import load_env
load_env()
```

## Similar projects

- <https://github.com/jezdez/envdir> - runs another program with a
modified environment according to files in a specified directory
- <https://github.com/theskumar/python-dotenv> - Reads the key,value
pair from .env and adds them to environment variable
Loading

0 comments on commit c4d212c

Please sign in to comment.