From 2aa2e01bc5cc96633b2a7339c3f2696fe8689946 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 20 Feb 2010 14:31:53 +0100 Subject: [PATCH] support of backend capabilities --- weboob/__init__.py | 2 +- weboob/backend.py | 23 +++++++++++++++++++++++ weboob/backends/aum/__init__.py | 23 ++++++++++++++++++++++- weboob/backends/aum/backend.py | 25 +++++++++++++++++++++++++ weboob/backends/dlfp/__init__.py | 1 + weboob/backends/dlfp/backend.py | 25 +++++++++++++++++++++++++ weboob/capabilities.py | 21 +++++++++++++++++++++ weboob/modules.py | 25 ++++++++++++++++++++----- weboob/ouiboube.py | 7 +++++++ weboob2mail | 3 ++- 10 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 weboob/backend.py create mode 100644 weboob/backends/aum/backend.py create mode 100644 weboob/backends/dlfp/backend.py create mode 100644 weboob/capabilities.py diff --git a/weboob/__init__.py b/weboob/__init__.py index 9632336af3..19ab13f05d 100644 --- a/weboob/__init__.py +++ b/weboob/__init__.py @@ -18,4 +18,4 @@ """ -from ouiboube import Weboob +from .ouiboube import Weboob diff --git a/weboob/backend.py b/weboob/backend.py new file mode 100644 index 0000000000..18c5ad57ab --- /dev/null +++ b/weboob/backend.py @@ -0,0 +1,23 @@ + +# -*- 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 Backend: + CAPS = 0 diff --git a/weboob/backends/aum/__init__.py b/weboob/backends/aum/__init__.py index 399b414f53..e3309a9ede 100644 --- a/weboob/backends/aum/__init__.py +++ b/weboob/backends/aum/__init__.py @@ -1 +1,22 @@ -from adopte import AdopteUnMec +# -*- 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. + +""" + +from .adopte import AdopteUnMec +from .backend import AuMBackend diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py new file mode 100644 index 0000000000..b39e692c1a --- /dev/null +++ b/weboob/backends/aum/backend.py @@ -0,0 +1,25 @@ +# -*- 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. + +""" + +from weboob.backend import Backend +from weboob.capabilities import CAP_MAILS + +class AuMBackend(Backend): + CAPS = CAP_MAILS diff --git a/weboob/backends/dlfp/__init__.py b/weboob/backends/dlfp/__init__.py index 16b432bc20..e894520e46 100644 --- a/weboob/backends/dlfp/__init__.py +++ b/weboob/backends/dlfp/__init__.py @@ -19,3 +19,4 @@ """ from .browser import DLFP +from .backend import DLFPBackend diff --git a/weboob/backends/dlfp/backend.py b/weboob/backends/dlfp/backend.py new file mode 100644 index 0000000000..f1823dfd98 --- /dev/null +++ b/weboob/backends/dlfp/backend.py @@ -0,0 +1,25 @@ +# -*- 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. + +""" + +from weboob.backend import Backend +from weboob.capabilities import CAP_MAILS + +class DLFPBackend(Backend): + CAPS = CAP_MAILS diff --git a/weboob/capabilities.py b/weboob/capabilities.py new file mode 100644 index 0000000000..d4efe73877 --- /dev/null +++ b/weboob/capabilities.py @@ -0,0 +1,21 @@ +# -*- 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/modules.py b/weboob/modules.py index 8e2f53beba..c13f548138 100644 --- a/weboob/modules.py +++ b/weboob/modules.py @@ -22,13 +22,29 @@ import os import sys from logging import warning, debug +from types import ClassType import weboob.backends as backends +from backend import Backend -class Backend: +class Module: def __init__(self, name, module): self.name = name self.module = module + self.klass = None + for attrname in dir(self.module): + attr = getattr(self.module, attrname) + if isinstance(attr, ClassType) and issubclass(attr, Backend) and attr != Backend: + self.klass = attr + + if not self.klass: + raise ImportError("This is not a backend module (no Backend class found)") + + def hasCaps(self, caps): + return self.klass.CAPS & caps + + def createBackend(self): + return self.klass() class ModulesLoader: def __init__(self): @@ -44,10 +60,9 @@ def load(self): def load_module(self, name): try: - backend = Backend(name, __import__(name, fromlist=[name])) - except ImportError: - warning('Unable to import %s (%s)' % (name, path)) - raise + backend = Module(name, __import__(name, fromlist=[name])) + except ImportError, e: + warning('Unable to load module %s: %s' % (name, e)) return if name in self.modules: warning('Module "%s" is already loaded (%s)' % self.modules[name].module) diff --git a/weboob/ouiboube.py b/weboob/ouiboube.py index 82a89af32d..4bccd46497 100644 --- a/weboob/ouiboube.py +++ b/weboob/ouiboube.py @@ -34,3 +34,10 @@ def __init__(self, app_name, config_file=CONFIG_FILE): self.modules_loader = ModulesLoader() self.modules_loader.load() + 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() + self.backends[module.name] = backend + diff --git a/weboob2mail b/weboob2mail index 37141bf4f8..5f6fd8463a 100755 --- a/weboob2mail +++ b/weboob2mail @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import sys from weboob import Weboob +from weboob.capabilities import CAP_MAILS class User: def __init__(self, username, password, email): @@ -36,7 +37,7 @@ class Application: self.weboob = Weboob(self.APPNAME) def main(self, argv): - pass + self.weboob.loadmodules(CAP_MAILS) if __name__ == '__main__': app = Application() -- GitLab