Skip to content

Commit 0681a87

Browse files
author
Saurabh Kumar
committed
Refractor: move 'to_env' to compat.py
1 parent 57f9639 commit 0681a87

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/dotenv/compat.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import sys
2-
from typing import Text
2+
from typing import Text # noqa
33

4-
if sys.version_info >= (3, 0):
5-
from io import StringIO # noqa
6-
else:
4+
PY2 = sys.version_info[0] == 2 # type: bool
5+
6+
if PY2:
77
from StringIO import StringIO # noqa
8+
else:
9+
from io import StringIO # noqa
810

9-
PY2 = sys.version_info[0] == 2 # type: bool
11+
12+
def to_env(text):
13+
# type: (Text) -> str
14+
"""
15+
Encode a string the same way whether it comes from the environment or a `.env` file.
16+
"""
17+
if PY2:
18+
return text.encode(sys.getfilesystemencoding() or "utf-8")
19+
else:
20+
return text
1021

1122

1223
def to_text(string):

src/dotenv/main.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from collections import OrderedDict
1515
from contextlib import contextmanager
1616

17-
from .compat import StringIO, PY2
17+
from .compat import StringIO, PY2, to_env
1818
from .parser import parse_stream
1919

2020
if TYPE_CHECKING: # pragma: no cover
@@ -31,17 +31,6 @@
3131
__posix_variable = re.compile(r'\$\{[^\}]*\}') # type: Pattern[Text]
3232

3333

34-
def to_env(text):
35-
# type: (Text) -> str
36-
"""
37-
Encode a string the same way whether it comes from the environment or a `.env` file.
38-
"""
39-
if PY2:
40-
return text.encode(sys.getfilesystemencoding() or "utf-8")
41-
else:
42-
return text
43-
44-
4534
class DotEnv():
4635

4736
def __init__(self, dotenv_path, verbose=False, encoding=None):

0 commit comments

Comments
 (0)