Convert config.py to yml

trunk
mio 2018-09-15 23:18:44 +00:00
parent 45029d9525
commit f0103c73db
5 changed files with 50 additions and 32 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@ __pycache__/
*.py[cod]
*$py.class
*.swp
*config.yml

View File

@ -1,14 +0,0 @@
# Server and channel settings
server = ("localhost", 6667)
channels = ["#rktest"]
bot_nick = "ramenkan"
# Admin user and code for admin actions
admin_user = "mio"
admin_code = "ramen"
# Request prefix, e.g. "!<request>"
req_prefix = "!"
# Print messages to stdout
debug = True

34
irc.py
View File

@ -1,4 +1,5 @@
import socket
import yaml
from sys import exit
from time import sleep
@ -6,23 +7,32 @@ from time import sleep
class IRC:
"""Methods for basic IRC communication."""
debug = False
def config(self, conf):
"""Load runtime settings from a YAML config file, and returns a
dictionary of config values."""
yml = open(conf, "r")
cfg = yaml.safe_load(yml)
yml.close()
self.server = (cfg["server"]["host"], cfg["server"]["port"])
self.channels = cfg["channels"]
self.bot_nick = cfg["bot_nick"]
self.req_prefix = cfg["req_prefix"]
self.admin_user = cfg["admin"]["user"]
self.admin_code = cfg["admin"]["code"]
self.debug = cfg["debug"]
return cfg
def run(self, listen_hook, conf):
"""A routine that connects to a server, joins channels, and initialises
the request listener in a loop."""
# Conf settings needed by listen(), also used by disconnect()
self.bot_nick = conf.bot_nick
self.admin_user = conf.admin_user
self.admin_code = conf.admin_code
self.connect(conf.server, self.bot_nick)
self.join_channels(conf.channels)
def run(self, listen_hook):
"""A routine that connects to a server, joins channels, and attaches
the request listener hook to a loop."""
self.connect(self.server, self.bot_nick)
self.join_channels(self.channels)
while 1:
sleep(2)
data = self.receive()
self.keepalive(data)
self.msg = self.parse(data, conf.req_prefix)
for c in conf.channels:
self.msg = self.parse(data, self.req_prefix)
for c in self.channels:
# Pass in a context dict for handlers
listen_hook({"msg": self.msg, "listen_chan": c})

View File

@ -0,0 +1,20 @@
# Server and channel settings
server:
host: "localhost"
port: 6667
channels:
- "#rktest"
bot_nick: "ramenkan"
# User and code for admin actions
admin:
user: "mio"
code: "ramen"
# Request prefix, e.g. "!" for "!<request>"
req_prefix: "!"
# Print messages to stdout
debug: True

View File

@ -1,6 +1,5 @@
from random import randint
import config as cfg
from irc import IRC
@ -10,16 +9,17 @@ class Ramen:
def main(self):
"""Instantiate an IRC object and attach the listeners."""
self.irc = IRC()
self.irc.debug = cfg.debug
self.irc.run(self.add_listeners, cfg)
self.cfg = self.irc.config("ramen.config.sample.yml")
self.irc.run(self.add_listeners)
def add_listeners(self, cxt):
"""Map triggers to handlers."""
self.irc.listen(cxt, "exit " + cfg.admin_code, self.quit, admin=True)
self.irc.listen(cxt, "exit " + self.cfg["admin"]["code"], self.quit, \
admin=True)
self.irc.listen(cxt, "rollcall", self.rollcall)
self.irc.listen(cxt, "help", self.rollcall)
self.irc.listen(cxt, "water " + cfg.bot_nick, self.water)
self.irc.listen(cxt, "botsnack " + cfg.bot_nick, self.botsnack)
self.irc.listen(cxt, "water " + self.cfg["bot_nick"], self.water)
self.irc.listen(cxt, "botsnack " + self.cfg["bot_nick"], self.botsnack)
def quit(self, cxt):
"""Disconnect from the server and quit."""