Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument to choose .env file encoding #144

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered just capturing the additional keyword arguments and passing them to the DotEnv constructor? That way you don't have to hard-code a default encoding in 3 different places.



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