Skip to content

Commit b8fa111

Browse files
authored
Optional cli (theskumar#99)
* Add cli as extra option in pip * move get_cli_string out of cli.py * Make test happy :)
1 parent 7694d8c commit b8fa111

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,16 @@ You can use dotenv with iPython. You can either let the dotenv search for .env w
171171
%dotenv -v
172172

173173

174-
175174
Command-line interface
176175
=================
177176

177+
For commandline support, use the `cli` option during installation:
178+
179+
::
180+
181+
pip install -U "python-dotenv[cli]"
182+
183+
178184
A cli interface ``dotenv`` is also included, which helps you manipulate
179185
the ``.env`` file without manually opening it. The same cli installed on
180186
remote machine combined with fabric (discussed later) will enable you to

dotenv/__init__.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
from .cli import get_cli_string
21
from .main import load_dotenv, get_key, set_key, unset_key, find_dotenv, dotenv_values
32

43

4+
def load_ipython_extension(ipython):
5+
from .ipython import load_ipython_extension
6+
load_ipython_extension(ipython)
7+
8+
9+
def get_cli_string(path=None, action=None, key=None, value=None, quote=None):
10+
"""Returns a string suitable for running as a shell script.
11+
12+
Useful for converting a arguments passed to a fabric task
13+
to be passed to a `local` or `run` command.
14+
"""
15+
command = ['dotenv']
16+
if quote:
17+
command.append('-q %s' % quote)
18+
if path:
19+
command.append('-f %s' % path)
20+
if action:
21+
command.append(action)
22+
if key:
23+
command.append(key)
24+
if value:
25+
if ' ' in value:
26+
command.append('"%s"' % value)
27+
else:
28+
command.append(value)
29+
30+
return ' '.join(command).strip()
31+
32+
533
__all__ = ['get_cli_string',
634
'load_dotenv',
735
'dotenv_values',
@@ -10,8 +38,3 @@
1038
'unset_key',
1139
'find_dotenv',
1240
'load_ipython_extension']
13-
14-
15-
def load_ipython_extension(ipython):
16-
from .ipython import load_ipython_extension
17-
load_ipython_extension(ipython)

dotenv/cli.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import os
2+
import sys
23

3-
import click
4+
try:
5+
import click
6+
except ImportError:
7+
sys.stderr.write('It seems python-dotenv is not installed with cli option. \n'
8+
'Run pip install "python-dotenv[cli]" to fix this.')
9+
sys.exit(1)
410

511
from .main import dotenv_values, get_key, set_key, unset_key
612

@@ -72,29 +78,5 @@ def unset(ctx, key):
7278
exit(1)
7379

7480

75-
def get_cli_string(path=None, action=None, key=None, value=None, quote=None):
76-
"""Returns a string suitable for running as a shell script.
77-
78-
Useful for converting a arguments passed to a fabric task
79-
to be passed to a `local` or `run` command.
80-
"""
81-
command = ['dotenv']
82-
if quote:
83-
command.append('-q %s' % quote)
84-
if path:
85-
command.append('-f %s' % path)
86-
if action:
87-
command.append(action)
88-
if key:
89-
command.append(key)
90-
if value:
91-
if ' ' in value:
92-
command.append('"%s"' % value)
93-
else:
94-
command.append(value)
95-
96-
return ' '.join(command).strip()
97-
98-
9981
if __name__ == "__main__":
10082
cli()

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
keywords=['environment variables', 'deployments', 'settings', 'env', 'dotenv',
2020
'configurations', 'python'],
2121
packages=['dotenv'],
22-
install_requires=[
23-
'click>=5.0',
24-
],
22+
extras_require={
23+
'cli': ['click>=5.0', ],
24+
},
2525
entry_points='''
2626
[console_scripts]
27-
dotenv=dotenv:cli.cli
27+
dotenv=dotenv.cli:cli
2828
''',
2929
classifiers=[
3030
# As from https://pypi.python.org/pypi?%3Aaction=list_classifiers
@@ -49,6 +49,7 @@
4949
'Programming Language :: Python :: 3.3',
5050
'Programming Language :: Python :: 3.4',
5151
'Programming Language :: Python :: 3.5',
52+
'Programming Language :: Python :: 3.6',
5253
'Programming Language :: Python :: Implementation :: PyPy',
5354
'Intended Audience :: Developers',
5455
'Intended Audience :: System Administrators',

tests/test_cli.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from os.path import dirname, join
44

55
import dotenv
6+
from dotenv.cli import cli as dotenv_cli
67

78
import sh
89

@@ -23,13 +24,13 @@ def test_get_key():
2324

2425
def test_list(cli, dotenv_file):
2526
success, key_to_set, value_to_set = dotenv.set_key(dotenv_file, 'HELLO', 'WORLD')
26-
result = cli.invoke(dotenv.cli.cli, ['--file', dotenv_file, 'list'])
27+
result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'list'])
2728
assert result.exit_code == 0, result.output
28-
assert result.output == 'HELLO="WORLD"\n'
29+
assert result.output == 'HELLO=WORLD\n'
2930

3031

3132
def test_list_wo_file(cli):
32-
result = cli.invoke(dotenv.cli.cli, ['--file', 'doesnotexists', 'list'])
33+
result = cli.invoke(dotenv_cli, ['--file', 'doesnotexists', 'list'])
3334
assert result.exit_code == 2, result.output
3435
assert 'Invalid value for "-f"' in result.output
3536

@@ -117,7 +118,7 @@ def test_default_path(cli):
117118
sh.cd(here)
118119
sh.dotenv('set', 'HELLO', 'WORLD')
119120
output = sh.dotenv('get', 'HELLO')
120-
assert output == 'HELLO="WORLD"\n'
121+
assert output == 'HELLO=WORLD\n'
121122
sh.rm(dotenv_path)
122123

123124

0 commit comments

Comments
 (0)