tgsupportbot/supportbot/__main__.py

77 lines
1.8 KiB
Python
Raw Permalink Normal View History

2020-06-21 14:42:28 +02:00
#!/usr/bin/env python3
import logging
import yaml
import threading
import sys
import os
import shelve
import getopt
2024-07-01 20:50:12 +02:00
from pickle import Unpickler
2020-06-21 14:42:28 +02:00
2024-07-01 20:50:12 +02:00
from . import bot
2020-06-21 14:42:28 +02:00
2024-07-01 20:50:12 +02:00
def start_new_thread(func, join=False, args=(), kwargs=None):
2020-06-21 14:42:28 +02:00
t = threading.Thread(target=func, args=args, kwargs=kwargs)
if not join:
t.daemon = True
t.start()
if join:
t.join()
def usage():
2024-07-01 20:50:12 +02:00
print("Usage: %s [-q|-d] [-c file]" % sys.argv[0])
2020-06-21 14:42:28 +02:00
print("Options:")
print(" -h Display this text")
print(" -q Quiet, set log level to WARNING")
print(" -c Location of config file (default: ./config.yaml)")
2024-07-01 20:50:12 +02:00
# well this is just dumb
class RenamingUnpickler(Unpickler):
def find_class(self, module, name):
if module == "src.core":
module = "supportbot.bot"
return super().find_class(module, name)
2020-06-21 14:42:28 +02:00
def main(configpath, loglevel=logging.INFO):
with open(configpath, "r") as f:
config = yaml.safe_load(f)
logging.basicConfig(format="%(levelname)-7s [%(asctime)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=loglevel)
2024-07-01 20:50:12 +02:00
shelve.Unpickler = RenamingUnpickler
db = shelve.open(config["database"])
2020-06-21 14:42:28 +02:00
2024-07-01 20:50:12 +02:00
bot.init(config, db)
2020-06-21 14:42:28 +02:00
try:
2024-07-01 20:50:12 +02:00
start_new_thread(bot.run, join=True)
2020-06-21 14:42:28 +02:00
except KeyboardInterrupt:
logging.info("Interrupted, exiting")
db.close()
os._exit(1)
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hqc:", ["help"])
except getopt.GetoptError as e:
print(str(e))
exit(1)
2024-07-01 20:50:12 +02:00
2020-06-21 14:42:28 +02:00
# Process command line args
2024-07-01 20:50:12 +02:00
def readopt(name):
for e in opts:
if e[0] == name:
return e[1]
2021-12-19 22:37:12 +01:00
if readopt("-h") is not None or readopt("--help") is not None:
2020-06-21 14:42:28 +02:00
usage()
exit(0)
loglevel = logging.INFO
if readopt("-q") is not None:
loglevel = logging.WARNING
configpath = "./config.yaml"
if readopt("-c") is not None:
configpath = readopt("-c")
2024-07-01 20:50:12 +02:00
2020-06-21 14:42:28 +02:00
# Run the actual program
main(configpath, loglevel)