Files
omnibot22/omnibot/__main__.py
Alek Ratzloff d7dd0277ca Update priority for config path
Previously, the environment variable would take priority over the
command line argument. This is now reversed.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2022-06-04 20:41:23 -07:00

56 lines
1.3 KiB
Python

import argparse
import asyncio
import logging
import os
from pathlib import Path
import sys
from .config import ServerConfig
from .bot import Bot
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) 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())