This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from dephell/dotenv
Dotenv
- Loading branch information
Showing
10 changed files
with
202 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import shlex | ||
from codecs import decode | ||
from pathlib import Path | ||
from string import Template | ||
from typing import Dict | ||
|
||
|
||
def read_dotenv(path: Path, env_vars: Dict[str, str] = None) -> Dict[str, str]: | ||
if env_vars is None: | ||
env_vars = dict() | ||
else: | ||
env_vars = env_vars.copy() | ||
|
||
if path.is_dir(): | ||
path = path / '.env' | ||
if not path.exists(): | ||
return env_vars | ||
|
||
with path.open('r', encoding='utf-8') as stream: | ||
for line in stream: | ||
line = line.strip() | ||
if not line or line[0] == '#': | ||
continue | ||
key, value = line.split('=', 1) | ||
|
||
# clean key | ||
key = key.strip() | ||
if key.startswith('export '): | ||
key = key.replace('export ', '', 1) | ||
key = key.strip() | ||
if key[0] == '$': | ||
key = key[1:] | ||
|
||
# clean and substitute value | ||
value = ' '.join(shlex.split(value, comments=True)) | ||
value = decode(value, 'unicode-escape') | ||
if '$' in value: | ||
value = value.replace(r'\$', '$$') # escaping | ||
value = Template(value).safe_substitute(env_vars) | ||
|
||
env_vars[key] = value | ||
|
||
return env_vars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
|
||
from dephell.actions import read_dotenv | ||
|
||
|
||
# https://github.com/jpadilla/django-dotenv/blob/master/tests.py | ||
# https://github.com/motdotla/dotenv/blob/master/tests/.env | ||
@pytest.mark.parametrize('lines, expected', [ | ||
(['a=b # lol'], {'a': 'b'}), | ||
# strip spaces | ||
(['FOO=bar'], {'FOO': 'bar'}), | ||
(['FOO =bar'], {'FOO': 'bar'}), | ||
(['FOO= bar'], {'FOO': 'bar'}), | ||
(['FOO=bar '], {'FOO': 'bar'}), | ||
([' FOO=bar'], {'FOO': 'bar'}), | ||
(['take = me out '], {'take': 'me out'}), | ||
# quotes | ||
(['FOO="bar"'], {'FOO': 'bar'}), | ||
(["FOO='bar'"], {'FOO': 'bar'}), | ||
# key formats | ||
(['FOO.BAR=foobar'], {'FOO.BAR': 'foobar'}), | ||
# empty | ||
(['FOO='], {'FOO': ''}), | ||
(['FOO= '], {'FOO': ''}), | ||
# substitution | ||
(['FOO=test', 'BAR=$FOO'], {'FOO': 'test', 'BAR': 'test'}), | ||
(['FOO=test', 'BAR=${FOO}bar'], {'FOO': 'test', 'BAR': 'testbar'}), | ||
# escaping | ||
([r'FOO="escaped\"bar"'], {'FOO': 'escaped"bar'}), | ||
(['FOO=test', r'BAR="foo\$FOO"'], {'FOO': 'test', 'BAR': 'foo$FOO'}), | ||
(['FOO=test', r'BAR="foo\${FOO}"'], {'FOO': 'test', 'BAR': 'foo${FOO}'}), | ||
# escape sequences | ||
([r'take="me\nout"'], {'take': 'me\nout'}), | ||
([r'take="me\out"'], {'take': r'me\out'}), | ||
# comments | ||
(['take=me # out'], {'take': 'me'}), | ||
(['take="me # to" # church'], {'take': 'me # to'}), | ||
(['take="me" # to # church'], {'take': 'me'}), | ||
(['take="me # out"'], {'take': 'me # out'}), | ||
(['# take', 'me=out'], {'me': 'out'}), | ||
]) | ||
def test_read_dotenv(temp_path, lines, expected): | ||
(temp_path / '.env').write_text('\n'.join(lines)) | ||
assert read_dotenv(temp_path) == expected |