ForwardXMPP/forwardxmpp/__main__.py
2020-01-25 18:19:18 -05:00

51 lines
1.4 KiB
Python

#!/usr/bin/env python3
import argparse
import os
import pathlib
import logging
import sys
from . import bot as bot_mod, config as config_mod
def parse_arguments(arglist):
try:
config_dir = pathlib.Path(os.environ['XDG_CONFIG_DIR'])
except KeyError:
if os.getuid():
config_dir = pathlib.Path.home() / '.config'
else:
config_dir = pathlib.Path('/etc')
parser = argparse.ArgumentParser()
parser.add_argument(
'--config-file', '-C',
type=pathlib.Path,
default=config_dir / 'forwardxmpp/config.ini',
help="Path to INI configuration file."
" Default %(default)s.",
)
return parser.parse_args(arglist)
def setup_logger(logger, loglevel, stream):
formatter = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
handler = logging.StreamHandler(stream)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(loglevel)
def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
args = parse_arguments(arglist)
config = config_mod.Config()
for _ in config.read_paths([args.config_file]):
pass
setup_logger(logging.getLogger(), config.get_loglevel(logging.WARNING), stderr)
if not config.ready():
return 3
bot = bot_mod.ForwardBot(config)
bot.connect()
bot.process()
return 0
if __name__ == '__main__':
exit(main())