Skip to content

Commit d4f3243

Browse files
author
Nimrod Milo
committed
Added IPython magic option to override
1 parent ee6bde5 commit d4f3243

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

‎dotenv/ipython.py‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
from __future__ import print_function
22
from .main import load_dotenv, find_dotenv
33

4+
from IPython.core.magic import Magics, magics_class, line_magic
5+
from IPython.core.magic_arguments import (argument, magic_arguments,
6+
parse_argstring)
47

5-
def _magic(dotenv_path):
6-
"""
7-
dotenv [dotenv_path]
88

9-
Search in increasingly higher folders for the `dotenv_path`
10-
"""
11-
# Locate the .env file
12-
dotenv_path = dotenv_path or '.env'
13-
try:
14-
dotenv_path = find_dotenv(dotenv_path, True, True)
15-
except IOError:
16-
print("cannot find .env file")
17-
return
9+
@magics_class
10+
class IPythonDotEnv(Magics):
1811

19-
# Load the .env file
20-
load_dotenv(dotenv_path)
12+
@magic_arguments()
13+
@argument(
14+
'-o', '--override', action='store_true',
15+
help="Indicate to override existing variables"
16+
)
17+
@argument('dotenv_path', nargs='?', type=str, default='.env',
18+
help='Search in increasingly higher folders for the `dotenv_path`')
19+
@line_magic
20+
def dotenv(self, line):
21+
args = parse_argstring(self.dotenv, line)
22+
# Locate the .env file
23+
dotenv_path = args.dotenv_path
24+
try:
25+
dotenv_path = find_dotenv(dotenv_path, True, True)
26+
except IOError:
27+
print("cannot find .env file")
28+
return
29+
30+
# Load the .env file
31+
load_dotenv(dotenv_path, override=args.override)
2132

2233

2334
def load_ipython_extension(ipython):
2435
"""Register the %dotenv magic."""
25-
ipython.register_magic_function(_magic, magic_name='dotenv')
36+
ipython.register_magics(IPythonDotEnv)

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ wheel
66
pytest-cov
77
pytest-flake8
88
click
9+
ipython

0 commit comments

Comments
 (0)