Skip to content

Commit 5094182

Browse files
committed
Refactor to convert dotenv as a package instead of pymodule
1 parent 8b64d5d commit 5094182

File tree

6 files changed

+117
-109
lines changed

6 files changed

+117
-109
lines changed

dotenv/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .cli import get_cli_string
2+
from .main import load_dotenv, get_key, set_key, unset_key
3+
4+
__all__ = ['get_cli_string', 'load_dotenv', 'get_key', 'set_key', 'unset_key']

dotenv/cli.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
3+
import click
4+
5+
from .main import get_key, parse_dotenv, set_key, unset_key
6+
7+
8+
@click.group()
9+
@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'),
10+
type=click.Path(exists=True),
11+
help="Location of the .env file, defaults to .env file in current working directory.")
12+
@click.pass_context
13+
def cli(ctx, file):
14+
'''This script is used to set, get or unset values from a .env file.'''
15+
ctx.obj = {}
16+
ctx.obj['FILE'] = file
17+
18+
# Need to investigate if this can actually work or if the scope of the new environ variables
19+
# Expires when python exits
20+
21+
# elif action == "load":
22+
# success = load_dotenv(file)
23+
# if success != None:
24+
# click.echo("loaded %s into environment" % file)
25+
# else:
26+
# exit(1)
27+
28+
29+
@cli.command()
30+
@click.pass_context
31+
def list(ctx):
32+
'''Display all the stored key/value.'''
33+
file = ctx.obj['FILE']
34+
dotenv_as_dict = parse_dotenv(file)
35+
for k, v in dotenv_as_dict:
36+
click.echo('%s="%s"' % (k, v))
37+
38+
39+
@cli.command()
40+
@click.pass_context
41+
@click.argument('key', required=True)
42+
@click.argument('value', required=True)
43+
def set(ctx, key, value):
44+
'''Store the given key/value.'''
45+
file = ctx.obj['FILE']
46+
success, key, value = set_key(file, key, value)
47+
if success:
48+
click.echo('%s="%s"' % (key, value))
49+
else:
50+
exit(1)
51+
52+
53+
@cli.command()
54+
@click.pass_context
55+
@click.argument('key', required=True)
56+
def get(ctx, key):
57+
'''Retrive the value for the given key.'''
58+
file = ctx.obj['FILE']
59+
stored_value = get_key(file, key)
60+
if stored_value:
61+
click.echo('%s="%s"' % (key, stored_value))
62+
else:
63+
exit(1)
64+
65+
66+
@cli.command()
67+
@click.pass_context
68+
@click.argument('key', required=True)
69+
def unset(ctx, key):
70+
'''Removes the given key.'''
71+
file = ctx.obj['FILE']
72+
success, key = unset_key(file, key)
73+
if success:
74+
click.echo("Successfully removed %s" % key)
75+
else:
76+
exit(1)
77+
78+
79+
def get_cli_string(path=None, action=None, key=None, value=None):
80+
"""Returns a string suitable for running as a shell script.
81+
82+
Useful for converting a arguments passed to a fabric task
83+
to be passed to a `local` or `run` command.
84+
"""
85+
command = ['dotenv']
86+
if path:
87+
command.append('-f %s' % path)
88+
if action:
89+
command.append(action)
90+
if key:
91+
command.append(key)
92+
if value:
93+
if ' ' in value:
94+
command.append('"%s"' % value)
95+
else:
96+
command.append(value)
97+
98+
return ' '.join(command).strip()
99+
100+
if __name__ == "__main__":
101+
cli()

dotenv/compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
try:
2+
from collections import OrderedDict # noqa
3+
except ImportError:
4+
from ordereddict import OrderedDict # noqa

dotenv.py renamed to dotenv/main.py

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
import os
44
import warnings
5-
try:
6-
from collections import OrderedDict
7-
except ImportError:
8-
from ordereddict import OrderedDict
95

10-
import click
6+
from .compat import OrderedDict
117

128

