diff --git a/weboob/tools/capabilities/bill/__init__.py b/weboob/tools/capabilities/bill/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/weboob/tools/capabilities/bill/documents.py b/weboob/tools/capabilities/bill/documents.py new file mode 100644 index 0000000000000000000000000000000000000000..25eb2439998397741ee43ae72cf252c51797376b --- /dev/null +++ b/weboob/tools/capabilities/bill/documents.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2020 Budget Insight +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with weboob. If not, see . + +from collections import OrderedDict + + +__all__ = ['sorted_documents', 'merge_iterators'] + + +def sorted_documents(iterable): + """Sort an iterable of documents in reverse chronological order""" + return sorted(iterable, reverse=True, key=lambda doc: doc.date) + + +def merge_iterators(*iterables): + """Merge documents iterators keeping sort order. + + Each iterator must already be sorted in reverse chronological order. + """ + + def keyfunc(kv): + return kv[1].date + + its = OrderedDict((iter(it), None) for it in iterables) + for k in list(its): + try: + its[k] = next(k) + except StopIteration: + del its[k] + + while its: + k, v = max(its.items(), key=keyfunc) + yield v + + try: + its[k] = next(k) + except StopIteration: + del its[k]