From f06ef5da3415ff9007b93b24422a7ad4756c0b0b Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Tue, 23 Feb 2010 20:38:24 +0100 Subject: [PATCH] better capabilities management (class interfaces instead of constants) --- weboob/backend.py | 12 +++++++++++- weboob/backends/aum/backend.py | 6 +++--- weboob/backends/dlfp/backend.py | 6 +++--- weboob/capabilities.py | 21 --------------------- weboob/capabilities/__init__.py | 0 weboob/modules.py | 15 ++++++++++----- weboob/ouiboube.py | 15 ++++++++++++++- 7 files changed, 41 insertions(+), 34 deletions(-) delete mode 100644 weboob/capabilities.py create mode 100644 weboob/capabilities/__init__.py diff --git a/weboob/backend.py b/weboob/backend.py index 18c5ad57ab..858f39cde5 100644 --- a/weboob/backend.py +++ b/weboob/backend.py @@ -20,4 +20,14 @@ """ class Backend: - CAPS = 0 + def __init__(self, config): + self.config = config + + def hasCaps(self, caps): + if not isinstance(caps, (list,tuple)): + caps = (caps,) + + for c in caps: + if isinstance(self, c): + return True + return False diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index b39e692c1a..2904a0b510 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -19,7 +19,7 @@ """ from weboob.backend import Backend -from weboob.capabilities import CAP_MAILS +from weboob.capabilities.messages import IMessages, IMessagesReply -class AuMBackend(Backend): - CAPS = CAP_MAILS +class AuMBackend(Backend, IMessages, IMessagesReply): + pass diff --git a/weboob/backends/dlfp/backend.py b/weboob/backends/dlfp/backend.py index f1823dfd98..c9e5493f40 100644 --- a/weboob/backends/dlfp/backend.py +++ b/weboob/backends/dlfp/backend.py @@ -19,7 +19,7 @@ """ from weboob.backend import Backend -from weboob.capabilities import CAP_MAILS +from weboob.capabilities.messages import IMessages, IMessagesReply -class DLFPBackend(Backend): - CAPS = CAP_MAILS +class DLFPBackend(Backend, IMessages, IMessagesReply): + pass diff --git a/weboob/capabilities.py b/weboob/capabilities.py deleted file mode 100644 index d4efe73877..0000000000 --- a/weboob/capabilities.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- 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. - -""" - -CAP_MAILS = 0x00001 diff --git a/weboob/capabilities/__init__.py b/weboob/capabilities/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/weboob/modules.py b/weboob/modules.py index c13f548138..f39d5dd249 100644 --- a/weboob/modules.py +++ b/weboob/modules.py @@ -20,12 +20,11 @@ import re import os -import sys from logging import warning, debug from types import ClassType import weboob.backends as backends -from backend import Backend +from weboob.backend import Backend class Module: def __init__(self, name, module): @@ -41,10 +40,16 @@ def __init__(self, name, module): raise ImportError("This is not a backend module (no Backend class found)") def hasCaps(self, caps): - return self.klass.CAPS & caps + if not isinstance(caps, (list,tuple)): + caps = (caps,) - def createBackend(self): - return self.klass() + for c in caps: + if issubclass(self.klass, c): + return True + return False + + def createBackend(self, config): + return self.klass(config) class ModulesLoader: def __init__(self): diff --git a/weboob/ouiboube.py b/weboob/ouiboube.py index 4bccd46497..5f4184e47a 100644 --- a/weboob/ouiboube.py +++ b/weboob/ouiboube.py @@ -38,6 +38,19 @@ def loadmodules(self, caps=None, name=None): for name, module in self.modules_loader.modules.iteritems(): if (not caps or module.hasCaps(caps)) and \ (not name or module.name == name): - backend = module.createBackend() + backend = module.createBackend(self.config.get('backends', module.name)) self.backends[module.name] = backend + def loadmodule(self, modname, instname): + module = self.modules_loader[modname] + self.backends[instname] = module.createBackend(self.config.get('backends', instname)) + + def getBackends(self, caps=None): + if caps is None: + return self.backends + + d = {} + for name, backend in self.backends.iteritems(): + if backend.hasCaps(caps): + d[name] = backend + return d -- GitLab