Source code for the PTG event scheduling bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.3 KiB

# Copyright (c) 2017, Thierry Carrez
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
import calendar
from collections import OrderedDict
import copy
import datetime
import json
import os
import random
class PTGDataBase():
BASE = {'tracks': [],
'slots': OrderedDict(),
'now': OrderedDict(),
'next': OrderedDict(),
'colors': OrderedDict(),
'location': OrderedDict(),
'schedule': OrderedDict(),
'voice': 0,
'motd': {'message': '', 'level': 'info'},
'links': OrderedDict()}
def __init__(self, config):
self.filename = config['db_filename']
if os.path.isfile(self.filename):
with open(self.filename, 'r') as fp: = json.load(fp, object_pairs_hook=OrderedDict)
else: = copy.deepcopy(self.BASE)
def import_json(self, jsondata):
# Update the DB with the data found in the provided JSON
# Add tracks mentioned in configuration that are not in track list
for room, bookings in['schedule'].items():
for time, track in bookings.items():
if track not in['tracks']:
def add_now(self, track, session):['now'][track] = session
# Update location if none manually provided yet
room = self.get_track_room(track)
if room and track not in['location']:
self.add_location(track, room)
if track in['next']:
def add_color(self, track, color):['colors'][track] = color
def colorize(self):
for track in['tracks']:
if track not in['colors']:
self.add_color(track, random.choice([
def get_track_room(self, track):
# This simplified version returns the first room the track is
# scheduled in for the day. If the event does not run on this
# day, pick the first day.
today = calendar.day_name[]
if today not in['slots']:
today = next(iter(['slots']))
for room, bookings in['schedule'].items():
for btime, btrack in bookings.items():
for slot in['slots'].get(today, []):
if btrack == track and btime == slot['name']:
return room
return None
def add_location(self, track, location):['location'][track] = location
def add_next(self, track, session):
if track not in['next']:['next'][track] = []['next'][track].append(session)
def is_track_valid(self, track):
return track in['tracks']
def list_tracks(self):
return sorted(['tracks'])
def add_tracks(self, tracks):
for track in tracks:
if track not in['tracks']:['tracks'].append(track)
def del_tracks(self, tracks):
for track in tracks:
if track in['tracks']:['tracks'].remove(track)
def clean_tracks(self, tracks):
for track in tracks:
if track in['now']:
if track in['next']:
def is_slot_valid_and_empty(self, room, timeslot):
return not['schedule'][room][timeslot]
except KeyError:
return False
def is_slot_booked_for_track(self, track, room, timeslot):
return['schedule'][room][timeslot] == track
except KeyError:
return False
def book(self, track, room, timeslot):['schedule'][room][timeslot] = track
def unbook(self, room, timeslot):
if room in['schedule'].keys():
if timeslot in['schedule'][room].keys():['schedule'][room][timeslot] = ""
def is_voice_required(self):
return['voice'] == 1
def require_voice(self):['voice'] = 1
def allow_everyone(self):['voice'] = 0
def new_day_cleanup(self):['now'] = {}['next'] = {}['location'] = {}
def empty(self): = copy.deepcopy(self.BASE)
def motd(self, level, message):
if level in ['info', 'success', 'warning', 'danger']:['motd'] = {'level': level, 'message': message}
def clean_motd(self):['motd'] = {'level': '', 'message': ''}
def save(self):
timestamp =['timestamp'] = '{:%Y-%m-%d %H:%M:%S}'.format(timestamp)['tracks'] = sorted(['tracks'])
with open(self.filename, 'w') as fp:
json.dump(, fp)