Rewrite config to use toml instead of .env

This commit is contained in:
lm
2021-09-27 01:47:47 +02:00
parent 1df3e08bee
commit de05b885c2
7 changed files with 140 additions and 118 deletions
+60
View File
@@ -0,0 +1,60 @@
"""
TBD
"""
__author__ = "Lukas Mahler"
__version__ = "0.0.0"
__date__ = "27.09.2021"
__email__ = "m@hler.eu"
__status__ = "Development"
# Imports
from pycoingecko import CoinGeckoAPI
class GeckoAPI:
def __init__(self):
self.api = self.getAPI()
self.coinlist = self.getCoins()
@staticmethod
def getAPI():
"""
"""
api = CoinGeckoAPI()
if api.ping()['gecko_says'] == '(V3) To the Moon!':
return api
else:
raise ConnectionError("Couldnt connect to Coingecko API.")
def getCoins(self):
"""
"""
return self.api.get_coins_list()
def getSymbol(self, symbol_id, curr="EUR", decimal=2):
"""
"""
matching = [x for x in self.coinlist if x["id"] == symbol_id.lower()]
if len(matching) == 1:
resp_dict = self.api.get_price(ids=matching[0]["id"], vs_currencies=curr.lower())
price = resp_dict[matching[0]["id"]][curr.lower()]
if isinstance(price, int):
price = float(price) # Convert to always have floats
price = f"{price:.{decimal}f}"
return price
else:
print(matching)
# No unique coin found
return "Not found"
if __name__ == "__main__":
exit()
+12
View File
@@ -0,0 +1,12 @@
[Connection]
host = "example.com"
port = 10011
sid = 1
[Authentication]
user = "exampleuser"
pwd = "password"
[Misc]
nickname = "myTS3-Bot"
whitelisted = false
+77
View File
@@ -0,0 +1,77 @@
"""
TBD
"""
__author__ = "Lukas Mahler"
__version__ = "0.0.0"
__date__ = "27.09.2021"
__email__ = "m@hler.eu"
__status__ = "Development"
# Default
import sys
import shutil
import os.path
import logging
from datetime import date
# Custom
import toml
def unhandledException(exc_type, exc_value, exc_traceback):
"""
src = https://stackoverflow.com/a/16993115/5593051
"""
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
def getconf(fname):
"""
"""
if fname.endswith(".toml"):
if os.path.isfile(fname):
try:
data = toml.load(fname)
return data
except ValueError as e:
log.critical(e)
# log.critical("The provided '.toml' seems to be invalid.")
exit(1)
else:
log.critical(f"Couldn't locate the '.toml' file [{fname}].")
log.info("Creating a new '.toml' file from template, please edit and restart.")
shutil.copy("src/template.toml", fname)
exit(1)
else:
log.critical("The provided config file is not a '.toml' file.")
log.info("Creating a new '.toml' file from template, please edit and restart.")
shutil.copy("src/template.toml", "prod.toml")
exit(1)
def setupLogger(lvl="DEBUG"):
"""
"""
global log # Needed to log exceptions in src\util
log = logging.getLogger()
now = date.today().strftime("%Y-%m-%d")
handler = logging.FileHandler(f"myTS3_{now}.log", encoding="utf-8")
logformat = logging.Formatter("%(asctime)s %(levelname)7s %(message)s", "%d-%m-%Y %H:%M:%S")
handler.setFormatter(logformat)
log.addHandler(handler)
log.setLevel(lvl)
return log
if __name__ == "__main__":
exit()