From c83c923e3d0c46c99c26ec1e02dd326ba055e9c8 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 20 Nov 2010 20:42:50 +0100 Subject: [PATCH] moved ICapDating.get_status() and ICapMessagesPost.get_status() into ICapAccount.get_account_status() --- weboob/applications/qhavesex/status.py | 42 +++++++++++++++-------- weboob/backends/aum/backend.py | 27 ++++++++------- weboob/backends/sfr/backend.py | 10 +++--- weboob/capabilities/account.py | 23 ++++++++++++- weboob/capabilities/dating.py | 17 --------- weboob/capabilities/messages.py | 13 ------- weboob/tools/application/qt/backendcfg.py | 2 +- weboob/tools/application/repl.py | 2 +- 8 files changed, 72 insertions(+), 64 deletions(-) diff --git a/weboob/applications/qhavesex/status.py b/weboob/applications/qhavesex/status.py index 4ed2821d..1f9f5235 100644 --- a/weboob/applications/qhavesex/status.py +++ b/weboob/applications/qhavesex/status.py @@ -16,9 +16,9 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -from PyQt4.QtGui import QWidget, QHBoxLayout, QVBoxLayout, QFrame, QLabel, QImage, QPixmap +from PyQt4.QtGui import QScrollArea, QWidget, QHBoxLayout, QVBoxLayout, QFrame, QLabel, QImage, QPixmap -from weboob.capabilities.dating import StatusField +from weboob.capabilities.account import ICapAccount, StatusField from weboob.tools.application.qt import QtDo class Account(QFrame): @@ -31,12 +31,14 @@ class Account(QFrame): self.weboob = weboob self.backend = backend self.setLayout(QVBoxLayout()) + self.timer = None head = QHBoxLayout() headw = QWidget() headw.setLayout(head) self.title = QLabel(u'

%s — %s

' % (backend.name, backend.DESCRIPTION)) + self.body = QLabel() if backend.ICON: self.icon = QLabel() @@ -47,18 +49,23 @@ class Account(QFrame): head.addWidget(self.title) head.addStretch() - self.body = QLabel(u'Waiting...') - self.layout().addWidget(headw) - self.layout().addWidget(self.body) - self.weboob.repeat(60, self.updateStats) + if backend.has_caps(ICapAccount): + self.body.setText(u'Waiting...') + self.layout().addWidget(self.body) + + self.timer = self.weboob.repeat(60, self.updateStats) + + def deinit(self): + if self.timer is not None: + self.weboob.stop(self.timer) def updateStats(self): self.process = QtDo(self.weboob, self.updateStats_cb, self.updateStats_eb) self.process.body = u'' self.process.in_p = False - self.process.do('get_status', backends=self.backend) + self.process.do('get_account_status', backends=self.backend) def updateStats_cb(self, backend, field): if not field: @@ -93,23 +100,30 @@ class Account(QFrame): self.body.setText(u'Unable to connect: %s' % unicode(err)) self.title.setText(u'%s' % unicode(self.title.text())) -class AccountsStatus(QWidget): +class AccountsStatus(QScrollArea): def __init__(self, weboob, parent=None): - QWidget.__init__(self, parent) + QScrollArea.__init__(self, parent) self.weboob = weboob - self.setLayout(QVBoxLayout()) + self.setFrameShadow(self.Plain) + self.setFrameShape(self.NoFrame) + self.setWidgetResizable(True) + + widget = QWidget(self) + widget.setLayout(QVBoxLayout()) + widget.show() + self.setWidget(widget) def load(self): - while self.layout().count() > 0: - item = self.layout().takeAt(0) + while self.widget().layout().count() > 0: + item = self.widget().layout().takeAt(0) if item.widget(): item.widget().hide() item.widget().deleteLater() for backend in self.weboob.iter_backends(): account = Account(self.weboob, backend) - self.layout().addWidget(account) + self.widget().layout().addWidget(account) - self.layout().addStretch() + self.widget().layout().addStretch() diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index 5af096ce..e1073914 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -25,9 +25,9 @@ from dateutil import tz from weboob.capabilities.base import NotLoaded from weboob.capabilities.chat import ICapChat from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread -from weboob.capabilities.dating import ICapDating, StatusField, OptimizationNotFound +from weboob.capabilities.dating import ICapDating, OptimizationNotFound from weboob.capabilities.contact import ICapContact, Contact, ContactPhoto, ProfileNode, Query, QueryError -from weboob.capabilities.account import ICapAccount +from weboob.capabilities.account import ICapAccount, StatusField from weboob.tools.backend import BaseBackend from weboob.tools.browser import BrowserUnavailable from weboob.tools.value import Value, ValuesDict, ValueBool @@ -90,17 +90,6 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh self.add_optimization('PRIORITY_CONNECTION', PriorityConnection(self.weboob.scheduler, self.storage, self.browser)) self.add_optimization('QUERIES_QUEUE', QueriesQueue(self.weboob.scheduler, self.storage, self.browser)) - def get_status(self): - with self.browser: - try: - return ( - StatusField('myname', 'My name', self.browser.get_my_name()), - StatusField('score', 'Score', self.browser.score()), - StatusField('avcharms', 'Available charms', self.browser.nb_available_charms()), - ) - except AdopteWait: - return (StatusField('notice', '', u'

You are currently waiting 1am to be able to connect with this account

', StatusField.FIELD_HTML|StatusField.FIELD_TEXT)) - # ---- ICapMessages methods --------------------- def fill_thread(self, thread, fields): @@ -495,6 +484,18 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh """ raise NotImplementedError() + def get_account_status(self): + with self.browser: + try: + return ( + StatusField('myname', 'My name', self.browser.get_my_name()), + StatusField('score', 'Score', self.browser.score()), + StatusField('avcharms', 'Available charms', self.browser.nb_available_charms()), + StatusField('godchilds', 'Number of godchilds', self.browser.nb_godchilds()), + ) + except AdopteWait: + return (StatusField('notice', '', u'

You are currently waiting 1am to be able to connect with this account

', StatusField.FIELD_HTML|StatusField.FIELD_TEXT)) + OBJECTS = {Thread: fill_thread, Contact: fill_contact, ContactPhoto: fill_photo diff --git a/weboob/backends/sfr/backend.py b/weboob/backends/sfr/backend.py index 6b05652e..3218ce8b 100644 --- a/weboob/backends/sfr/backend.py +++ b/weboob/backends/sfr/backend.py @@ -18,7 +18,8 @@ from __future__ import with_statement -from weboob.capabilities.messages import CantSendMessage, ICapMessagesPost, StatusField +from weboob.capabilities.messages import CantSendMessage, ICapMessages, ICapMessagesPost +from weboob.capabilities.account import ICapAccount, StatusField from weboob.tools.backend import BaseBackend from weboob.tools.value import ValuesDict, Value @@ -28,7 +29,7 @@ from .browser import SfrBrowser __all__ = ['SfrBackend'] -class SfrBackend(BaseBackend, ICapMessagesPost): +class SfrBackend(BaseBackend, ICapAccount, ICapMessages, ICapMessagesPost): NAME = 'sfr' MAINTAINER = 'Christophe Benz' EMAIL = 'christophe.benz@gmail.com' @@ -38,13 +39,14 @@ class SfrBackend(BaseBackend, ICapMessagesPost): CONFIG = ValuesDict(Value('login', label='Login'), Value('password', label='Password', masked=True)) BROWSER = SfrBrowser + ACCOUNT_REGISTER_PROPERTIES = None def create_default_browser(self): return self.create_browser(self.config['login'], self.config['password']) # ICapMessagesPost methods - def get_status(self): + def get_account_status(self): with self.browser: return (StatusField('nb_remaining_free_sms', 'Number of remaining free SMS', self.browser.get_nb_remaining_free_sms()),) @@ -53,4 +55,4 @@ class SfrBackend(BaseBackend, ICapMessagesPost): if not message.content.strip(): raise CantSendMessage(u'Message content is empty.') with self.browser: - self.browser.post_message(message) \ No newline at end of file + self.browser.post_message(message) diff --git a/weboob/capabilities/account.py b/weboob/capabilities/account.py index 0cbb4d4d..9ca69080 100644 --- a/weboob/capabilities/account.py +++ b/weboob/capabilities/account.py @@ -32,8 +32,21 @@ class Account(CapBaseObject): self.add_field('password', basestring) self.add_field('properties', dict) +class StatusField(object): + FIELD_TEXT = 0x001 # the value is a long text + FIELD_HTML = 0x002 # the value is HTML formated + + def __init__(self, key, label, value, flags=0): + self.key = key + self.label = label + self.value = value + self.flags = flags + + class ICapAccount(IBaseCap): - ACCOUNT_REGISTER_PROPERTIES = [] + # This class constant may be a list of Value* objects. If the value remains + # None, weboob considers that register_account() isn't supported. + ACCOUNT_REGISTER_PROPERTIES = None @staticmethod def register_account(account): @@ -64,3 +77,11 @@ class ICapAccount(IBaseCap): Update the current account. """ raise NotImplementedError() + + def get_account_status(self): + """ + Get status of the current account. + + @return a list of fields + """ + raise NotImplementedError() diff --git a/weboob/capabilities/dating.py b/weboob/capabilities/dating.py index 1addab27..bcb05dc9 100644 --- a/weboob/capabilities/dating.py +++ b/weboob/capabilities/dating.py @@ -45,24 +45,7 @@ class Optimization(object): def set_config(self, params): raise NotImplementedError() -class StatusField(object): - FIELD_TEXT = 0x001 # the value is a long text - FIELD_HTML = 0x002 # the value is HTML formated - - def __init__(self, key, label, value, flags=0): - self.key = key - self.label = label - self.value = value - self.flags = flags - - class ICapDating(IBaseCap): - def get_status(self): - """ - Get a list of fields - """ - raise NotImplementedError() - def init_optimizations(self): raise NotImplementedError() diff --git a/weboob/capabilities/messages.py b/weboob/capabilities/messages.py index 8096fb9a..c8692785 100644 --- a/weboob/capabilities/messages.py +++ b/weboob/capabilities/messages.py @@ -117,13 +117,6 @@ class Thread(CapBaseObject): yield m -class StatusField(object): - def __init__(self, key, label, value): - self.key = key - self.label = label - self.value = value - - class ICapMessages(IBaseCap): def iter_threads(self): """ @@ -162,12 +155,6 @@ class CantSendMessage(Exception): pass class ICapMessagesPost(IBaseCap): - def get_status(self): - """ - Get a list of fields - """ - raise NotImplementedError() - def post_message(self, message): """ Post a message. diff --git a/weboob/tools/application/qt/backendcfg.py b/weboob/tools/application/qt/backendcfg.py index 1b1edfd6..99b3a385 100644 --- a/weboob/tools/application/qt/backendcfg.py +++ b/weboob/tools/application/qt/backendcfg.py @@ -287,7 +287,7 @@ class BackendCfg(QDialog): backend.description, ', '.join([cap.__name__ for cap in backend.iter_caps()]))) - if backend.has_caps(ICapAccount) and self.ui.nameEdit.isEnabled(): + if backend.has_caps(ICapAccount) and self.ui.nameEdit.isEnabled() and backend.klass.ACCOUNT_REGISTER_PROPERTIES is not None: self.ui.registerButton.show() else: self.ui.registerButton.hide() diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index d38ed46a..ecb3a365 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -151,7 +151,7 @@ class ReplApplication(Cmd, BaseApplication): print 'Backend "%s" does not exist.' % name return None - if not backend.has_caps(ICapAccount): + if not backend.has_caps(ICapAccount) or backend.klass.ACCOUNT_REGISTER_PROPERTIES is None: print 'You can\'t register a new account with %s' % name return None