ForwardXMPP/forwardxmpp/__main__.py

54 lines
1.5 KiB
Python
Raw Normal View History

2020-01-25 23:18:11 +00:00
#!/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)
root_logger = logging.getLogger()
2020-01-25 23:18:11 +00:00
config = config_mod.Config()
setup_logger(root_logger, config.get_loglevel(), stderr)
2020-01-25 23:18:11 +00:00
for _ in config.read_paths([args.config_file]):
pass
root_logger.setLevel(config.get_loglevel())
2020-01-25 23:18:11 +00:00
if not config.ready():
return 3
bot = bot_mod.ForwardBot(config)
bot.connect()
bot.process()
return 0
if __name__ == '__main__':
exit(main())