From f5906ccfe53fbe266adaab17c39c4d757abb905c Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Thu, 29 Jun 2017 15:29:38 +0200 Subject: [PATCH] Refactor ethercalc-specific code out of db Move ethercalc-related code out of the DB module, before we start making it a lot more complex. Change-Id: Iacbbe14e8cff9b06887b9bf76dc1f186c38ae2d2 --- ptgbot/bot.py | 13 +++++++++---- ptgbot/db.py | 20 ++++---------------- ptgbot/ethercalc.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 ptgbot/ethercalc.py diff --git a/ptgbot/bot.py b/ptgbot/bot.py index 549760e..c460cbf 100644 --- a/ptgbot/bot.py +++ b/ptgbot/bot.py @@ -25,6 +25,7 @@ import time import ssl import ptgbot.db +import ptgbot.ethercalc try: import daemon.pidlockfile as pid_file_module @@ -175,10 +176,14 @@ def start(configpath): else: logging.basicConfig(level=logging.DEBUG) - db = ptgbot.db.PTGDataBase( - config['db_filename'], - config.get('ethercalc_url'), - config.get('ethercalc_cells')) + if 'ethercalc_url' in config: + ethercalc = ptgbot.ethercalc.Ethercalc( + config['ethercalc_url'], + config.get('ethercalc_cells')) + else: + ethercalc = None + + db = ptgbot.db.PTGDataBase(config['db_filename'], ethercalc) bot = PTGBot(config['irc_nick'], config.get('irc_pass', ''), diff --git a/ptgbot/db.py b/ptgbot/db.py index f5e15fa..e3387ee 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -16,17 +16,15 @@ import json import os -import requests class PTGDataBase(): BASE = {'rooms': [], 'ethercalc': [], 'now': {}, 'next': {}} - def __init__(self, filename, ethercalc_url, ethercalc_cells): + def __init__(self, filename, ethercalc): self.filename = filename - self.ethercalc_url = ethercalc_url - self.ethercalc_cells = ethercalc_cells + self.ethercalc = ethercalc if os.path.isfile(filename): with open(filename, 'r') as fp: self.data = json.load(fp) @@ -71,22 +69,12 @@ class PTGDataBase(): del self.data['next'][room] self.save() - def from_ethercalc(self): - if self.ethercalc_url: - ethercalc = requests.get(self.ethercalc_url).json() - self.data['ethercalc'] = [] - for cell in self.ethercalc_cells: - if ('comment' in ethercalc[cell] and - 'datavalue' in ethercalc[cell]): - msg = '%s: %s' % (ethercalc[cell]['comment'], - ethercalc[cell]['datavalue']) - self.data['ethercalc'].append(msg) - def wipe(self): self.data = self.BASE self.save() def save(self): - self.from_ethercalc() + if self.ethercalc: + self.data['ethercalc'] = self.ethercalc.load() with open(self.filename, 'w') as fp: json.dump(self.data, fp) diff --git a/ptgbot/ethercalc.py b/ptgbot/ethercalc.py new file mode 100644 index 0000000..a06197f --- /dev/null +++ b/ptgbot/ethercalc.py @@ -0,0 +1,35 @@ +#! /usr/bin/env python + +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests + + +class Ethercalc(): + + def __init__(self, url, cells_spec): + self.url = url + self.cells = cells_spec + + def load(self): + doc = requests.get(self.url).json() + ret = [] + for cell in self.cells: + if ('comment' in doc[cell] and + 'datavalue' in doc[cell]): + msg = '%s: %s' % (doc[cell]['comment'], + doc[cell]['datavalue']) + ret.append(msg) + return ret