From b08db0035e76cdcaecb8a9811566bb828044a215 Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Fri, 26 Mar 2010 20:14:10 +0100 Subject: [PATCH] WIP: updatable --- weboob/backends/dlfp/backend.py | 5 +-- weboob/capabilities/messages.py | 5 ++- weboob/capabilities/updatable.py | 46 ++++++++++++++++++++++++++++ weboob/frontends/dummy/scripts/dummy | 2 +- 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 weboob/capabilities/updatable.py diff --git a/weboob/backends/dlfp/backend.py b/weboob/backends/dlfp/backend.py index 97097146..f39223bb 100644 --- a/weboob/backends/dlfp/backend.py +++ b/weboob/backends/dlfp/backend.py @@ -20,9 +20,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from weboob.backend import Backend from weboob.capabilities.messages import ICapMessages, ICapMessagesReply, Message +from weboob.capabilities.updatable import ICapUpdatable from feeds import ArticlesList -class DLFPBackend(Backend, ICapMessages, ICapMessagesReply): +class DLFPBackend(Backend, ICapMessages, ICapMessagesReply, ICapUpdatable): MAINTAINER = 'Romain Bignon' EMAIL = 'romain@peerfuse.org' VERSION = '1.0' @@ -30,7 +31,7 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply): def __init__(self, weboob): Backend.__init__(self, weboob) - def iter_new_messages(self, thread=None): + def iter_messages(self): articles_list = ArticlesList('newspaper') for article in articles_list.iter_articles(): yield Message('threadid', article._id, article.title, article.author, signature='Bite bite bite bite', diff --git a/weboob/capabilities/messages.py b/weboob/capabilities/messages.py index aad1a40b..f48f06cb 100644 --- a/weboob/capabilities/messages.py +++ b/weboob/capabilities/messages.py @@ -78,12 +78,11 @@ class Message: return result.encode('utf-8') class ICapMessages: - def iter_new_messages(self, thread=None): + def iter_messages(self): """ Iterates on new messages from last time this function has been called. - @param thread [str] if given, get new messages for a specific thread. - @return [list] a list of Message objects. + @return [list] Message objects """ raise NotImplementedError() diff --git a/weboob/capabilities/updatable.py b/weboob/capabilities/updatable.py new file mode 100644 index 00000000..b0ef785d --- /dev/null +++ b/weboob/capabilities/updatable.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +""" +Copyright(C) 2010 Romain Bignon + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" + + +class ICapUpdatable: + snapshots = {} + + def take_snapshot(self, name, collection): + if name not in self.snapshots: + self.snapshots[name] = dict(original=set(collection)) + elif 'original' in self.snapshots[name]: + if 'updated' in self.snapshots[name]: + self.snapshots[name]['original'] = self.snapshots[name]['updated'] + self.snapshots[name]['updated'] = set(collection) + + def iter_new_items(self, name): + """ + Iterates on new items from last time this function has been called. + + @param name [str] name of the collection to iter + @return [iter] new items + """ + if name not in self.snapshots: + raise ValueError('"%s" has not been snapshot previously' % name) + elif 'original' not in self.snapshots[name] or 'updated' not in self.snapshots[name]: + raise ValueError('At least two snapshots are required to detect new items') + diff = self.snapshots[name]['updated'] - self.snapshots[name]['original'] + for item in diff: + yield item diff --git a/weboob/frontends/dummy/scripts/dummy b/weboob/frontends/dummy/scripts/dummy index 4d3a4a95..8e0af547 100755 --- a/weboob/frontends/dummy/scripts/dummy +++ b/weboob/frontends/dummy/scripts/dummy @@ -35,7 +35,7 @@ class Application(BaseApplication): print '= Processing backend name = %s' % name if backend.has_caps(ICapMessages): print '== Backend is ICapMessages => print its messages' - for message in backend.iter_new_messages(): + for message in backend.iter_messages(): print '=== %s' % message if backend.has_caps(ICapMessagesReply): print '== Backend is ICapMessagesReply => TODO'