Skip to content

Commit 5fc02b7

Browse files
committed
I made a thing.
0 parents  commit 5fc02b7

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.egg-info
2+
*.pyc
3+
build/
4+
dist/

LICENSE.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2013, Jacob Kaplan-Moss
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice,
9+
this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
* Neither the name of django-dotenv nor the names of its contributors
14+
may be used to endorse or promote products derived from this software
15+
without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# django-dotenv
2+
3+
[foreman](https://github.com/ddollar/foreman) reads from `.env`. `manage.py`
4+
doesn't. Let's fix that.
5+
6+
## Installation
7+
8+
```
9+
pip install django-dotenv
10+
```
11+
12+
## Usage
13+
14+
Pop open `manage.py`. Add:
15+
16+
```
17+
import dotenv
18+
dotenv.read_dotenv()
19+
```
20+
21+
You have to pass in `__file__` so that we know where to find the .env file.
22+
You can also pass an explicit path to the .env file, or to the directory
23+
where it lives. It's smart, it'll figure it out.
24+
25+
That's it. Now go 12 factor the crap out of something.

dotenv.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import sys
3+
4+
def read_dotenv(dotenv=None):
5+
"""
6+
Read a .env file into os.environ.
7+
8+
If not given a path to a dotenv path, does filthy magic stack backtracking
9+
to find manage.py and then find the dotenv.
10+
"""
11+
if dotenv is None:
12+
frame = sys._getframe()
13+
dotenv = os.path.join(os.path.dirname(frame.f_back.f_code.co_filename), '.env')
14+
if not os.path.exists(dotenv):
15+
raise EnvironmentError("%s doesn't exist. You may need to "
16+
"explicitly pass the path to .env to "
17+
"read_dotenv()." % dotenv)
18+
19+
for k, v in parse_dotenv(dotenv):
20+
os.environ.setdefault(k, v)
21+
22+
def parse_dotenv(dotenv):
23+
for line in open(dotenv):
24+
line = line.strip()
25+
if not line or line.startswith('#') or '=' not in line:
26+
continue
27+
k, v = line.split('=', 1)
28+
v = v.strip("'").strip('"')
29+
yield k, v

setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name = "django-dotenv",
5+
description = "foreman reads from .env. manage.py doesn't. Let's fix that.",
6+
version = "1.0",
7+
author = "Jacob Kaplan-Moss",
8+
author_email = "jacob@jacobian.org",
9+
url = "http://github.com/jacobian/django-dotenv",
10+
py_modules = ['dotenv'],
11+
classifiers = [
12+
'Development Status :: 4 - Beta',
13+
'Environment :: Web Environment',
14+
'Intended Audience :: Developers',
15+
'License :: OSI Approved :: BSD License',
16+
'Programming Language :: Python :: 2.7',
17+
]
18+
)

0 commit comments

Comments
 (0)