From 0681a878a2530908ef1ef64dff17ed19cfe20465 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 22 May 2019 19:12:16 +0530 Subject: [PATCH] Refractor: move 'to_env' to compat.py --- src/dotenv/compat.py | 21 ++++++++++++++++----- src/dotenv/main.py | 13 +------------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/dotenv/compat.py b/src/dotenv/compat.py index 1a145345..394d3a3f 100644 --- a/src/dotenv/compat.py +++ b/src/dotenv/compat.py @@ -1,12 +1,23 @@ import sys -from typing import Text +from typing import Text # noqa -if sys.version_info >= (3, 0): - from io import StringIO # noqa -else: +PY2 = sys.version_info[0] == 2 # type: bool + +if PY2: from StringIO import StringIO # noqa +else: + from io import StringIO # noqa -PY2 = sys.version_info[0] == 2 # type: bool + +def to_env(text): + # type: (Text) -> str + """ + Encode a string the same way whether it comes from the environment or a `.env` file. + """ + if PY2: + return text.encode(sys.getfilesystemencoding() or "utf-8") + else: + return text def to_text(string): diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 5b619b11..1b44a0c2 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -14,7 +14,7 @@ from collections import OrderedDict from contextlib import contextmanager -from .compat import StringIO, PY2 +from .compat import StringIO, PY2, to_env from .parser import parse_stream if TYPE_CHECKING: # pragma: no cover @@ -31,17 +31,6 @@ __posix_variable = re.compile(r'\$\{[^\}]*\}') # type: Pattern[Text] -def to_env(text): - # type: (Text) -> str - """ - Encode a string the same way whether it comes from the environment or a `.env` file. - """ - if PY2: - return text.encode(sys.getfilesystemencoding() or "utf-8") - else: - return text - - class DotEnv(): def __init__(self, dotenv_path, verbose=False, encoding=None):