6
6
import tempfile
7
7
from collections import OrderedDict
8
8
from contextlib import contextmanager
9
- from typing import (IO , Dict , Iterable , Iterator , Mapping , Optional , Text ,
10
- Tuple , Union )
9
+ from typing import (IO , Dict , Iterable , Iterator , Mapping , Optional , Tuple ,
10
+ Union )
11
11
12
12
from .parser import Binding , parse_stream
13
13
from .variables import parse_variables
17
17
if sys .version_info >= (3 , 6 ):
18
18
_PathLike = os .PathLike
19
19
else :
20
- _PathLike = Text
20
+ _PathLike = str
21
21
22
22
23
23
def with_warn_for_invalid_lines (mappings : Iterator [Binding ]) -> Iterator [Binding ]:
@@ -33,21 +33,21 @@ def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding
33
33
class DotEnv ():
34
34
def __init__ (
35
35
self ,
36
- dotenv_path : Union [Text , _PathLike , io .StringIO ],
36
+ dotenv_path : Union [str , _PathLike , io .StringIO ],
37
37
verbose : bool = False ,
38
- encoding : Union [None , Text ] = None ,
38
+ encoding : Union [None , str ] = None ,
39
39
interpolate : bool = True ,
40
40
override : bool = True ,
41
41
) -> None :
42
- self .dotenv_path = dotenv_path # type: Union[Text ,_PathLike, io.StringIO]
43
- self ._dict = None # type: Optional[Dict[Text , Optional[Text ]]]
42
+ self .dotenv_path = dotenv_path # type: Union[str ,_PathLike, io.StringIO]
43
+ self ._dict = None # type: Optional[Dict[str , Optional[str ]]]
44
44
self .verbose = verbose # type: bool
45
- self .encoding = encoding # type: Union[None, Text ]
45
+ self .encoding = encoding # type: Union[None, str ]
46
46
self .interpolate = interpolate # type: bool
47
47
self .override = override # type: bool
48
48
49
49
@contextmanager
50
- def _get_stream (self ) -> Iterator [IO [Text ]]:
50
+ def _get_stream (self ) -> Iterator [IO [str ]]:
51
51
if isinstance (self .dotenv_path , io .StringIO ):
52
52
yield self .dotenv_path
53
53
elif os .path .isfile (self .dotenv_path ):
@@ -58,7 +58,7 @@ def _get_stream(self) -> Iterator[IO[Text]]:
58
58
logger .info ("Python-dotenv could not find configuration file %s." , self .dotenv_path or '.env' )
59
59
yield io .StringIO ('' )
60
60
61
- def dict (self ) -> Dict [Text , Optional [Text ]]:
61
+ def dict (self ) -> Dict [str , Optional [str ]]:
62
62
"""Return dotenv as dict"""
63
63
if self ._dict :
64
64
return self ._dict
@@ -72,7 +72,7 @@ def dict(self) -> Dict[Text, Optional[Text]]:
72
72
73
73
return self ._dict
74
74
75
- def parse (self ) -> Iterator [Tuple [Text , Optional [Text ]]]:
75
+ def parse (self ) -> Iterator [Tuple [str , Optional [str ]]]:
76
76
with self ._get_stream () as stream :
77
77
for mapping in with_warn_for_invalid_lines (parse_stream (stream )):
78
78
if mapping .key is not None :
@@ -90,7 +90,7 @@ def set_as_environment_variables(self) -> bool:
90
90
91
91
return True
92
92
93
- def get (self , key : Text ) -> Optional [Text ]:
93
+ def get (self , key : str ) -> Optional [str ]:
94
94
"""
95
95
"""
96
96
data = self .dict ()
@@ -104,7 +104,7 @@ def get(self, key: Text) -> Optional[Text]:
104
104
return None
105
105
106
106
107
- def get_key (dotenv_path : Union [Text , _PathLike ], key_to_get : Text ) -> Optional [Text ]:
107
+ def get_key (dotenv_path : Union [str , _PathLike ], key_to_get : str ) -> Optional [str ]:
108
108
"""
109
109
Gets the value of a given key from the given .env
110
110
@@ -114,7 +114,7 @@ def get_key(dotenv_path: Union[Text, _PathLike], key_to_get: Text) -> Optional[T
114
114
115
115
116
116
@contextmanager
117
- def rewrite (path : _PathLike ) -> Iterator [Tuple [IO [Text ], IO [Text ]]]:
117
+ def rewrite (path : _PathLike ) -> Iterator [Tuple [IO [str ], IO [str ]]]:
118
118
try :
119
119
if not os .path .isfile (path ):
120
120
with io .open (path , "w+" ) as source :
@@ -132,11 +132,11 @@ def rewrite(path: _PathLike) -> Iterator[Tuple[IO[Text], IO[Text]]]:
132
132
133
133
def set_key (
134
134
dotenv_path : _PathLike ,
135
- key_to_set : Text ,
136
- value_to_set : Text ,
137
- quote_mode : Text = "always" ,
135
+ key_to_set : str ,
136
+ value_to_set : str ,
137
+ quote_mode : str = "always" ,
138
138
export : bool = False ,
139
- ) -> Tuple [Optional [bool ], Text , Text ]:
139
+ ) -> Tuple [Optional [bool ], str , str ]:
140
140
"""
141
141
Adds or Updates a key/value to the given .env
142
142
@@ -176,9 +176,9 @@ def set_key(
176
176
177
177
def unset_key (
178
178
dotenv_path : _PathLike ,
179
- key_to_unset : Text ,
180
- quote_mode : Text = "always" ,
181
- ) -> Tuple [Optional [bool ], Text ]:
179
+ key_to_unset : str ,
180
+ quote_mode : str = "always" ,
181
+ ) -> Tuple [Optional [bool ], str ]:
182
182
"""
183
183
Removes a given key from the given .env
184
184
@@ -205,17 +205,17 @@ def unset_key(
205
205
206
206
207
207
def resolve_variables (
208
- values : Iterable [Tuple [Text , Optional [Text ]]],
208
+ values : Iterable [Tuple [str , Optional [str ]]],
209
209
override : bool ,
210
- ) -> Mapping [Text , Optional [Text ]]:
211
- new_values = {} # type: Dict[Text , Optional[Text ]]
210
+ ) -> Mapping [str , Optional [str ]]:
211
+ new_values = {} # type: Dict[str , Optional[str ]]
212
212
213
213
for (name , value ) in values :
214
214
if value is None :
215
215
result = None
216
216
else :
217
217
atoms = parse_variables (value )
218
- env = {} # type: Dict[Text , Optional[Text ]]
218
+ env = {} # type: Dict[str , Optional[str ]]
219
219
if override :
220
220
env .update (os .environ ) # type: ignore
221
221
env .update (new_values )
@@ -229,7 +229,7 @@ def resolve_variables(
229
229
return new_values
230
230
231
231
232
- def _walk_to_root (path : Text ) -> Iterator [Text ]:
232
+ def _walk_to_root (path : str ) -> Iterator [str ]:
233
233
"""
234
234
Yield directories starting from the given directory up to the root
235
235
"""
@@ -248,10 +248,10 @@ def _walk_to_root(path: Text) -> Iterator[Text]:
248
248
249
249
250
250
def find_dotenv (
251
- filename : Text = '.env' ,
251
+ filename : str = '.env' ,
252
252
raise_error_if_not_found : bool = False ,
253
253
usecwd : bool = False ,
254
- ) -> Text :
254
+ ) -> str :
255
255
"""
256
256
Search in increasingly higher folders for the given file
257
257
@@ -289,12 +289,12 @@ def _is_interactive():
289
289
290
290
291
291
def load_dotenv (
292
- dotenv_path : Union [Text , _PathLike , None ] = None ,
292
+ dotenv_path : Union [str , _PathLike , None ] = None ,
293
293
stream : Optional [io .StringIO ] = None ,
294
294
verbose : bool = False ,
295
295
override : bool = False ,
296
296
interpolate : bool = True ,
297
- encoding : Optional [Text ] = "utf-8" ,
297
+ encoding : Optional [str ] = "utf-8" ,
298
298
) -> bool :
299
299
"""Parse a .env file and then load all the variables found as environment variables.
300
300
@@ -320,12 +320,12 @@ def load_dotenv(
320
320
321
321
322
322
def dotenv_values (
323
- dotenv_path : Union [Text , _PathLike , None ] = None ,
323
+ dotenv_path : Union [str , _PathLike , None ] = None ,
324
324
stream : Optional [io .StringIO ] = None ,
325
325
verbose : bool = False ,
326
326
interpolate : bool = True ,
327
- encoding : Optional [Text ] = "utf-8" ,
328
- ) -> Dict [Text , Optional [Text ]]:
327
+ encoding : Optional [str ] = "utf-8" ,
328
+ ) -> Dict [str , Optional [str ]]:
329
329
"""
330
330
Parse a .env file and return its content as a dict.
331
331
0 commit comments