BackendStorage class to encapsulate storage for a specific backend
This commit is contained in:
parent
cad438214e
commit
33922ca89e
2 changed files with 33 additions and 11 deletions
|
|
@ -20,6 +20,29 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
import re
|
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):
|
class Backend(object):
|
||||||
# Module name.
|
# Module name.
|
||||||
NAME = None
|
NAME = None
|
||||||
|
|
@ -69,9 +92,8 @@ class Backend(object):
|
||||||
elif isinstance(field.default, float):
|
elif isinstance(field.default, float):
|
||||||
value = float(value)
|
value = float(value)
|
||||||
self.config[name] = value
|
self.config[name] = value
|
||||||
self.storage = storage
|
self.storage = BackendStorage(self.name, storage)
|
||||||
if self.storage:
|
self.storage.load(self.STORAGE)
|
||||||
self.storage.load(self.name, self.STORAGE)
|
|
||||||
|
|
||||||
def has_caps(self, *caps):
|
def has_caps(self, *caps):
|
||||||
for c in caps:
|
for c in caps:
|
||||||
|
|
|
||||||
|
|
@ -63,22 +63,22 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply):
|
||||||
for message in self._iter_messages_of('telegram', thread, only_new):
|
for message in self._iter_messages_of('telegram', thread, only_new):
|
||||||
yield message
|
yield message
|
||||||
|
|
||||||
def _iter_messages_of(self, what, thread, only_new):
|
def _iter_messages_of(self, what, thread_wanted, only_new):
|
||||||
if not what in self.storage.get(self.name, 'seen'):
|
if not what in self.storage.get('seen'):
|
||||||
self.storage.set(self.name, 'seen', what, {})
|
self.storage.set('seen', what, {})
|
||||||
|
|
||||||
seen = {}
|
seen = {}
|
||||||
for article in ArticlesList(what).iter_articles():
|
for article in ArticlesList(what).iter_articles():
|
||||||
if thread and thread != article.id:
|
if thread_wanted and thread_wanted != article.id:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
thread = self.browser.get_content(article.id)
|
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': []}
|
seen[article.id] = {'comments': []}
|
||||||
new = True
|
new = True
|
||||||
else:
|
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
|
new = False
|
||||||
if not only_new or new:
|
if not only_new or new:
|
||||||
yield Message(thread.id,
|
yield Message(thread.id,
|
||||||
|
|
@ -104,8 +104,8 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply):
|
||||||
comment.reply_id,
|
comment.reply_id,
|
||||||
comment.body,
|
comment.body,
|
||||||
'Score: %d' % comment.score)
|
'Score: %d' % comment.score)
|
||||||
self.storage.set(self.name, 'seen', what, seen)
|
self.storage.set('seen', what, seen)
|
||||||
self.storage.save(self.name)
|
self.storage.save()
|
||||||
|
|
||||||
def post_reply(self, thread_id, reply_id, title, message):
|
def post_reply(self, thread_id, reply_id, title, message):
|
||||||
return self.browser.post(thread_id, reply_id, title, message)
|
return self.browser.post(thread_id, reply_id, title, message)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue