create .env file if its not exists
This commit is contained in:
@@ -12,14 +12,14 @@
|
|||||||
# To get you started we've included code to prevent your Battlesnake from moving backwards.
|
# To get you started we've included code to prevent your Battlesnake from moving backwards.
|
||||||
# For more info see docs.battlesnake.com
|
# For more info see docs.battlesnake.com
|
||||||
|
|
||||||
|
from server.CreateEnvironmentFile import CreateEnvironmentFile
|
||||||
from server.Server import Server
|
from server.Server import Server
|
||||||
|
|
||||||
from dotenv import load_dotenv, find_dotenv
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Start server when `python main.py` is run
|
# Start server when `python main.py` is run
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
load_dotenv(find_dotenv())
|
CreateEnvironmentFile.load_dotenv({"STORE_GAME_HISTORY": True, "DEBUG": True, "SNAKE": "DummSnake"})
|
||||||
|
|
||||||
server = Server(
|
server = Server(
|
||||||
data_path=os.path.dirname(__file__),
|
data_path=os.path.dirname(__file__),
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
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)
|
||||||
Reference in New Issue
Block a user