Files
Gwendolyn/utils/utilFunctions.py
NikolajDanger 41bc121a91 📝 Better Logging
2021-04-02 21:00:30 +02:00

132 lines
3.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import logging
import os
import sys
from .helperClasses import Options
FORMAT = " %(asctime)s | %(name)-16s | %(levelname)-8s | %(message)s"
PRINTFORMAT = "%(asctime)s - %(message)s"
DATEFORMAT = "%Y-%m-%d %H:%M:%S"
logging.addLevelName(25, "PRINT")
logging.basicConfig(format=FORMAT, datefmt=DATEFORMAT, level=logging.INFO, filename="gwendolyn.log")
logger = logging.getLogger("Gwendolyn")
printer = logging.getLogger("printer")
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(fmt = PRINTFORMAT, datefmt=DATEFORMAT))
printer.addHandler(handler)
printer.propagate = False
def getParams():
with open("resources/slashParameters.json", "r") as f:
params = json.load(f)
options = Options()
if options.testing:
for p in params:
params[p]["guild_ids"] = options.guildIds
return params
def logThis(messages, channel : str = "", level : int = 20):
channel = channel.replace("Direct Message with ","")
if type(messages) is str:
messages = [messages]
printMessage = messages[0]
for x, msg in enumerate(messages):
if channel != "":
messages[x] = f"{msg} - ({channel})"
if len(messages) > 1:
printMessage += " (details in log)"
if level >= 25:
printer.log(level, printMessage)
for logMessage in messages:
logger.log(level, logMessage)
# Capitalizes all words except some of them
def cap(s):
no_caps_list = ["of","the"]
# Capitalizes a strink like a movie title
word_number = 0
lst = s.split()
res = ''
for word in lst:
word_number += 1
if word not in no_caps_list or word_number == 1:
word = word.capitalize()
res += word+" "
res = res[:-1]
return res
def makeFiles():
def makeJsonFile(path,content):
# Creates json file if it doesn't exist
if not os.path.isfile(path):
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
with open(path,"w") as f:
json.dump(content,f,indent = 4)
def makeTxtFile(path,content):
# Creates txt file if it doesn't exist
if not os.path.isfile(path):
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
with open(path,"w") as f:
f.write(content)
def directory(path):
if not os.path.isdir(path):
os.makedirs(path)
logThis("The "+path.split("/")[-1]+" directory didn't exist")
with open("resources/startingFiles.json") as f:
data = json.load(f)
for path, content in data["json"].items():
makeJsonFile(path,content)
for path, content in data["txt"].items():
makeTxtFile(path,content)
for path in data["folder"]:
directory(path)
# Replaces multiple things with the same thing
def replaceMultiple(mainString, toBeReplaces, newString):
# Iterate over the strings to be replaced
for elem in toBeReplaces :
# Check if string is in the main string
if elem in mainString :
# Replace the string
mainString = mainString.replace(elem, newString)
return mainString
def emojiToCommand(emoji):
if emoji == "1":
return 1
elif emoji == "2":
return 2
elif emoji == "3":
return 3
elif emoji == "4":
return 4
elif emoji == "5":
return 5
elif emoji == "6":
return 6
elif emoji == "7":
return 7
elif emoji == "🎲":
return "roll"
elif emoji == "":
return "none"
elif emoji == "✔️":
return 1
else: return ""