From 42451c744b0af0c23f6315b873e0d4de30f74bda Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Mon, 26 Nov 2018 13:38:26 +0100 Subject: [PATCH] Load base schedule dynamically The base schedule was specified in the bot configuration file, which made it a bit painful to do last-minute configuration. The bot shall load a basic minimal database and support being configured online instead, using commands and json imports. This change removes the schedule loading from configuration, and replaces it with a function to update the database using a provided JSON URL (the ~fetchdb admin command). Change-Id: I4e7a79baf88cce8d827eb66b1b0c26584c62c8d7 --- README.rst | 6 +++++- config.json.sample | 37 +------------------------------------ ptgbot/bot.py | 9 +++++++++ ptgbot/db.py | 16 ++++++---------- 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/README.rst b/README.rst index 1977189..2c6dcef 100644 --- a/README.rst +++ b/README.rst @@ -136,6 +136,10 @@ You have to be a channel operator (+o) to use admin commands. ~reload Resets the database entirely (reloads from configuration) +~fetchdb URL + Fetches JSON DB from specified URL. Any JSON key specified will replace + existing data in database. + ~requirevoice Requires that users are voiced (+v) to issue track moderation commands @@ -166,7 +170,7 @@ In one terminal, run the bot:: Join that channel and give commands to the bot:: - ~add swift + ~fetchdb http://paste.openstack.org/raw/736003/ #swift now discussing ring placement (note, the bot currently only takes commands from Freenode identified users) diff --git a/config.json.sample b/config.json.sample index 6467f56..bc7a17b 100644 --- a/config.json.sample +++ b/config.json.sample @@ -4,40 +4,5 @@ "irc_server": "irc.freenode.net", "irc_port": 6667, "irc_channel": "#CHANNEL", - "db_filename": "html/ptg.json", - "slots": { - "Monday": [ - { - "name": "MonAM", - "desc": "09:00-12:00" - }, - { - "name": "MonPM", - "desc": "14:00-17:00" - } - ], - "Tuesday": [ - { - "name": "TueAM", - "desc": "09:00-12:00" - }, - { - "name": "TuePM", - "desc": "14:00-17:00" - } - ] - }, - "schedule": { - "Ontario": { - "MonAM": "swift", - "MonPM": "", - "TueAM": "swift", - "TuePM": "swift" - }, - "Montana": { - "MonAM": "cinder", - "MonPM": "cinder", - "TueAM": "cinder", - } - } + "db_filename": "html/ptg.json" } diff --git a/ptgbot/bot.py b/ptgbot/bot.py index 123a911..9d75a3d 100644 --- a/ptgbot/bot.py +++ b/ptgbot/bot.py @@ -22,6 +22,7 @@ import irc.bot import json import logging.config import os +import requests import time import textwrap @@ -154,6 +155,14 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot): command = words[0][1:].lower() if command == 'reload': self.data.reload() + elif command == 'fetchdb': + url = words[1] + self.send(chan, "Loading DB from %s ..." % url) + try: + self.data.import_json(requests.get(url).json()) + self.send(chan, "Done.") + except Exception as e: + self.send(chan, "Error loading DB: %s" % e) elif command == 'unbook': params = str.join(' ', words[1:]) room, sep, timeslot = params.partition('-') diff --git a/ptgbot/db.py b/ptgbot/db.py index aa2fb09..e57706f 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -27,8 +27,6 @@ class PTGDataBase(): def __init__(self, config): self.filename = config['db_filename'] - self.slots = config['slots'] - self.schedule = config['schedule'] if os.path.isfile(self.filename): with open(self.filename, 'r') as fp: @@ -36,21 +34,20 @@ class PTGDataBase(): else: self.data = copy.deepcopy(self.BASE) - self.load_data_from_config() self.save() - def load_data_from_config(self): - # Copy slots definition and scheduled rooms from configuration - self.data['slots'] = self.slots - self.data['schedule'] = self.schedule + def import_json(self, jsondata): + # Update the DB with the data found in the provided JSON + self.data.update(jsondata) # Add tracks mentioned in configuration that are not in track list - for room, bookings in self.schedule.items(): + for room, bookings in self.data['schedule'].items(): for time, track in bookings.items(): if track not in self.data['tracks']: - self.data['tracks'].append(track) + self.add_tracks([track]) self.colorize() + self.save() def add_now(self, track, session): self.data['now'][track] = session @@ -170,7 +167,6 @@ class PTGDataBase(): def reload(self): self.data = copy.deepcopy(self.BASE) - self.load_data_from_config() self.save() def save(self):