Support reading config from stdin

This commit is contained in:
dave 2017-05-01 08:29:20 -07:00
parent ee71fe69d2
commit 3b1eaae929
2 changed files with 9 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import logging
import signal
from argparse import ArgumentParser
from pyircbot import PyIRCBot
from json import loads
if __name__ == "__main__":
@ -16,6 +17,8 @@ if __name__ == "__main__":
parser = ArgumentParser(description="Run pyircbot")
parser.add_argument("-c", "--config", help="Path to config file", required=True)
parser.add_argument("--debug", action="store_true", help="Dump raw irc network")
parser.add_argument("-q", "--quit-message", help="Quit message if killed by signal",
default="received signal {}")
args = parser.parse_args()
@ -25,9 +28,9 @@ if __name__ == "__main__":
if not args.config:
log.critical("No bot config file specified (-c). Exiting.")
sys.exit(0)
sys.exit(1)
botconfig = PyIRCBot.load(args.config)
botconfig = loads(sys.stdin.read()) if args.config == "-" else PyIRCBot.load(args.config)
log.debug(botconfig)
@ -35,7 +38,7 @@ if __name__ == "__main__":
def signal_handler(signum, stack):
print('Received:', signum)
bot.kill(message="received signal {}".format(signum))
bot.kill(message=args.quit_message.format(signum))
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

View File

@ -12,6 +12,7 @@ from pyircbot.rpc import BotRPC
from pyircbot.irccore import IRCCore
from collections import namedtuple
from socket import AF_INET, AF_INET6
from json import load
import os.path
import asyncio
@ -370,7 +371,7 @@ class PyIRCBot(object):
"""
if filepath.endswith(".json"):
from json import load
return load(open(filepath, 'r'))
with open(filepath, 'r') as f:
return load(f)
else:
raise Exception("Unknown config format")