From cfc93cb8d86d658902d481361461547be1009545 Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Mon, 15 Apr 2019 16:29:57 +0200 Subject: [PATCH] Preserve JSON dictionary order Load JSON db into ordered dictionaries so that the order of days is preserved. Change-Id: Icef5daf95271b0e8f1da61ee20eee1d4d3cd2c79 --- ptgbot/db.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ptgbot/db.py b/ptgbot/db.py index 46a62ab..eea8853 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -13,6 +13,7 @@ # limitations under the License. import calendar +from collections import OrderedDict import copy import datetime import json @@ -22,17 +23,23 @@ import random class PTGDataBase(): - BASE = {'tracks': [], 'slots': {}, 'now': {}, 'next': {}, 'colors': {}, - 'location': {}, 'schedule': {}, 'voice': 0, + BASE = {'tracks': [], + 'slots': OrderedDict(), + 'now': OrderedDict(), + 'next': OrderedDict(), + 'colors': OrderedDict(), + 'location': OrderedDict(), + 'schedule': OrderedDict(), + 'voice': 0, 'motd': {'message': '', 'level': 'info'}, - 'links': {}} + 'links': OrderedDict()} def __init__(self, config): self.filename = config['db_filename'] if os.path.isfile(self.filename): with open(self.filename, 'r') as fp: - self.data = json.load(fp) + self.data = json.load(fp, object_pairs_hook=OrderedDict) else: self.data = copy.deepcopy(self.BASE)