Better makeFiles()

This commit is contained in:
Nikolaj Danger
2020-08-05 11:50:29 +02:00
parent b241dc13a8
commit e799eb792e
3 changed files with 117 additions and 193 deletions

View File

@ -13,6 +13,7 @@ logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
# Capitalizes all words except some of them
no_caps_list = ["of","the"]
def cap(s):
# Capitalizes a strink like a movie title
word_number = 0
lst = s.split()
res = ''
@ -25,7 +26,7 @@ def cap(s):
return res
def time_in_range(start, end, x):
"""Return true if x is in the range [start, end]"""
# Return true if x is in the range [start, end]
if start <= end:
return start <= x <= end
else:
@ -134,146 +135,45 @@ def findWikiPage(search : str):
logThis("Couldn't find the page (error code 1002)")
return "", "Couldn't find page (error code 1002)", ""
def makeJsonFile(path,content):
# Creates json file if it doesn't exist
try:
f = open(path,"r")
except:
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
with open(path,"w") as f:
json.dump(content,f,indent = 4)
finally:
f.close()
def makeTxtFile(path,content):
# Creates txt file if it doesn't exist
try:
f = open(path,"r")
except:
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
with open(path,"w") as f:
f.write(content)
finally:
f.close()
def makeFolder(path):
if os.path.isdir(path) == False:
os.makedirs(path)
logThis("The "+path.split("/")[-1]+" directory didn't exist")
def makeFiles():
# Creates swcharacters.json if it doesn't exist
try:
f = open("resources/starWars/swcharacters.json","r")
except:
logThis("swcharacters.json didn't exist. Making it now.")
emptyDict = {}
with open("resources/starWars/swcharacters.json","w") as f:
json.dump(emptyDict,f,indent = 4)
finally:
f.close()
# Creates games.json if it doesn't exist
try:
f = open("resources/games/games.json","r")
except:
logThis("games.json didn't exist. Making it now.")
data = {"trivia questions":{},"blackjack games":{},"4 in a row games": {},"users":{}}
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
with open("resources/startingFiles.json") as f:
data = json.load(f)
# Creates hexGames.json if it doesn't exist
try:
f = open("resources/games/hexGames.json","r")
except:
logThis("hexGames.json didn't exist. Making it now.")
data = {}
with open("resources/games/hexGames.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
# Creates monsters.json if it doesn't exist
try:
f = open("resources/lookup/monsters.json","r")
except:
logThis("monsters.json didn't exist. Making it now.")
with open("resources/lookup/lookupExamples.json") as f:
data = json.load(f)["monster"]
with open("resources/lookup/monsters.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
# Creates spells.json if it doesn't exist
try:
f = open("resources/lookup/spells.json","r")
except:
logThis("spells.json didn't exist. Making it now.")
with open("resources/lookup/lookupExamples.json") as f:
data = json.load(f)["spell"]
with open("resources/lookup/spells.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
for path, content in data["json"].items():
makeJsonFile(path,content)
# Creates users.json if it doesn't exist
try:
f = open("resources/users.json","r")
except:
logThis("users.json didn't exist. Making it now.")
data = {}
with open("resources/users.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
# Creates destinyPoints.txt if it doesn't exist
try:
f = open("resources/starWars/destinyPoints.txt","r")
except:
logThis("destinyPoints.txt didn't exist. Making it now.")
with open("resources/starWars/destinyPoints.txt","w") as f:
f.write("")
finally:
f.close()
for path, content in data["txt"].items():
makeTxtFile(path,content)
# Creates movies.txt if it doesn't exist
try:
f = open("resources/movies.txt","r")
except:
logThis("movies.txt didn't exist. Making it now.")
with open("resources/movies.txt","w") as f:
f.write("The Room")
finally:
f.close()
# Creates names.txt if it doesn't exist
try:
f = open("resources/names.txt","r")
except:
logThis("names.txt didn't exist. Making it now.")
with open("resources/names.txt","w") as f:
f.write("Gandalf")
finally:
f.close()
# Creates token.txt if it doesn't exist
try:
f = open("token.txt","r")
except:
logThis("token.txt didn't exist. Write your bot token below, or in token.txt later.")
token = input()
with open("token.txt","w") as f:
f.write(token)
finally:
f.close()
# Creates the blackjacktables foulder if it doesn't exist
if os.path.isdir("resources/games/blackjackTables") == False:
os.makedirs("resources/games/blackjackTables")
logThis("The tables directory didn't exist")
# Creates the 4InARowBoards foulder if it doesn't exist
if os.path.isdir("resources/games/4InARowBoards") == False:
os.makedirs("resources/games/4InARowBoards")
logThis("The 4 in a row boards directory didn't exist")
# Creates the hexBoards foulder if it doesn't exist
if os.path.isdir("resources/games/hexBoards") == False:
os.makedirs("resources/games/hexBoards")
logThis("The Hex boards directory didn't exist")
# Creates the oldImages foulder if it doesn't exist
if os.path.isdir("resources/games/oldImages") == False:
os.makedirs("resources/games/oldImages")
logThis("The old images directory didn't exist")
# Creates the blackjackCards foulder if it doesn't exist
if os.path.isdir("resources/games/blackjackCards") == False:
os.makedirs("resources/games/blackjackCards")
logThis("The blackjack cards directory didn't exist")
# Creates the hilo foulder if it doesn't exist
if os.path.isdir("resources/games/hilo") == False:
os.makedirs("resources/games/hilo")
logThis("The hi-lo directory didn't exist")
for path in data["folder"]:
makeFolder(path)
# Replaces multiple things with the same thing
def replaceMultiple(mainString, toBeReplaces, newString):