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
=======
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::

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.
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()

View File

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