20
20
_PathLike = Text
21
21
22
22
23
- def with_warn_for_invalid_lines (mappings ):
24
- # type: (Iterator[Binding]) -> Iterator[Binding]
23
+ def with_warn_for_invalid_lines (mappings : Iterator [Binding ]) -> Iterator [Binding ]:
25
24
for mapping in mappings :
26
25
if mapping .error :
27
26
logger .warning (
@@ -32,9 +31,14 @@ def with_warn_for_invalid_lines(mappings):
32
31
33
32
34
33
class DotEnv ():
35
-
36
- def __init__ (self , dotenv_path , verbose = False , encoding = None , interpolate = True , override = True ):
37
- # type: (Union[Text, _PathLike, io.StringIO], bool, Union[None, Text], bool, bool) -> None
34
+ def __init__ (
35
+ self ,
36
+ dotenv_path : Union [Text , _PathLike , io .StringIO ],
37
+ verbose : bool = False ,
38
+ encoding : Union [None , Text ] = None ,
39
+ interpolate : bool = True ,
40
+ override : bool = True ,
41
+ ) -> None :
38
42
self .dotenv_path = dotenv_path # type: Union[Text,_PathLike, io.StringIO]
39
43
self ._dict = None # type: Optional[Dict[Text, Optional[Text]]]
40
44
self .verbose = verbose # type: bool
@@ -43,8 +47,7 @@ def __init__(self, dotenv_path, verbose=False, encoding=None, interpolate=True,
43
47
self .override = override # type: bool
44
48
45
49
@contextmanager
46
- def _get_stream (self ):
47
- # type: () -> Iterator[IO[Text]]
50
+ def _get_stream (self ) -> Iterator [IO [Text ]]:
48
51
if isinstance (self .dotenv_path , io .StringIO ):
49
52
yield self .dotenv_path
50
53
elif os .path .isfile (self .dotenv_path ):
@@ -55,8 +58,7 @@ def _get_stream(self):
55
58
logger .info ("Python-dotenv could not find configuration file %s." , self .dotenv_path or '.env' )
56
59
yield io .StringIO ('' )
57
60
58
- def dict (self ):
59
- # type: () -> Dict[Text, Optional[Text]]
61
+ def dict (self ) -> Dict [Text , Optional [Text ]]:
60
62
"""Return dotenv as dict"""
61
63
if self ._dict :
62
64
return self ._dict
@@ -70,15 +72,13 @@ def dict(self):
70
72
71
73
return self ._dict
72
74
73
- def parse (self ):
74
- # type: () -> Iterator[Tuple[Text, Optional[Text]]]
75
+ def parse (self ) -> Iterator [Tuple [Text , Optional [Text ]]]:
75
76
with self ._get_stream () as stream :
76
77
for mapping in with_warn_for_invalid_lines (parse_stream (stream )):
77
78
if mapping .key is not None :
78
79
yield mapping .key , mapping .value
79
80
80
- def set_as_environment_variables (self ):
81
- # type: () -> bool
81
+ def set_as_environment_variables (self ) -> bool :
82
82
"""
83
83
Load the current dotenv as system environment variable.
84
84
"""
@@ -90,8 +90,7 @@ def set_as_environment_variables(self):
90
90
91
91
return True
92
92
93
- def get (self , key ):
94
- # type: (Text) -> Optional[Text]
93
+ def get (self , key : Text ) -> Optional [Text ]:
95
94
"""
96
95
"""
97
96
data = self .dict ()
@@ -105,8 +104,7 @@ def get(self, key):
105
104
return None
106
105
107
106
108
- def get_key (dotenv_path , key_to_get ):
109
- # type: (Union[Text, _PathLike], Text) -> Optional[Text]
107
+ def get_key (dotenv_path : Union [Text , _PathLike ], key_to_get : Text ) -> Optional [Text ]:
110
108
"""
111
109
Gets the value of a given key from the given .env
112
110
@@ -116,8 +114,7 @@ def get_key(dotenv_path, key_to_get):
116
114
117
115
118
116
@contextmanager
119
- def rewrite (path ):
120
- # type: (_PathLike) -> Iterator[Tuple[IO[Text], IO[Text]]]
117
+ def rewrite (path : _PathLike ) -> Iterator [Tuple [IO [Text ], IO [Text ]]]:
121
118
try :
122
119
if not os .path .isfile (path ):
123
120
with io .open (path , "w+" ) as source :
@@ -133,8 +130,13 @@ def rewrite(path):
133
130
shutil .move (dest .name , path )
134
131
135
132
136
- def set_key (dotenv_path , key_to_set , value_to_set , quote_mode = "always" , export = False ):
137
- # type: (_PathLike, Text, Text, Text, bool) -> Tuple[Optional[bool], Text, Text]
133
+ def set_key (
134
+ dotenv_path : _PathLike ,
135
+ key_to_set : Text ,
136
+ value_to_set : Text ,
137
+ quote_mode : Text = "always" ,
138
+ export : bool = False ,
139
+ ) -> Tuple [Optional [bool ], Text , Text ]:
138
140
"""
139
141
Adds or Updates a key/value to the given .env
140
142
@@ -172,8 +174,11 @@ def set_key(dotenv_path, key_to_set, value_to_set, quote_mode="always", export=F
172
174
return True , key_to_set , value_to_set
173
175
174
176
175
- def unset_key (dotenv_path , key_to_unset , quote_mode = "always" ):
176
- # type: (_PathLike, Text, Text) -> Tuple[Optional[bool], Text]
177
+ def unset_key (
178
+ dotenv_path : _PathLike ,
179
+ key_to_unset : Text ,
180
+ quote_mode : Text = "always" ,
181
+ ) -> Tuple [Optional [bool ], Text ]:
177
182
"""
178
183
Removes a given key from the given .env
179
184
@@ -199,9 +204,10 @@ def unset_key(dotenv_path, key_to_unset, quote_mode="always"):
199
204
return removed , key_to_unset
200
205
201
206
202
- def resolve_variables (values , override ):
203
- # type: (Iterable[Tuple[Text, Optional[Text]]], bool) -> Mapping[Text, Optional[Text]]
204
-
207
+ def resolve_variables (
208
+ values : Iterable [Tuple [Text , Optional [Text ]]],
209
+ override : bool ,
210
+ ) -> Mapping [Text , Optional [Text ]]:
205
211
new_values = {} # type: Dict[Text, Optional[Text]]
206
212
207
213
for (name , value ) in values :
@@ -223,8 +229,7 @@ def resolve_variables(values, override):
223
229
return new_values
224
230
225
231
226
- def _walk_to_root (path ):
227
- # type: (Text) -> Iterator[Text]
232
+ def _walk_to_root (path : Text ) -> Iterator [Text ]:
228
233
"""
229
234
Yield directories starting from the given directory up to the root
230
235
"""
@@ -242,8 +247,11 @@ def _walk_to_root(path):
242
247
last_dir , current_dir = current_dir , parent_dir
243
248
244
249
245
- def find_dotenv (filename = '.env' , raise_error_if_not_found = False , usecwd = False ):
246
- # type: (Text, bool, bool) -> Text
250
+ def find_dotenv (
251
+ filename : Text = '.env' ,
252
+ raise_error_if_not_found : bool = False ,
253
+ usecwd : bool = False ,
254
+ ) -> Text :
247
255
"""
248
256
Search in increasingly higher folders for the given file
249
257
@@ -281,14 +289,13 @@ def _is_interactive():
281
289
282
290
283
291
def load_dotenv (
284
- dotenv_path = None ,
285
- stream = None ,
286
- verbose = False ,
287
- override = False ,
288
- interpolate = True ,
289
- encoding = "utf-8" ,
290
- ):
291
- # type: (Union[Text, _PathLike, None], Optional[io.StringIO], bool, bool, bool, Optional[Text]) -> bool # noqa
292
+ dotenv_path : Union [Text , _PathLike , None ] = None ,
293
+ stream : Optional [io .StringIO ] = None ,
294
+ verbose : bool = False ,
295
+ override : bool = False ,
296
+ interpolate : bool = True ,
297
+ encoding : Optional [Text ] = "utf-8" ,
298
+ ) -> bool :
292
299
"""Parse a .env file and then load all the variables found as environment variables.
293
300
294
301
- *dotenv_path*: absolute or relative path to .env file.
@@ -313,13 +320,12 @@ def load_dotenv(
313
320
314
321
315
322
def dotenv_values (
316
- dotenv_path = None ,
317
- stream = None ,
318
- verbose = False ,
319
- interpolate = True ,
320
- encoding = "utf-8" ,
321
- ):
322
- # type: (Union[Text, _PathLike, None], Optional[io.StringIO], bool, bool, Optional[Text]) -> Dict[Text, Optional[Text]] # noqa: E501
323
+ dotenv_path : Union [Text , _PathLike , None ] = None ,
324
+ stream : Optional [io .StringIO ] = None ,
325
+ verbose : bool = False ,
326
+ interpolate : bool = True ,
327
+ encoding : Optional [Text ] = "utf-8" ,
328
+ ) -> Dict [Text , Optional [Text ]]:
323
329
"""
324
330
Parse a .env file and return its content as a dict.
325
331
0 commit comments