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
This commit is contained in:
parent
c12a4956be
commit
42451c744b
@ -136,6 +136,10 @@ You have to be a channel operator (+o) to use admin commands.
|
|||||||
~reload
|
~reload
|
||||||
Resets the database entirely (reloads from configuration)
|
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
|
~requirevoice
|
||||||
Requires that users are voiced (+v) to issue track moderation commands
|
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::
|
Join that channel and give commands to the bot::
|
||||||
|
|
||||||
~add swift
|
~fetchdb http://paste.openstack.org/raw/736003/
|
||||||
#swift now discussing ring placement
|
#swift now discussing ring placement
|
||||||
|
|
||||||
(note, the bot currently only takes commands from Freenode identified users)
|
(note, the bot currently only takes commands from Freenode identified users)
|
||||||
|
@ -4,40 +4,5 @@
|
|||||||
"irc_server": "irc.freenode.net",
|
"irc_server": "irc.freenode.net",
|
||||||
"irc_port": 6667,
|
"irc_port": 6667,
|
||||||
"irc_channel": "#CHANNEL",
|
"irc_channel": "#CHANNEL",
|
||||||
"db_filename": "html/ptg.json",
|
"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",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import irc.bot
|
|||||||
import json
|
import json
|
||||||
import logging.config
|
import logging.config
|
||||||
import os
|
import os
|
||||||
|
import requests
|
||||||
import time
|
import time
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
@ -154,6 +155,14 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot):
|
|||||||
command = words[0][1:].lower()
|
command = words[0][1:].lower()
|
||||||
if command == 'reload':
|
if command == 'reload':
|
||||||
self.data.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':
|
elif command == 'unbook':
|
||||||
params = str.join(' ', words[1:])
|
params = str.join(' ', words[1:])
|
||||||
room, sep, timeslot = params.partition('-')
|
room, sep, timeslot = params.partition('-')
|
||||||
|
16
ptgbot/db.py
16
ptgbot/db.py
@ -27,8 +27,6 @@ class PTGDataBase():
|
|||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.filename = config['db_filename']
|
self.filename = config['db_filename']
|
||||||
self.slots = config['slots']
|
|
||||||
self.schedule = config['schedule']
|
|
||||||
|
|
||||||
if os.path.isfile(self.filename):
|
if os.path.isfile(self.filename):
|
||||||
with open(self.filename, 'r') as fp:
|
with open(self.filename, 'r') as fp:
|
||||||
@ -36,21 +34,20 @@ class PTGDataBase():
|
|||||||
else:
|
else:
|
||||||
self.data = copy.deepcopy(self.BASE)
|
self.data = copy.deepcopy(self.BASE)
|
||||||
|
|
||||||
self.load_data_from_config()
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def load_data_from_config(self):
|
def import_json(self, jsondata):
|
||||||
# Copy slots definition and scheduled rooms from configuration
|
# Update the DB with the data found in the provided JSON
|
||||||
self.data['slots'] = self.slots
|
self.data.update(jsondata)
|
||||||
self.data['schedule'] = self.schedule
|
|
||||||
|
|
||||||
# Add tracks mentioned in configuration that are not in track list
|
# 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():
|
for time, track in bookings.items():
|
||||||
if track not in self.data['tracks']:
|
if track not in self.data['tracks']:
|
||||||
self.data['tracks'].append(track)
|
self.add_tracks([track])
|
||||||
|
|
||||||
self.colorize()
|
self.colorize()
|
||||||
|
self.save()
|
||||||
|
|
||||||
def add_now(self, track, session):
|
def add_now(self, track, session):
|
||||||
self.data['now'][track] = session
|
self.data['now'][track] = session
|
||||||
@ -170,7 +167,6 @@ class PTGDataBase():
|
|||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
self.data = copy.deepcopy(self.BASE)
|
self.data = copy.deepcopy(self.BASE)
|
||||||
self.load_data_from_config()
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user