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
This commit is contained in:
Thierry Carrez 2017-06-29 15:07:57 +02:00
parent a434345db4
commit 39e17d012c
5 changed files with 41 additions and 51 deletions

View File

@ -34,27 +34,23 @@ Example::
Testing 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 "irc_nick": "ptgbot",
pass= "irc_server": "irc.freenode.net",
server=irc.freenode.net "irc_port": 6667,
port=6667 "irc_channel": "#testptg",
channels=testptg "db_filename": "html/ptg.json",
}
[db]
filename=html/ptg.json
ethercalc=
cells=
In one terminal, run the bot:: 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:: Join that channel and give a command to the bot::

View File

@ -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

10
config.json.sample Normal file
View File

@ -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"]
}

View File

@ -16,9 +16,9 @@
# limitations under the License. # limitations under the License.
import argparse import argparse
import configparser
import daemon import daemon
import irc.bot import irc.bot
import json
import logging.config import logging.config
import os import os
import time import time
@ -43,7 +43,7 @@ ANTI_FLOOD_SLEEP = 2
class PTGBot(irc.bot.SingleServerIRCBot): class PTGBot(irc.bot.SingleServerIRCBot):
log = logging.getLogger("ptgbot.bot") 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: if port == 6697:
factory = irc.connection.Factory(wrapper=ssl.wrap_socket) factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
irc.bot.SingleServerIRCBot.__init__(self, irc.bot.SingleServerIRCBot.__init__(self,
@ -56,7 +56,7 @@ class PTGBot(irc.bot.SingleServerIRCBot):
nickname, nickname) nickname, nickname)
self.nickname = nickname self.nickname = nickname
self.password = password self.password = password
self.channel_list = channels self.channel = channel
self.identify_msg_cap = False self.identify_msg_cap = False
self.data = db self.data = db
@ -77,10 +77,9 @@ class PTGBot(irc.bot.SingleServerIRCBot):
if (self.password): if (self.password):
self.log.debug("Identifying to nickserv") self.log.debug("Identifying to nickserv")
c.privmsg("nickserv", "identify %s " % self.password) c.privmsg("nickserv", "identify %s " % self.password)
for channel in self.channel_list: self.log.info("Joining %s" % self.channel)
self.log.info("Joining %s" % channel) c.join(self.channel)
c.join(channel) time.sleep(ANTI_FLOOD_SLEEP)
time.sleep(ANTI_FLOOD_SLEEP)
def on_cap(self, c, e): def on_cap(self, c, e):
self.log.debug("Received cap response %s" % repr(e.arguments)) self.log.debug("Received cap response %s" % repr(e.arguments))
@ -164,11 +163,11 @@ class PTGBot(irc.bot.SingleServerIRCBot):
def start(configpath): def start(configpath):
config = configparser.RawConfigParser() with open(configpath, 'r') as fp:
config.read(configpath) config = json.load(fp)
if config.has_option('ircbot', 'log_config'): if 'log_config' in config:
log_config = config.get('ircbot', 'log_config') log_config = config['log_config']
fp = os.path.expanduser(log_config) fp = os.path.expanduser(log_config)
if not os.path.exists(fp): if not os.path.exists(fp):
raise Exception("Unable to read logging config file at %s" % fp) raise Exception("Unable to read logging config file at %s" % fp)
@ -176,27 +175,23 @@ def start(configpath):
else: else:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
channels = ['#' + name.strip() for name in
config.get('ircbot', 'channels').split(',')]
db = ptgbot.db.PTGDataBase( db = ptgbot.db.PTGDataBase(
config.get('db', 'filename'), config['db_filename'],
config.get('db', 'ethercalc'), config.get('ethercalc_url'),
config.get('db', 'cells')) config.get('ethercalc_cells'))
bot = PTGBot(config.get('ircbot', 'nick'), bot = PTGBot(config['irc_nick'],
config.get('ircbot', 'pass'), config.get('irc_pass', ''),
config.get('ircbot', 'server'), config['irc_server'],
config.getint('ircbot', 'port'), config['irc_port'],
channels, config['irc_channel'],
db) db)
bot.start() bot.start()
def main(): def main():
parser = argparse.ArgumentParser(description='PTG bot.') parser = argparse.ArgumentParser(description='PTG bot.')
parser.add_argument('configfile', nargs=1, parser.add_argument('configfile', help='specify the config file')
help='specify the config file')
parser.add_argument('-d', dest='nodaemon', action='store_true', parser.add_argument('-d', dest='nodaemon', action='store_true',
help='do not run as a daemon') help='do not run as a daemon')
args = parser.parse_args() args = parser.parse_args()

View File

@ -26,7 +26,7 @@ class PTGDataBase():
def __init__(self, filename, ethercalc_url, ethercalc_cells): def __init__(self, filename, ethercalc_url, ethercalc_cells):
self.filename = filename self.filename = filename
self.ethercalc_url = ethercalc_url self.ethercalc_url = ethercalc_url
self.ethercalc_cells = ethercalc_cells.split(' ') self.ethercalc_cells = ethercalc_cells
if os.path.isfile(filename): if os.path.isfile(filename):
with open(filename, 'r') as fp: with open(filename, 'r') as fp:
self.data = json.load(fp) self.data = json.load(fp)