From 39e17d012cf774b13b263852dc9d2fdc0455f60c Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Thu, 29 Jun 2017 15:07:57 +0200 Subject: [PATCH] Move config to a JSON file Move config to a JSON file in order to allow for complex configuration of the ethercalc data model. Change-Id: I0d18bc6aa3fd8ea1739c64a3d488145435bebb4f --- README.rst | 26 +++++++++++--------------- config.ini.sample | 11 ----------- config.json.sample | 10 ++++++++++ ptgbot/bot.py | 43 +++++++++++++++++++------------------------ ptgbot/db.py | 2 +- 5 files changed, 41 insertions(+), 51 deletions(-) delete mode 100644 config.ini.sample create mode 100644 config.json.sample diff --git a/README.rst b/README.rst index ac4fcf2..f3c6f58 100644 --- a/README.rst +++ b/README.rst @@ -34,27 +34,23 @@ Example:: Testing ======= -Copy config.ini.sample to config.ini:: +Copy config.json.sample to config.json:: - cp config.ini.sample config.ini + cp config.json.sample config.json -Edit config.ini contents, for example:: +Edit config.json contents, for example:: - [ircbot] - nick=ptgbot - pass= - server=irc.freenode.net - port=6667 - channels=testptg - - [db] - filename=html/ptg.json - ethercalc= - cells= + { + "irc_nick": "ptgbot", + "irc_server": "irc.freenode.net", + "irc_port": 6667, + "irc_channel": "#testptg", + "db_filename": "html/ptg.json", + } In one terminal, run the bot:: - tox -evenv -- ptgbot -d config.ini + tox -evenv -- ptgbot -d config.json Join that channel and give a command to the bot:: diff --git a/config.ini.sample b/config.ini.sample deleted file mode 100644 index 2484887..0000000 --- a/config.ini.sample +++ /dev/null @@ -1,11 +0,0 @@ -[ircbot] -nick=NICKNAME -pass=PASSWORD -server=irc.freenode.net -port=6667 -channels=foo,bar - -[db] -filename=html/ptg.json -ethercalc=https://ethercalc.openstack.org/_/MyDocumentName/cells -cells=E9 E10 diff --git a/config.json.sample b/config.json.sample new file mode 100644 index 0000000..3221d56 --- /dev/null +++ b/config.json.sample @@ -0,0 +1,10 @@ +{ +"irc_nick": "NICK", +"irc_pass": "PASS", +"irc_server": "irc.freenode.net", +"irc_port": 6667, +"irc_channel": "#mychannel", +"db_filename": "html/ptg.json", +"ethercalc_url": "https://ethercalc.openstack.org/_/YOURDOC/cells", +"ethercalc_cells": ["E9", "E10", "E11"] +} diff --git a/ptgbot/bot.py b/ptgbot/bot.py index 0d6ffa4..549760e 100644 --- a/ptgbot/bot.py +++ b/ptgbot/bot.py @@ -16,9 +16,9 @@ # limitations under the License. import argparse -import configparser import daemon import irc.bot +import json import logging.config import os import time @@ -43,7 +43,7 @@ ANTI_FLOOD_SLEEP = 2 class PTGBot(irc.bot.SingleServerIRCBot): log = logging.getLogger("ptgbot.bot") - def __init__(self, nickname, password, server, port, channels, db): + def __init__(self, nickname, password, server, port, channel, db): if port == 6697: factory = irc.connection.Factory(wrapper=ssl.wrap_socket) irc.bot.SingleServerIRCBot.__init__(self, @@ -56,7 +56,7 @@ class PTGBot(irc.bot.SingleServerIRCBot): nickname, nickname) self.nickname = nickname self.password = password - self.channel_list = channels + self.channel = channel self.identify_msg_cap = False self.data = db @@ -77,10 +77,9 @@ class PTGBot(irc.bot.SingleServerIRCBot): if (self.password): self.log.debug("Identifying to nickserv") c.privmsg("nickserv", "identify %s " % self.password) - for channel in self.channel_list: - self.log.info("Joining %s" % channel) - c.join(channel) - time.sleep(ANTI_FLOOD_SLEEP) + self.log.info("Joining %s" % self.channel) + c.join(self.channel) + time.sleep(ANTI_FLOOD_SLEEP) def on_cap(self, c, e): self.log.debug("Received cap response %s" % repr(e.arguments)) @@ -164,11 +163,11 @@ class PTGBot(irc.bot.SingleServerIRCBot): def start(configpath): - config = configparser.RawConfigParser() - config.read(configpath) + with open(configpath, 'r') as fp: + config = json.load(fp) - if config.has_option('ircbot', 'log_config'): - log_config = config.get('ircbot', 'log_config') + if 'log_config' in config: + log_config = config['log_config'] fp = os.path.expanduser(log_config) if not os.path.exists(fp): raise Exception("Unable to read logging config file at %s" % fp) @@ -176,27 +175,23 @@ def start(configpath): else: logging.basicConfig(level=logging.DEBUG) - channels = ['#' + name.strip() for name in - config.get('ircbot', 'channels').split(',')] - db = ptgbot.db.PTGDataBase( - config.get('db', 'filename'), - config.get('db', 'ethercalc'), - config.get('db', 'cells')) + config['db_filename'], + config.get('ethercalc_url'), + config.get('ethercalc_cells')) - bot = PTGBot(config.get('ircbot', 'nick'), - config.get('ircbot', 'pass'), - config.get('ircbot', 'server'), - config.getint('ircbot', 'port'), - channels, + bot = PTGBot(config['irc_nick'], + config.get('irc_pass', ''), + config['irc_server'], + config['irc_port'], + config['irc_channel'], db) bot.start() def main(): parser = argparse.ArgumentParser(description='PTG bot.') - parser.add_argument('configfile', nargs=1, - help='specify the config file') + parser.add_argument('configfile', help='specify the config file') parser.add_argument('-d', dest='nodaemon', action='store_true', help='do not run as a daemon') args = parser.parse_args() diff --git a/ptgbot/db.py b/ptgbot/db.py index 957fec8..f5e15fa 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -26,7 +26,7 @@ class PTGDataBase(): def __init__(self, filename, ethercalc_url, ethercalc_cells): self.filename = filename self.ethercalc_url = ethercalc_url - self.ethercalc_cells = ethercalc_cells.split(' ') + self.ethercalc_cells = ethercalc_cells if os.path.isfile(filename): with open(filename, 'r') as fp: self.data = json.load(fp)