#!/usr/bin/env python3 import logging import threading import sys import getopt import yaml from . import bot def start_new_thread(func, join=False, args=(), kwargs=None): t = threading.Thread(target=func, args=args, kwargs=kwargs) if not join: t.daemon = True t.start() if join: t.join() def usage(): print("Usage: %s [-q|-d] [-c file]" % sys.argv[0]) print("Options:") print(" -h Display this text") print(" -q Quiet, set log level to WARNING") print(" -c Location of config file (default: ./config.yaml)") 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) db = bot.JsonDb(config["database"]) bot.init(config, db) try: start_new_thread(bot.reminder_loop) start_new_thread(bot.run, join=True) except KeyboardInterrupt: logging.info("Interrupted, exiting") if __name__ == "__main__": try: opts, args = getopt.getopt(sys.argv[1:], "hqc:", ["help"]) except getopt.GetoptError as e: print(str(e)) exit(1) def readopt(name): for e in opts: if e[0] == name: return e[1] if readopt("-h") is not None or readopt("--help") is not None: 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") main(configpath, loglevel)