better capabilities management (class interfaces instead of constants)

This commit is contained in:
Romain Bignon 2010-02-23 20:38:24 +01:00
commit f06ef5da34
7 changed files with 41 additions and 34 deletions

View file

@ -20,4 +20,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
class Backend: 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

View file

@ -19,7 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from weboob.backend import Backend from weboob.backend import Backend
from weboob.capabilities import CAP_MAILS from weboob.capabilities.messages import IMessages, IMessagesReply
class AuMBackend(Backend): class AuMBackend(Backend, IMessages, IMessagesReply):
CAPS = CAP_MAILS pass

View file

@ -19,7 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
""" """
from weboob.backend import Backend from weboob.backend import Backend
from weboob.capabilities import CAP_MAILS from weboob.capabilities.messages import IMessages, IMessagesReply
class DLFPBackend(Backend): class DLFPBackend(Backend, IMessages, IMessagesReply):
CAPS = CAP_MAILS pass

View file

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

View file

View file

@ -20,12 +20,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re import re
import os import os
import sys
from logging import warning, debug from logging import warning, debug
from types import ClassType from types import ClassType
import weboob.backends as backends import weboob.backends as backends
from backend import Backend from weboob.backend import Backend
class Module: class Module:
def __init__(self, name, module): def __init__(self, name, module):
@ -41,10 +40,16 @@ class Module:
raise ImportError("This is not a backend module (no Backend class found)") raise ImportError("This is not a backend module (no Backend class found)")
def hasCaps(self, caps): def hasCaps(self, caps):
return self.klass.CAPS & caps if not isinstance(caps, (list,tuple)):
caps = (caps,)
def createBackend(self): for c in caps:
return self.klass() if issubclass(self.klass, c):
return True
return False
def createBackend(self, config):
return self.klass(config)
class ModulesLoader: class ModulesLoader:
def __init__(self): def __init__(self):

View file

@ -38,6 +38,19 @@ class Weboob:
for name, module in self.modules_loader.modules.iteritems(): for name, module in self.modules_loader.modules.iteritems():
if (not caps or module.hasCaps(caps)) and \ if (not caps or module.hasCaps(caps)) and \
(not name or module.name == name): (not name or module.name == name):
backend = module.createBackend() backend = module.createBackend(self.config.get('backends', module.name))
self.backends[module.name] = backend 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