refactor loading .env file

This commit is contained in:
debugtalk
2018-08-07 15:06:08 +08:00
parent aaac5ba323
commit 167fc9cc02
5 changed files with 34 additions and 11 deletions

View File

@@ -138,24 +138,31 @@ def load_folder_files(folder_path, recursive=True):
def load_dot_env_file(path):
""" load .env file and set to os.environ
""" load .env file
"""
if not path:
path = os.path.join(os.getcwd(), ".env")
if not os.path.isfile(path):
logger.log_debug(".env file not exist: {}".format(path))
return
return {}
else:
if not os.path.isfile(path):
raise exceptions.FileNotFound("env file not exist: {}".format(path))
logger.log_info("Loading environment variables from {}".format(path))
env_variables_mapping = {}
with io.open(path, 'r', encoding='utf-8') as fp:
for line in fp:
variable, value = line.split("=")
variable = variable.strip()
os.environ[variable] = value.strip()
logger.log_debug("Loaded variable: {}".format(variable))
if "=" in line:
variable, value = line.split("=")
elif ":" in line:
variable, value = line.split(":")
else:
raise exceptions.FileFormatError(".env format error")
env_variables_mapping[variable.strip()] = value.strip()
return env_variables_mapping
###############################################################################