@@ -143,30 +143,40 @@ def rewrite(path):
143
143
shutil .move (dest .name , path )
144
144
145
145
146
- def set_key ( dotenv_path , key_to_set , value_to_set , quote_mode = "always" , export = False ):
147
- # type: (_PathLike, Text, Text, Text, bool) -> Tuple[Optional[bool], Text, Text]
146
+ def make_env_line ( key , value , quote_mode = "always" , export = False ):
147
+ # type: (Text, Text, Text, bool) -> Text
148
148
"""
149
- Adds or Updates a key/value to the given .env
150
-
151
- If the .env path given doesn't exist, fails instead of risking creating
152
- an orphan .env somewhere in the filesystem
149
+ Make a line which format fits to .env
153
150
"""
154
151
if quote_mode not in ("always" , "auto" , "never" ):
155
152
raise ValueError ("Unknown quote_mode: {}" .format (quote_mode ))
156
153
157
154
quote = (
158
155
quote_mode == "always"
159
- or (quote_mode == "auto" and not value_to_set .isalnum ())
156
+ or (quote_mode == "auto" and not value .isalnum ())
160
157
)
161
158
162
159
if quote :
163
- value_out = "'{}'" .format (value_to_set .replace ("'" , "\\ '" ))
160
+ value_out = "'{}'" .format (value .replace ("'" , "\\ '" ))
164
161
else :
165
- value_out = value_to_set
162
+ value_out = value
166
163
if export :
167
- line_out = 'export {}={}\n ' .format (key_to_set , value_out )
164
+ line_out = 'export {}={}\n ' .format (key , value_out )
168
165
else :
169
- line_out = "{}={}\n " .format (key_to_set , value_out )
166
+ line_out = "{}={}\n " .format (key , value_out )
167
+
168
+ return line_out
169
+
170
+
171
+ def set_key (dotenv_path , key_to_set , value_to_set , quote_mode = "always" , export = False ):
172
+ # type: (_PathLike, Text, Text, Text, bool) -> Tuple[Optional[bool], Text, Text]
173
+ """
174
+ Adds or Updates a key/value to the given .env
175
+
176
+ If the .env path given doesn't exist, fails instead of risking creating
177
+ an orphan .env somewhere in the filesystem
178
+ """
179
+ line_out = make_env_line (key_to_set , value_to_set , quote_mode , export )
170
180
171
181
with rewrite (dotenv_path ) as (source , dest ):
172
182
replaced = False
@@ -356,3 +366,29 @@ def dotenv_values(
356
366
override = True ,
357
367
encoding = encoding ,
358
368
).dict ()
369
+
370
+
371
+ def update_dict_to_dotenv (dotenv_path , env_dict , quote_mode = "always" , export = False ):
372
+ # type: (_PathLike, Dict[Text, Optional[Text]], Text, bool) -> None
373
+ """
374
+ Adds or Updates key/value pairs in the given dictionary to the given .env
375
+
376
+ If the .env path given doesn't exist, fails instead of risking creating
377
+ an orphan .env somewhere in the filesystem
378
+ """
379
+ key_to_line = {}
380
+
381
+ for key_to_set , value_to_set in env_dict .items ():
382
+ env_line = make_env_line (key_to_set , value_to_set , quote_mode , export )
383
+ key_to_line [key_to_set ] = env_line
384
+
385
+ with rewrite (dotenv_path ) as (source , dest ):
386
+ for mapping in with_warn_for_invalid_lines (parse_stream (source )):
387
+ if mapping .key in key_to_line :
388
+ line_out = key_to_line .pop (mapping .key )
389
+ dest .write (line_out )
390
+ else :
391
+ dest .write (mapping .original .string )
392
+
393
+ for _ , line_out in key_to_line .items ():
394
+ dest .write (line_out )
0 commit comments