27 lines
737 B
Python
27 lines
737 B
Python
from dotenv import load_dotenv, find_dotenv
|
|
import os
|
|
|
|
class CreateEnvironmentFile:
|
|
def __init__(self):
|
|
self.path = find_dotenv()
|
|
|
|
def create_file(self, environment_vars:dict[str], path:str="./.env"):
|
|
if environment_vars:
|
|
data = self.convert_dict_to_list(environment_vars)
|
|
with open(path, 'w') as f:
|
|
f.writelines(data)
|
|
|
|
def convert_dict_to_list(self, data_dict:dict):
|
|
data = []
|
|
for k, v in data_dict.items():
|
|
data.append(f"{k}={v}\n")
|
|
return data
|
|
|
|
@classmethod
|
|
def load_dotenv(cls, environment_vars:dict[str]=None):
|
|
new_class = cls()
|
|
if os.path.exists(new_class.path):
|
|
return load_dotenv(new_class.path)
|
|
else:
|
|
return new_class.create_file(environment_vars)
|