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

Allow passing of encoding to load_dotenv and dotenv_values (Pr/144) #161

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
`load_dotenv` do not override existing System environment variables. To
override, pass `override=True` to `load_dotenv()`.

`load_dotenv` also accepts `encoding` parameter to open the `.env` file. The default encoding is platform dependent (whatever `locale.getpreferredencoding()` returns), but any encoding supported by Python can be used. See the [codecs](https://docs.python.org/3/library/codecs.html#standard-encodings) module for the list of supported encodings.

You can use `find_dotenv()` method that will try to find a `.env` file
by (a) guessing where to start using `__file__` or the working directory
-- allowing this to work in non-file contexts such as IPython notebooks
Expand Down Expand Up @@ -298,6 +300,7 @@ Unreleased
-----

- Add type hints and expose them to users ([@qnighy])([#172])
- `load_dotenv` and `dotenv_values` now accepts `encoding` paramater, defaults to `None` ([@theskumar])([@earlbread]) (#161)

0.10.1
-----
Expand Down Expand Up @@ -409,6 +412,7 @@ Unreleased
[@hugochinchilla](https://github.com/hugochinchilla)).
- Improved test coverage.

[#161]: https://github.com/theskumar/python-dotenv/issues/161
[#78]: https://github.com/theskumar/python-dotenv/issues/78
[#148]: https://github.com/theskumar/python-dotenv/issues/148
[#158]: https://github.com/theskumar/python-dotenv/issues/158
Expand All @@ -423,3 +427,4 @@ Unreleased
[@cjauvin]: https://github.com/cjauvin
[@bbc2]: https://github.com/bbc2
[@qnighy]: https://github.com/qnighy
[@earlbread]: https://github.com/earlbread
19 changes: 10 additions & 9 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,20 @@ def parse_stream(stream):

class DotEnv():

def __init__(self, dotenv_path, verbose=False):
# type: (Union[Text, _PathLike, _StringIO], bool) -> None
def __init__(self, dotenv_path, verbose=False, encoding=None):
# type: (Union[Text, _PathLike, _StringIO], bool, Union[None, Text]) -> None
self.dotenv_path = dotenv_path # type: Union[Text,_PathLike, _StringIO]
self._dict = None # type: Optional[Dict[Text, Text]]
self.verbose = verbose # type: bool
self.encoding = encoding # type: Union[None, Text]

@contextmanager
def _get_stream(self):
# type: () -> Iterator[IO[Text]]
if isinstance(self.dotenv_path, StringIO):
yield self.dotenv_path
elif os.path.isfile(self.dotenv_path):
with io.open(self.dotenv_path) as stream:
with io.open(self.dotenv_path, encoding=self.encoding) as stream:
yield stream
else:
if self.verbose:
Expand Down Expand Up @@ -349,16 +350,16 @@ 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):
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool) -> bool
def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False, **kwargs):
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, Union[None, Text]) -> bool
f = dotenv_path or stream or find_dotenv()
return DotEnv(f, verbose=verbose).set_as_environment_variables(override=override)
return DotEnv(f, verbose=verbose, **kwargs).set_as_environment_variables(override=override)


def dotenv_values(dotenv_path=None, stream=None, verbose=False):
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool) -> Dict[Text, Text]
def dotenv_values(dotenv_path=None, stream=None, verbose=False, **kwargs):
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, Union[None, Text]) -> Dict[Text, Text]
f = dotenv_path or stream or find_dotenv()
return DotEnv(f, verbose=verbose).dict()
return DotEnv(f, verbose=verbose, **kwargs).dict()


def run_command(command, env):
Expand Down