support of backend capabilities

This commit is contained in:
Romain Bignon 2010-02-20 14:31:53 +01:00
commit 2aa2e01bc5
10 changed files with 147 additions and 8 deletions

View file

@ -18,4 +18,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from ouiboube import Weboob from .ouiboube import Weboob

23
weboob/backend.py Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -19,3 +19,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from .browser import DLFP from .browser import DLFP
from .backend import DLFPBackend

View file

@ -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

21
weboob/capabilities.py Normal file
View file

@ -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

View file

@ -22,13 +22,29 @@ import re
import os import os
import sys import sys
from logging import warning, debug from logging import warning, debug
from types import ClassType
import weboob.backends as backends import weboob.backends as backends
from backend import Backend
class Backend: class Module:
def __init__(self, name, module): def __init__(self, name, module):
self.name = name self.name = name
self.module = module 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: class ModulesLoader:
def __init__(self): def __init__(self):
@ -44,10 +60,9 @@ class ModulesLoader:
def load_module(self, name): def load_module(self, name):
try: try:
backend = Backend(name, __import__(name, fromlist=[name])) backend = Module(name, __import__(name, fromlist=[name]))
except ImportError: except ImportError, e:
warning('Unable to import %s (%s)' % (name, path)) warning('Unable to load module %s: %s' % (name, e))
raise
return return
if name in self.modules: if name in self.modules:
warning('Module "%s" is already loaded (%s)' % self.modules[name].module) warning('Module "%s" is already loaded (%s)' % self.modules[name].module)

View file

@ -34,3 +34,10 @@ class Weboob:
self.modules_loader = ModulesLoader() self.modules_loader = ModulesLoader()
self.modules_loader.load() 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

View file

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys import sys
from weboob import Weboob from weboob import Weboob
from weboob.capabilities import CAP_MAILS
class User: class User:
def __init__(self, username, password, email): def __init__(self, username, password, email):
@ -36,7 +37,7 @@ class Application:
self.weboob = Weboob(self.APPNAME) self.weboob = Weboob(self.APPNAME)
def main(self, argv): def main(self, argv):
pass self.weboob.loadmodules(CAP_MAILS)
if __name__ == '__main__': if __name__ == '__main__':
app = Application() app = Application()