Files
omnibot22/omnibot/__main__.py
Alek Ratzloff fcb4bd6df1 Update to Python 3.12
* Remove toml dependency since that comes with Python as of 3.11
* Update toml usage to tomllib in config.py
* Update `with open(...)` for toml file reading to be 'rb'
* Update Pipfile.lock for locked dependencies to work with Python 3.12

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2024-07-18 11:39:34 -07:00

70 lines
1.7 KiB
Python

import argparse
import asyncio
import logging
import os
from pathlib import Path
import sys
from .config import ServerConfig
from .bot import Bot
# ADD TRACE
# add TRACE logging level to logging module
TRACE = 5
logging.addLevelName(TRACE, "TRACE")
logging.TRACE = TRACE
def trace(self, message, *args, **kwargs):
if self.isEnabledFor(TRACE):
#args = list(map(str, args))
self._log(TRACE, message, args, **kwargs)
logging.Logger.trace = trace
# END ADD TRACE
parser = argparse.ArgumentParser(description="Run an IRC bot")
parser.add_argument("-c", "--config", type=str, default=None)
parser.add_argument(
"--loglevel",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
required=False,
)
args = parser.parse_args()
config_path = Path(args.config or os.environ.get("OMNIBOT_CONFIG", "config.toml"))
config = ServerConfig()
try:
with open(config_path, 'rb') as fp:
config.load(fp)
except FileNotFoundError:
print(f"ERROR: config file not found: {config_path}", file=sys.stderr)
sys.exit(1)
# Set log level now
loglevel = logging.INFO
if args.loglevel:
loglevel = getattr(logging, args.loglevel)
elif config.loglevel:
loglevel = getattr(logging, config.loglevel)
logging.basicConfig(
level=loglevel,
format="%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s",
)
log = logging.getLogger(__name__)
log.info("Loaded config file %s", args.config)
log.debug("Using configuration: %s", config)
bot = Bot(config)
try:
asyncio.run(bot.run())
except KeyboardInterrupt:
log.info("Got ctrl-c")
finally:
log.info("Quitting, press ctrl-c to quit immediately")
bot.quit()
asyncio.run(bot.keepalive())