139
def load_dotenv(dotenv_path):
@@ -95,99 +91,3 @@ def flatten_and_write(dotenv_path, dotenv_as_dict):
9591
for k, v in dotenv_as_dict.items():
9692
f.write('%s="%s"\n' % (k, v))
9793
return True
98-
99-
100-
@click.group()
101-
@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'),
102-
type=click.Path(exists=True),
103-
help="Location of the .env file, defaults to .env file in current working directory.")
104-
@click.pass_context
105-
def cli(ctx, file):
106-
'''This script is used to set, get or unset values from a .env file.'''
107-
ctx.obj = {}
108-
ctx.obj['FILE'] = file
109-
110-
# Need to investigate if this can actually work or if the scope of the new environ variables
111-
# Expires when python exits
112-
113-
# elif action == "load":
114-
# success = load_dotenv(file)
115-
# if success != None:
116-
# click.echo("loaded %s into environment" % file)
117-
# else:
118-
# exit(1)
119-
120-
121-
@cli.command()
122-
@click.pass_context
123-
def list(ctx):
124-
'''Display all the stored key/value.'''
125-
file = ctx.obj['FILE']
126-
dotenv_as_dict = parse_dotenv(file)
127-
for k, v in dotenv_as_dict:
128-
click.echo('%s="%s"' % (k, v))
129-
130-
131-
@cli.command()
132-
@click.pass_context
133-
@click.argument('key', required=True)
134-
@click.argument('value', required=True)
135-
def set(ctx, key, value):
136-
'''Store the given key/value.'''
137-
file = ctx.obj['FILE']
138-
success, key, value = set_key(file, key, value)
139-
if success:
140-
click.echo('%s="%s"' % (key, value))
141-
else:
142-
exit(1)
143-
144-
145-
@cli.command()
146-
@click.pass_context
147-
@click.argument('key', required=True)
148-
def get(ctx, key):
149-
'''Retrive the value for the given key.'''
150-
file = ctx.obj['FILE']
151-
stored_value = get_key(file, key)
152-
if stored_value:
153-
click.echo('%s="%s"' % (key, stored_value))
154-
else:
155-
exit(1)
156-
157-
158-
@cli.command()
159-
@click.pass_context
160-
@click.argument('key', required=True)
161-
def unset(ctx, key):
162-
'''Removes the given key.'''
163-
file = ctx.obj['FILE']
164-
success, key = unset_key(file, key)
165-
if success:
166-
click.echo("Successfully removed %s" % key)
167-
else:
168-
exit(1)
169-
170-
171-
def get_cli_string(path=None, action=None, key=None, value=None):
172-
"""Returns a string suitable for running as a shell script.
173-
174-
Useful for converting a arguments passed to a fabric task
175-
to be passed to a `local` or `run` command.
176-
"""
177-
command = ['dotenv']
178-
if path:
179-
command.append('-f %s' % path)
180-
if action:
181-
command.append(action)
182-
if key:
183-
command.append(key)
184-
if value:
185-
if ' ' in value:
186-
command.append('"%s"' % value)
187-
else:
188-
command.append(value)
189-
190-
return ' '.join(command).strip()
191-
192-
if __name__ == "__main__":
193-
cli()

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import unicode_literals
3-
42
from setuptools import setup
53

64
with open('README.rst') as readme_file:
@@ -16,14 +14,14 @@
1614
url="http://github.com/theskumar/python-dotenv",
1715
keywords=['environment variables', 'deployments', 'settings', 'env', 'dotenv',
1816
'configurations', 'python'],
19-
py_modules=['dotenv'],
17+
packages=['dotenv'],
2018
install_requires=[
2119
'click>=5.0',
2220
'ordereddict'
2321
],
2422
entry_points='''
2523
[console_scripts]
26-
dotenv=dotenv:cli
24+
dotenv=dotenv:cli.cli
2725
''',
2826
classifiers=[
2927
# As from https://pypi.python.org/pypi?%3Aaction=list_classifiers

tests/test_cli.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
from os.path import dirname, join
55

6-
import sh
76
import dotenv
87

8+
import sh
9+
910
here = dirname(__file__)
1011
dotenv_path = join(here, '.env')
1112

@@ -42,15 +43,15 @@ def test_console_script(cli):
4243
sh.rm(dotenv_path)
4344

4445
# should fail for not existing file
45-
result = cli.invoke(dotenv.set, ['my_key', 'my_value'])
46+
result = cli.invoke(dotenv.cli.set, ['my_key', 'my_value'])
4647
assert result.exit_code != 0
4748

4849
# should fail for not existing file
49-
result = cli.invoke(dotenv.get, ['my_key'])
50+
result = cli.invoke(dotenv.cli.get, ['my_key'])
5051
assert result.exit_code != 0
5152

5253
# should fail for not existing file
53-
result = cli.invoke(dotenv.list, [])
54+
result = cli.invoke(dotenv.cli.list, [])
5455
assert result.exit_code != 0
5556

5657

0 commit comments

Comments
 (0)