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
This commit is contained in:
parent
39e17d012c
commit
f5906ccfe5
|
@ -25,6 +25,7 @@ import time
|
||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
import ptgbot.db
|
import ptgbot.db
|
||||||
|
import ptgbot.ethercalc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import daemon.pidlockfile as pid_file_module
|
import daemon.pidlockfile as pid_file_module
|
||||||
|
@ -175,10 +176,14 @@ def start(configpath):
|
||||||
else:
|
else:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
db = ptgbot.db.PTGDataBase(
|
if 'ethercalc_url' in config:
|
||||||
config['db_filename'],
|
ethercalc = ptgbot.ethercalc.Ethercalc(
|
||||||
config.get('ethercalc_url'),
|
config['ethercalc_url'],
|
||||||
config.get('ethercalc_cells'))
|
config.get('ethercalc_cells'))
|
||||||
|
else:
|
||||||
|
ethercalc = None
|
||||||
|
|
||||||
|
db = ptgbot.db.PTGDataBase(config['db_filename'], ethercalc)
|
||||||
|
|
||||||
bot = PTGBot(config['irc_nick'],
|
bot = PTGBot(config['irc_nick'],
|
||||||
config.get('irc_pass', ''),
|
config.get('irc_pass', ''),
|
||||||
|
|
20
ptgbot/db.py
20
ptgbot/db.py
|
@ -16,17 +16,15 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import requests
|
|
||||||
|
|
||||||
|
|
||||||
class PTGDataBase():
|
class PTGDataBase():
|
||||||
|
|
||||||
BASE = {'rooms': [], 'ethercalc': [], 'now': {}, 'next': {}}
|
BASE = {'rooms': [], 'ethercalc': [], 'now': {}, 'next': {}}
|
||||||
|
|
||||||
def __init__(self, filename, ethercalc_url, ethercalc_cells):
|
def __init__(self, filename, ethercalc):
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.ethercalc_url = ethercalc_url
|
self.ethercalc = ethercalc
|
||||||
self.ethercalc_cells = ethercalc_cells
|
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
with open(filename, 'r') as fp:
|
with open(filename, 'r') as fp:
|
||||||
self.data = json.load(fp)
|
self.data = json.load(fp)
|
||||||
|
@ -71,22 +69,12 @@ class PTGDataBase():
|
||||||
del self.data['next'][room]
|
del self.data['next'][room]
|
||||||
self.save()
|
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):
|
def wipe(self):
|
||||||
self.data = self.BASE
|
self.data = self.BASE
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.from_ethercalc()
|
if self.ethercalc:
|
||||||
|
self.data['ethercalc'] = self.ethercalc.load()
|
||||||
with open(self.filename, 'w') as fp:
|
with open(self.filename, 'w') as fp:
|
||||||
json.dump(self.data, fp)
|
json.dump(self.data, fp)
|
||||||
|
|
|
@ -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…
Reference in New Issue