BackendStorage class to encapsulate storage for a specific backend

This commit is contained in:
Romain Bignon 2010-04-11 18:05:37 +02:00
commit 33922ca89e
2 changed files with 33 additions and 11 deletions

View file

@ -20,6 +20,29 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re
class BackendStorage(object):
def __init__(self, name, storage):
self.name = name
self.storage = storage
def set(self, *args):
if self.storage:
return self.storage.set(self.name, *args)
def get(self, *args, **kwargs):
if self.storage:
return self.storage.get(self.name, *args, **kwargs)
else:
return kwargs.get('default', None)
def load(self, default):
if self.storage:
return self.storage.load(self.name, default)
def save(self):
if self.storage:
return self.storage.save(self.name)
class Backend(object):
# Module name.
NAME = None
@ -69,9 +92,8 @@ class Backend(object):
elif isinstance(field.default, float):
value = float(value)
self.config[name] = value
self.storage = storage
if self.storage:
self.storage.load(self.name, self.STORAGE)
self.storage = BackendStorage(self.name, storage)
self.storage.load(self.STORAGE)
def has_caps(self, *caps):
for c in caps:

View file

@ -63,22 +63,22 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply):
for message in self._iter_messages_of('telegram', thread, only_new):
yield message
def _iter_messages_of(self, what, thread, only_new):
if not what in self.storage.get(self.name, 'seen'):
self.storage.set(self.name, 'seen', what, {})
def _iter_messages_of(self, what, thread_wanted, only_new):
if not what in self.storage.get('seen'):
self.storage.set('seen', what, {})
seen = {}
for article in ArticlesList(what).iter_articles():
if thread and thread != article.id:
if thread_wanted and thread_wanted != article.id:
continue
thread = self.browser.get_content(article.id)
if not article.id in self.storage.get(self.name, 'seen', what):
if not article.id in self.storage.get('seen', what):
seen[article.id] = {'comments': []}
new = True
else:
seen[article.id] = self.storage.get(self.name, 'seen', what, article.id)
seen[article.id] = self.storage.get('seen', what, article.id)
new = False
if not only_new or new:
yield Message(thread.id,
@ -104,8 +104,8 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply):
comment.reply_id,
comment.body,
'Score: %d' % comment.score)
self.storage.set(self.name, 'seen', what, seen)
self.storage.save(self.name)
self.storage.set('seen', what, seen)
self.storage.save()
def post_reply(self, thread_id, reply_id, title, message):
return self.browser.post(thread_id, reply_id, title, message)