[Git][gajim/gajim][master] 2 commits: Add BaseModule

Philipp Hörist gitlab at dev.gajim.org
Sat Jan 26 20:18:01 CET 2019


Philipp Hörist pushed to branch master at gajim / gajim


Commits:
ce0abb99 by Philipp Hörist at 2019-01-26T13:34:58Z
Add BaseModule

- - - - -
80e5934b by Philipp Hörist at 2019-01-26T19:04:43Z
Dont skip extension point

- If the contact has no avatar the tooltip extension point was not executed

- - - - -


4 changed files:

- + gajim/common/modules/base.py
- gajim/common/modules/blocking.py
- gajim/common/modules/muc.py
- gajim/gtk/tooltips.py


Changes:

=====================================
gajim/common/modules/base.py
=====================================
@@ -0,0 +1,59 @@
+# This file is part of Gajim.
+#
+# Gajim 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 only.
+#
+# Gajim 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 Gajim.  If not, see <http://www.gnu.org/licenses/>.
+
+import logging
+from functools import partial
+from unittest.mock import Mock
+
+from gajim.common import app
+
+log = logging.getLogger('gajim.c.m.base')
+
+
+class BaseModule:
+
+    _nbxmpp_extends = ''
+    _nbxmpp_methods = []
+
+    def __init__(self, con):
+        self._con = con
+        self._account = con.name
+        self._nbxmpp_callbacks = {}
+        self.handlers = []
+
+    def __getattr__(self, key):
+        if key not in self._nbxmpp_methods:
+            raise AttributeError
+        if not app.account_is_connected(self._account):
+            log.warning('Account %s not connected, cant use %s',
+                        self._account, key)
+            return
+
+        module = self._con.connection.get_module(self._nbxmpp_extends)
+
+        return partial(getattr(module, key),
+                       callback=self._nbxmpp_callbacks.get(key))
+
+    def _nbxmpp(self, module_name=None):
+        if not app.account_is_connected(self._account):
+            log.warning('Account %s not connected, cant use nbxmpp method',
+                        self._account)
+            return Mock()
+
+        if module_name is None:
+            return self._con.connection
+        return self._con.connection.get_module(module_name)
+
+    def _register_callback(self, method, callback):
+        self._nbxmpp_callbacks[method] = callback


=====================================
gajim/common/modules/blocking.py
=====================================
@@ -15,30 +15,28 @@
 # XEP-0191: Blocking Command
 
 import logging
-from functools import wraps
 
 import nbxmpp
 
 from gajim.common import app
 from gajim.common.nec import NetworkEvent
 from gajim.common.nec import NetworkIncomingEvent
+from gajim.common.modules.base import BaseModule
 
 log = logging.getLogger('gajim.c.m.blocking')
 
 
-def ensure_online(func):
-    @wraps(func)
-    def func_wrapper(self, *args, **kwargs):
-        if not app.account_is_connected(self._account):
-            return
-        return func(self, *args, **kwargs)
-    return func_wrapper
+class Blocking(BaseModule):
 
+    _nbxmpp_extends = 'Blocking'
+    _nbxmpp_methods = [
+        'block',
+        'unblock',
+        'get_blocking_list',
+    ]
 
-class Blocking:
     def __init__(self, con):
-        self._con = con
-        self._account = con.name
+        BaseModule.__init__(self, con)
 
         self.blocked = []
 
@@ -46,22 +44,10 @@ class Blocking:
             ('iq', self._blocking_push_received, 'set', nbxmpp.NS_BLOCKING)
         ]
 
-        self.supported = False
-
-        self._nbmxpp_methods = [
-            'block',
-            'unblock',
-        ]
+        self._register_callback('get_blocking_list',
+                                self._blocking_list_received)
 
-    def __getattr__(self, key):
-        if key not in self._nbmxpp_methods:
-            raise AttributeError
-        if not app.account_is_connected(self._account):
-            log.warning('Account %s not connected, cant use %s',
-                        self._account, key)
-            return
-        module = self._con.connection.get_module('Blocking')
-        return getattr(module, key)
+        self.supported = False
 
     def pass_disco(self, from_, _identities, features, _data, _node):
         if nbxmpp.NS_BLOCKING not in features:
@@ -75,15 +61,6 @@ class Blocking:
 
         log.info('Discovered blocking: %s', from_)
 
-    @ensure_online
-    def get_blocking_list(self, callback=None):
-        log.info('Request list')
-        if callback is None:
-            callback = self._blocking_list_received
-
-        self._con.connection.get_module('Blocking').get_blocking_list(
-            callback=callback)
-
     def _blocking_list_received(self, result):
         if result.is_error:
             log.info('Error: %s', result.error)
@@ -98,7 +75,7 @@ class Blocking:
         childs = reply.getChildren()
         for child in childs:
             reply.delChild(child)
-        self._con.connection.send(reply)
+        self._nbxmpp().send(reply)
 
         changed_list = []
 
@@ -149,7 +126,7 @@ class Blocking:
         log.info('Presence probe: %s', jid)
         # Send a presence Probe to get the current Status
         probe = nbxmpp.Presence(jid, 'probe', frm=self._con.get_own_jid())
-        self._con.connection.send(probe)
+        self._nbxmpp().send(probe)
 
 
 class BlockingEvent(NetworkIncomingEvent):


=====================================
gajim/common/modules/muc.py
=====================================
@@ -30,14 +30,32 @@ from gajim.common.helpers import AdditionalDataDict
 from gajim.common.caps_cache import muc_caps_cache
 from gajim.common.nec import NetworkEvent
 from gajim.common.modules.bits_of_binary import store_bob_data
+from gajim.common.modules.base import BaseModule
+
 
 log = logging.getLogger('gajim.c.m.muc')
 
 
-class MUC:
+class MUC(BaseModule):
+
+    _nbxmpp_extends = 'MUC'
+    _nbxmpp_methods = [
+        'get_affiliation',
+        'set_role',
+        'set_affiliation',
+        'set_config',
+        'set_subject',
+        'cancel_config',
+        'send_captcha',
+        'decline',
+        'invite',
+        'request_config',
+        'request_voice',
+        'destroy',
+    ]
+
     def __init__(self, con):
-        self._con = con
-        self._account = con.name
+        BaseModule.__init__(self, con)
 
         self.handlers = [
             StanzaHandler(name='presence',
@@ -75,28 +93,7 @@ class MUC:
                           priority=49)
         ]
 
-        self._nbmxpp_methods = [
-            'get_affiliation',
-            'set_role',
-            'set_affiliation',
-            'set_config',
-            'set_subject',
-            'cancel_config',
-            'send_captcha',
-            'decline',
-            'request_voice',
-            'destroy',
-        ]
-
-    def __getattr__(self, key):
-        if key not in self._nbmxpp_methods:
-            raise AttributeError
-        if not app.account_is_connected(self._account):
-            log.warning('Account %s not connected, cant use %s',
-                        self._account, key)
-            return
-        module = self._con.connection.get_module('MUC')
-        return getattr(module, key)
+        self._register_callback('request_config', self._config_received)
 
     def pass_disco(self, from_, identities, features, _data, _node):
         for identity in identities:
@@ -423,24 +420,13 @@ class MUC:
             raise nbxmpp.NodeProcessed
 
     def invite(self, room, to, reason=None, continue_=False):
-        if not app.account_is_connected(self._account):
-            return
-
         type_ = InviteType.MEDIATED
         contact = app.contacts.get_contact_from_full_jid(self._account, to)
         if contact and contact.supports(nbxmpp.NS_CONFERENCE):
             type_ = InviteType.DIRECT
 
         password = app.gc_passwords.get(room, None)
-        self._con.connection.get_module('MUC').invite(
-            room, to, reason, password, continue_, type_)
-
-    def request_config(self, room_jid):
-        if not app.account_is_connected(self._account):
-            return
-
-        self._con.connection.get_module('MUC').request_config(
-            room_jid, callback=self._config_received)
+        self._nbxmpp('MUC').invite(room, to, reason, password, continue_, type_)
 
     def _config_received(self, result):
         if result.is_error:


=====================================
gajim/gtk/tooltips.py
=====================================
@@ -449,10 +449,9 @@ class RosterTooltip(StatusTable):
         scale = self._ui.tooltip_grid.get_scale_factor()
         surface = app.contacts.get_avatar(
             account, self.prim_contact.jid, AvatarSize.TOOLTIP, scale)
-        if surface is None:
-            return
-        self._ui.avatar.set_from_surface(surface)
-        self._ui.avatar.show()
+        if surface is not None:
+            self._ui.avatar.set_from_surface(surface)
+            self._ui.avatar.show()
 
         app.plugin_manager.gui_extension_point(
             'roster_tooltip_populate', self, contacts, self._ui.tooltip_grid)



View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/dad99f85fccf30ab91b4c7babf2a24c1faeabb0e...80e5934bb5e60b872b9b9dff72ecd096b19b109c

-- 
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/dad99f85fccf30ab91b4c7babf2a24c1faeabb0e...80e5934bb5e60b872b9b9dff72ecd096b19b109c
You're receiving this email because of your account on dev.gajim.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gajim.org/pipermail/commits/attachments/20190126/39239203/attachment-0001.html>


More information about the Commits mailing list