-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrunner.py
63 lines (51 loc) · 2.12 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import contextlib
import logging
import os
from logging.handlers import TimedRotatingFileHandler
from obsbot import __version__, __codename__
from obsbot.main import OBSBot
_log_dt_fmt = '%Y-%m-%d %H:%M:%S'
_log_fmt = '[{asctime}] [{levelname}] {name}: {message}'
logging.basicConfig(format=_log_fmt, datefmt=_log_dt_fmt, style='{', level=logging.INFO)
logger = logging.getLogger('runner')
@contextlib.contextmanager
def setup_logging(logfile=None, debug=False):
try:
logging.getLogger('discord').setLevel(logging.WARNING)
logging.getLogger('discord.http').setLevel(logging.WARNING)
log = logging.getLogger()
log.setLevel(logging.INFO if not debug else logging.DEBUG)
if logfile:
handler = TimedRotatingFileHandler(
filename=logfile, when='midnight', utc=True, encoding='utf-8', backupCount=5
)
fmt = logging.Formatter(_log_fmt, _log_dt_fmt, style='{')
handler.setFormatter(fmt)
log.addHandler(handler)
else:
logger.warning('Logging to file is disabled')
yield
finally:
handlers = log.handlers[:]
for handler in handlers:
handler.close()
log.removeHandler(handler)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--debug', dest='debug', action='store_true', help='Set loglevel to debug')
parser.add_argument('-l', '--logfile', dest='logfile', action='store', help='Logfile (optional)')
parser.add_argument('-V', '--version', dest='version', action='store_true', help='Print version and exit')
parser.add_argument(
'-c', '--config-file', dest='config_file', action='store', help='Configuration file to run bot with'
)
args = parser.parse_args()
if args.version:
print(f'OBS Bot version: {__version__} - "{__codename__}"')
exit(0)
if not args.config_file:
print('No config file specified!')
exit(1)
with setup_logging(logfile=args.logfile, debug=args.debug):
bot = OBSBot(os.path.realpath(args.config_file))
bot.run()