Skip to content

Commit

Permalink
Add argument to choose .env file encoding
Browse files Browse the repository at this point in the history
Added argument for .env file encoding. Default encoding is UTF-8.
  • Loading branch information
earlbread committed Oct 8, 2018
1 parent 40ed1b1 commit 40a76e9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 40a76e9

Please sign in to comment.