From 40a76e99fc83a62b1ecdb1972d40303e6ccc571c Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Mon, 8 Oct 2018 15:40:11 +0900 Subject: [PATCH] Add argument to choose .env file encoding Added argument for .env file encoding. Default encoding is UTF-8. --- dotenv/main.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/dotenv/main.py b/dotenv/main.py index e6040e49..a7aabd12 100644 --- a/dotenv/main.py +++ b/dotenv/main.py @@ -47,10 +47,11 @@ def parse_line(line): class DotEnv(): - def __init__(self, dotenv_path, verbose=False): + def __init__(self, dotenv_path, verbose=False, encoding='utf-8'): self.dotenv_path = dotenv_path self._dict = None self.verbose = verbose + self.encoding = encoding def _get_stream(self): self._is_file = False @@ -59,7 +60,7 @@ def _get_stream(self): if os.path.isfile(self.dotenv_path): self._is_file = True - return io.open(self.dotenv_path) + return io.open(self.dotenv_path, encoding=self.encoding) if self.verbose: warnings.warn("File doesn't exist {}".format(self.dotenv_path)) @@ -252,14 +253,21 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False): return '' -def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False): +def load_dotenv(dotenv_path=None, + stream=None, + verbose=False, + override=False, + encoding='utf-8'): f = dotenv_path or stream or find_dotenv() - return DotEnv(f, verbose=verbose).set_as_environment_variables(override=override) + return DotEnv(f, verbose=verbose, encoding=encoding).set_as_environment_variables(override=override) -def dotenv_values(dotenv_path=None, stream=None, verbose=False): +def dotenv_values(dotenv_path=None, + stream=None, + verbose=False, + encoding='utf-8'): f = dotenv_path or stream or find_dotenv() - return DotEnv(f, verbose=verbose).dict() + return DotEnv(f, verbose=verbose, encoding=encoding).dict() def run_command(command, env):