33import warnings
44from collections import OrderedDict
55
6+
67def load_dotenv (dotenv_path ):
78 """
89 Read a .env file and load into os.environ.
@@ -20,42 +21,44 @@ def read_dotenv(dotenv_path=None):
2021 Prior name of load_dotenv function.
2122
2223 Deprecated and pending removal
23-
24+
2425 If not given a path to a dotenv path, does filthy magic stack backtracking
2526 to find manage.py and then find the dotenv.
26- """
27+ """
2728 warnings .warn ("read_dotenv deprecated, use load_dotenv instead" )
2829 if dotenv_path is None :
29- warnings .warn ("read_dotenv without an explicit path is deprecated and will be removed soon" )
30+ warnings .warn (
31+ "read_dotenv without an explicit path is deprecated and will be removed soon" )
3032 frame = sys ._getframe ()
31- dotenv_path = os .path .join (os .path .dirname (frame .f_back .f_code .co_filename ), '.env' )
33+ dotenv_path = os .path .join (
34+ os .path .dirname (frame .f_back .f_code .co_filename ), '.env' )
3235 return load_dotenv (dotenv_path )
3336
3437
3538def get_key (dotenv_path , key_to_get ):
3639 """
37- Gets the value of a given key from the given .env
38-
40+ Gets the value of a given key from the given .env
41+
3942 If the .env path given doesn't exist, fails
40- """
43+ """
4144 key_to_get = str (key_to_get )
4245 if not os .path .exists (dotenv_path ):
4346 warnings .warn ("can't read %s - it doesn't exist." % dotenv_path )
4447 return None
4548 dotenv_as_dict = OrderedDict (parse_dotenv (dotenv_path ))
46- if dotenv_as_dict . has_key ( key_to_get ) :
49+ if key_to_get in dotenv_as_dict :
4750 return dotenv_as_dict [key_to_get ]
4851 else :
4952 warnings .warn ("key %s not found in %s." % (key_to_get , dotenv_path ))
5053 return None
51-
52-
54+
55+
5356def set_key (dotenv_path , key_to_set , value_to_set ):
5457 """
55- Adds or Updates a key/value to the given .env
56-
58+ Adds or Updates a key/value to the given .env
59+
5760 If the .env path given doesn't exist, fails instead of risking creating
58- an orphan .env somewhere in the filesystem
61+ an orphan .env somewhere in the filesystem
5962 """
6063 key_to_set = str (key_to_set )
6164 value_to_set = str (value_to_set ).strip ("'" ).strip ('"' )
@@ -70,23 +73,24 @@ def set_key(dotenv_path, key_to_set, value_to_set):
7073
7174def unset_key (dotenv_path , key_to_unset ):
7275 """
73- Removes a given key from the given .env
74-
76+ Removes a given key from the given .env
77+
7578 If the .env path given doesn't exist, fails
76- If the given key doesn't exist in the .env, fails
79+ If the given key doesn't exist in the .env, fails
7780 """
7881 key_to_unset = str (key_to_unset )
7982 if not os .path .exists (dotenv_path ):
8083 warnings .warn ("can't delete from %s - it doesn't exist." % dotenv_path )
8184 return None
8285 dotenv_as_dict = OrderedDict (parse_dotenv (dotenv_path ))
83- if dotenv_as_dict . has_key ( key_to_unset ) :
86+ if key_to_unset in dotenv_as_dict :
8487 dotenv_as_dict .pop (key_to_unset , None )
85- else :
86- warnings .warn ("key %s not removed from %s - key doesn't exist." % (key_to_unset , dotenv_path ))
88+ else :
89+ warnings .warn (
90+ "key %s not removed from %s - key doesn't exist." % (key_to_unset , dotenv_path ))
8791 return None
8892 success = flatten_and_write (dotenv_path , dotenv_as_dict )
89- return success , key_to_unset
93+ return success , key_to_unset
9094
9195
9296def parse_dotenv (dotenv_path ):
@@ -98,14 +102,14 @@ def parse_dotenv(dotenv_path):
98102 k , v = line .split ('=' , 1 )
99103 v = v .strip ("'" ).strip ('"' )
100104 yield k , v
101-
105+
102106
103107def flatten_and_write (dotenv_path , dotenv_as_dict ):
104108 with open (dotenv_path , "w" ) as f :
105109 for k , v in dotenv_as_dict .items ():
106110 f .write ('%s="%s"\r \n ' % (k , v ))
107111 return True
108-
112+
109113if __name__ == "__main__" :
110114 import argparse
111115 parser = argparse .ArgumentParser ()
@@ -115,35 +119,35 @@ def flatten_and_write(dotenv_path, dotenv_as_dict):
115119 parser .add_argument ("value" , help = "the value you want to set 'key' to" , nargs = '?' )
116120 parser .add_argument ("--force" , help = "force writing even if the file at the given path doesn't end in .env" )
117121 args = parser .parse_args ()
118-
122+
119123 if not os .path .exists (args .file_path ):
120124 warnings .warn ("there doesn't appear to be a file at %s" % args .file_path )
121125 exit (1 )
122126 if not args .force :
123127 if not args .file_path .endswith (".env" ):
124128 warnings .warn ("the file %s doesn't appear to be a .env file, use --force to proceed" % args .file_path )
125129 exit (1 )
126-
127- if args .action == None :
130+
131+ if not args .action :
128132 with open (args .file_path ) as f :
129133 print f .read ()
130- exit (0 )
134+ exit (0 )
131135 elif args .action == "get" :
132136 stored_value = get_key (args .file_path , args .key )
133- if stored_value != None :
137+ if stored_value is not None :
134138 print (args .key )
135139 print (stored_value )
136140 else :
137141 exit (1 )
138142 elif args .action == "set" :
139143 success , key , value = set_key (args .file_path , args .key , args .value )
140- if success != None :
144+ if success is not None :
141145 print ("%s: %s" % (key , value ))
142146 else :
143147 exit (1 )
144148 elif args .action == "unset" :
145149 success , key = unset_key (args .file_path , args .key )
146- if success != None :
150+ if success is not None :
147151 print ("unset %s" % key )
148152 else :
149153 exit (1 )
@@ -156,4 +160,4 @@ def flatten_and_write(dotenv_path, dotenv_as_dict):
156160 # print("loaded %s into environment" % args.file_path)
157161 # else:
158162 # exit(1)
159- exit (0 )
163+ exit (0 )
0 commit comments