Browse Source

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
changes/55/478955/1
Thierry Carrez 4 years ago
parent
commit
f5906ccfe5
3 changed files with 48 additions and 20 deletions
  1. +9
    -4
      ptgbot/bot.py
  2. +4
    -16
      ptgbot/db.py
  3. +35
    -0
      ptgbot/ethercalc.py

+ 9
- 4
ptgbot/bot.py View File

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


+ 4
- 16
ptgbot/db.py View File

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

+ 35
- 0
ptgbot/ethercalc.py View File

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

Loading…
Cancel
Save