Separate data loading from init
Separate the dataloading from the database class init, so that it can be reused to reload base data on the fly. Change-Id: I3c591c71e77fd04e3d4dcf4fcb8378eb66a7acfb
This commit is contained in:
parent
55956246a2
commit
cd089e90a5
|
@ -196,10 +196,7 @@ def start(configpath):
|
||||||
else:
|
else:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
db = ptgbot.db.PTGDataBase(config['db_filename'],
|
db = ptgbot.db.PTGDataBase(config)
|
||||||
config['slots'],
|
|
||||||
config['scheduled'],
|
|
||||||
config['extrarooms'])
|
|
||||||
|
|
||||||
bot = PTGBot(config['irc_nick'],
|
bot = PTGBot(config['irc_nick'],
|
||||||
config.get('irc_pass', ''),
|
config.get('irc_pass', ''),
|
||||||
|
|
49
ptgbot/db.py
49
ptgbot/db.py
|
@ -27,18 +27,36 @@ class PTGDataBase():
|
||||||
BASE = {'tracks': [], 'slots': {}, 'now': {}, 'next': {}, 'colors': {},
|
BASE = {'tracks': [], 'slots': {}, 'now': {}, 'next': {}, 'colors': {},
|
||||||
'location': {}, 'scheduled': {}, 'additional': {}}
|
'location': {}, 'scheduled': {}, 'additional': {}}
|
||||||
|
|
||||||
def __init__(self, filename, slots, scheduled, extrarooms):
|
def __init__(self, config):
|
||||||
self.filename = filename
|
self.filename = config['db_filename']
|
||||||
if os.path.isfile(filename):
|
self.slots = config['slots']
|
||||||
with open(filename, 'r') as fp:
|
self.scheduled = config['scheduled']
|
||||||
|
self.extrarooms = config['extrarooms']
|
||||||
|
|
||||||
|
if os.path.isfile(self.filename):
|
||||||
|
with open(self.filename, 'r') as fp:
|
||||||
self.data = json.load(fp)
|
self.data = json.load(fp)
|
||||||
else:
|
else:
|
||||||
self.data = self.BASE
|
self.data = self.BASE
|
||||||
self.data['slots'] = slots
|
|
||||||
|
|
||||||
self.data['scheduled'] = scheduled
|
old_data = self.data['additional'].copy()
|
||||||
|
self.load_data_from_config()
|
||||||
|
self.merge_additional(old_data)
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
def load_data_from_config(self):
|
||||||
|
# Copy slots definition and scheduled rooms from configuration
|
||||||
|
self.data['slots'] = self.slots
|
||||||
|
self.data['scheduled'] = self.scheduled
|
||||||
|
|
||||||
|
# Create additional rooms dictionary from extrarooms definition
|
||||||
|
for room in self.extrarooms.keys():
|
||||||
|
self.data['additional'][room] = {}
|
||||||
|
for slot in self.extrarooms[room]:
|
||||||
|
self.data['additional'][room][slot] = ''
|
||||||
|
|
||||||
# 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 scheduled.items():
|
for room, bookings in self.scheduled.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.data['tracks'].append(track)
|
||||||
|
@ -57,21 +75,14 @@ class PTGDataBase():
|
||||||
'#dc0d0e',
|
'#dc0d0e',
|
||||||
])
|
])
|
||||||
|
|
||||||
# Rebuild 'additional' with rooms and slots from configuration, but
|
def merge_additional(self, old_data):
|
||||||
# use saved data where the room/slot is preserved
|
# Populate 'additional' with saved data where appropriate
|
||||||
old_data = self.data['additional'].copy()
|
for room in self.data['additional'].keys():
|
||||||
self.data['additional'] = {}
|
for slot in self.data['additional'][room].keys():
|
||||||
|
|
||||||
for room in extrarooms.keys():
|
|
||||||
self.data['additional'][room] = {}
|
|
||||||
for slot in extrarooms[room]:
|
|
||||||
try:
|
try:
|
||||||
self.data['additional'][room][slot] = old_data[room][slot]
|
self.data['additional'][room][slot] = old_data[room][slot]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.data['additional'][room][slot] = ''
|
pass
|
||||||
|
|
||||||
# Save the data to disk
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
def add_now(self, track, session):
|
def add_now(self, track, session):
|
||||||
self.data['now'][track] = session
|
self.data['now'][track] = session
|
||||||
|
|
Loading…
Reference in New Issue