Browse Source

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
changes/08/607308/3
Thierry Carrez 1 year ago
parent
commit
42451c744b
4 changed files with 21 additions and 47 deletions
  1. +5
    -1
      README.rst
  2. +1
    -36
      config.json.sample
  3. +9
    -0
      ptgbot/bot.py
  4. +6
    -10
      ptgbot/db.py

+ 5
- 1
README.rst View File

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


+ 1
- 36
config.json.sample View File

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

+ 9
- 0
ptgbot/bot.py View File

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


+ 6
- 10
ptgbot/db.py View File

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


Loading…
Cancel
Save