-
Notifications
You must be signed in to change notification settings - Fork 28
/
__init__.py
31 lines (23 loc) · 947 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
__version__ = '0.0.2'
# So we can use environment variables to configure things.
def load_into_environ(fname):
""" load the key value pairs in fname into the os.environ.
Each line should have a key=value pair.
There can be comments with #.
:param fname: name of the '.env' file.
"""
import os
def as_key_value(lines):
""" Return a dict of the key value pairs.
"""
no_comments = [l.split('#')[0].rstrip() for l in lines if not l.startswith('#')]
no_empty = [l for l in no_comments if l]
kvs = [l.split('=') for l in no_empty]
no_extra_spaces = [(k.rstrip().lstrip(), v.rstrip().lstrip()) for k, v in kvs]
return no_extra_spaces
if os.path.exists(fname):
with open(fname) as lines:
for key, value in as_key_value(lines):
if key not in os.environ:
os.environ[key] = value
load_into_environ('.env')