Skip to content

Commit 4c1d4a9

Browse files
author
Alexander Mancevice
committed
Allow multi-line ENV values
Use regex to parse ENV variable names from .env For each variable name, find the value of the variable by pulling the text *between* the current variable and the next variable in the file (ignoring quotes)
1 parent 40ed1b1 commit 4c1d4a9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

dotenv/main.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .compat import StringIO
1515

1616
__escape_decoder = codecs.getdecoder('unicode_escape')
17-
__posix_variable = re.compile('\$\{[^\}]*\}')
17+
__posix_variable = re.compile(r'\$\{[^\}]*\}')
1818

1919

2020
def decode_escaped(escaped):
@@ -78,12 +78,16 @@ def dict(self):
7878
def parse(self):
7979
f = self._get_stream()
8080

81-
for line in f:
82-
key, value = parse_line(line)
83-
if not key:
84-
continue
81+
# Find all variable names in .env
82+
fbody = f.read()
83+
varexp = r'^(?:export )?([a-zA-Z_][a-zA-Z0-9_]+) *= *[^=\n]'
84+
vars = re.findall(varexp, fbody, re.MULTILINE)
8585

86-
yield key, value
86+
# Extract values from text *between* variable names (ignoreing quotes)
87+
for key, nxt in zip(vars, vars[1:] + ['$']):
88+
valexp = r'{} *= *[\'"]?(.*?)[\'"]? *\n?{}'.format(key, nxt)
89+
val = re.search(valexp, fbody, re.MULTILINE | re.DOTALL).group(1)
90+
yield key, val
8791

8892
if self._is_file:
8993
f.close()

0 commit comments

Comments
 (0)