[Git][gajim/gajim][master] Register all available modules automatically

Philipp Hörist gitlab at dev.gajim.org
Sat Jul 7 13:53:07 CEST 2018


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


Commits:
59428d2f by Philipp Hörist at 2018-07-07T13:52:44+02:00
Register all available modules automatically

- - - - -


24 changed files:

- gajim/common/connection.py
- gajim/common/connection_handlers.py
- gajim/common/helpers.py
- gajim/common/modules/__init__.py
- gajim/common/modules/annotations.py
- gajim/common/modules/bookmarks.py
- gajim/common/modules/entity_time.py
- gajim/common/modules/http_auth.py
- gajim/common/modules/httpupload.py
- gajim/common/modules/last_activity.py
- gajim/common/modules/pep.py
- gajim/common/modules/ping.py
- gajim/common/modules/pubsub.py
- gajim/common/modules/roster_item_exchange.py
- gajim/common/modules/search.py
- gajim/common/modules/software_version.py
- gajim/common/modules/user_activity.py
- gajim/common/modules/user_avatar.py
- gajim/common/modules/user_location.py
- gajim/common/modules/user_mood.py
- gajim/common/modules/user_nickname.py
- gajim/common/modules/user_tune.py
- gajim/common/modules/vcard_avatars.py
- gajim/common/modules/vcard_temp.py


Changes:

=====================================
gajim/common/connection.py
=====================================
--- a/gajim/common/connection.py
+++ b/gajim/common/connection.py
@@ -61,29 +61,9 @@ from gajim.common import gpg
 from gajim.common import passwords
 from gajim.common import i18n
 from gajim.common import idle
-from gajim.common.helpers import ModuleMock
-from gajim.common.modules.entity_time import EntityTime
-from gajim.common.modules.software_version import SoftwareVersion
-from gajim.common.modules.ping import Ping
-from gajim.common.modules.search import Search
-from gajim.common.modules.annotations import Annotations
-from gajim.common.modules.roster_item_exchange import RosterItemExchange
-from gajim.common.modules.last_activity import LastActivity
-from gajim.common.modules.http_auth import HTTPAuth
-from gajim.common.modules.vcard_temp import VCardTemp
-from gajim.common.modules.vcard_avatars import VCardAvatars
-from gajim.common.modules.pubsub import PubSub
-from gajim.common.modules.bookmarks import Bookmarks
-from gajim.common.modules.pep import PEP
-from gajim.common.modules.user_avatar import UserAvatar
-from gajim.common.modules.user_activity import UserActivity
-from gajim.common.modules.user_tune import UserTune
-from gajim.common.modules.user_mood import UserMood
-from gajim.common.modules.user_location import UserLocation
-from gajim.common.modules.user_nickname import UserNickname
-from gajim.common.modules.httpupload import HTTPUpload
 from gajim.common.connection_handlers import *
 from gajim.common.contacts import GC_Contact
+from gajim.common import modules
 from gajim.gtkgui_helpers import get_action
 
 
@@ -182,19 +162,7 @@ class CommonConnection:
         app.ged.raise_event(event, self.name, data)
 
     def get_module(self, name):
-        try:
-            return self._modules[name]
-        except KeyError:
-            return ModuleMock()
-
-    def get_module_handlers(self):
-        handlers = []
-        for module in self._modules.values():
-            handlers += module.handlers
-        return handlers
-
-    def register_module(self, name, cls, *args, **kwargs):
-        self._modules[name] = cls(*args, **kwargs)
+        return modules.get(self.name, name)
 
     def reconnect(self):
         """
@@ -661,26 +629,8 @@ class Connection(CommonConnection, ConnectionHandlers):
 
         self.sm = Smacks(self) # Stream Management
 
-        self.register_module('EntityTime', EntityTime, self)
-        self.register_module('SoftwareVersion', SoftwareVersion, self)
-        self.register_module('Ping', Ping, self)
-        self.register_module('Search', Search, self)
-        self.register_module('Annotations', Annotations, self)
-        self.register_module('RosterItemExchange', RosterItemExchange, self)
-        self.register_module('LastActivity', LastActivity, self)
-        self.register_module('HTTPAuth', HTTPAuth, self)
-        self.register_module('VCardTemp', VCardTemp, self)
-        self.register_module('VCardAvatars', VCardAvatars, self)
-        self.register_module('PubSub', PubSub, self)
-        self.register_module('PEP', PEP, self)
-        self.register_module('Bookmarks', Bookmarks, self)
-        self.register_module('UserAvatar', UserAvatar, self)
-        self.register_module('UserActivity', UserActivity, self)
-        self.register_module('UserTune', UserTune, self)
-        self.register_module('UserMood', UserMood, self)
-        self.register_module('UserLocation', UserLocation, self)
-        self.register_module('UserNickname', UserNickname, self)
-        self.register_module('HTTPUpload', HTTPUpload, self)
+        # Register all modules
+        modules.register(self)
 
         app.ged.register_event_handler('privacy-list-received', ged.CORE,
             self._nec_privacy_list_received)
@@ -700,6 +650,8 @@ class Connection(CommonConnection, ConnectionHandlers):
 
     def cleanup(self):
         ConnectionHandlers.cleanup(self)
+        modules.unregister(self)
+
         app.ged.remove_event_handler('privacy-list-received', ged.CORE,
             self._nec_privacy_list_received)
         app.ged.remove_event_handler('agent-info-error-received', ged.CORE,
@@ -780,6 +732,7 @@ class Connection(CommonConnection, ConnectionHandlers):
             self.terminate_sessions()
             self.remove_all_transfers()
             self.connection.disconnect()
+            ConnectionHandlers._unregister_handlers(self)
             self.connection = None
 
     def set_oldst(self): # Set old state


=====================================
gajim/common/connection_handlers.py
=====================================
--- a/gajim/common/connection_handlers.py
+++ b/gajim/common/connection_handlers.py
@@ -37,6 +37,7 @@ from gi.repository import GLib
 import nbxmpp
 from gajim.common import caps_cache as capscache
 
+from gajim.common import modules
 from gajim.common import helpers
 from gajim.common import app
 from gajim.common import jingle_xtls
@@ -1451,5 +1452,9 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream):
         con.RegisterHandler('iq', self._BlockingResultCB, 'result',
             nbxmpp.NS_BLOCKING)
 
-        for handler in self.get_module_handlers():
+        for handler in modules.get_handlers(self):
             con.RegisterHandler(*handler)
+
+    def _unregister_handlers(self):
+        for handler in modules.get_handlers(self):
+            self.connection.UnregisterHandler(*handler)


=====================================
gajim/common/helpers.py
=====================================
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -165,13 +165,6 @@ class InvalidFormat(Exception):
     pass
 
 
-class ModuleMock:
-    def __getattr__(self, key):
-        def _mock(self, *args, **kwargs):
-            return
-        return _mock
-
-
 def decompose_jid(jidstring):
     user = None
     server = None


=====================================
gajim/common/modules/__init__.py
=====================================
--- a/gajim/common/modules/__init__.py
+++ b/gajim/common/modules/__init__.py
@@ -0,0 +1,65 @@
+# 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 importlib import import_module
+from pathlib import Path
+
+log = logging.getLogger('gajim.c.m')
+
+imported_modules = []
+_modules = {}
+
+for file in Path(__file__).parent.iterdir():
+    if file.stem == '__init__':
+        continue
+
+    module = import_module('.%s' % file.stem, package='gajim.common.modules')
+    if hasattr(module, 'get_instance'):
+        log.info('Load module: %s', file.stem)
+        imported_modules.append(module)
+
+
+class ModuleMock:
+    def __getattr__(self, key):
+        def _mock(self, *args, **kwargs):
+            return
+        return _mock
+
+
+def register(con, *args, **kwargs):
+    if con in _modules:
+        return
+    _modules[con.name] = {}
+    for module in imported_modules:
+        instance, name = module.get_instance(con, *args, **kwargs)
+        _modules[con.name][name] = instance
+
+
+def unregister(con):
+    del _modules[con.name]
+
+
+def get(account, name):
+    try:
+        return _modules[account][name]
+    except KeyError:
+        return ModuleMock()
+
+
+def get_handlers(con):
+    handlers = []
+    for module in _modules[con.name].values():
+        handlers += module.handlers
+    return handlers


=====================================
gajim/common/modules/annotations.py
=====================================
--- a/gajim/common/modules/annotations.py
+++ b/gajim/common/modules/annotations.py
@@ -90,3 +90,7 @@ class Annotations:
             log.warning('Storing rosternotes failed: %s', stanza.getError())
             return
         log.info('Storing rosternotes successful')
+
+
+def get_instance(*args, **kwargs):
+    return Annotations(*args, **kwargs), 'Annotations'


=====================================
gajim/common/modules/bookmarks.py
=====================================
--- a/gajim/common/modules/bookmarks.py
+++ b/gajim/common/modules/bookmarks.py
@@ -292,3 +292,7 @@ class Bookmarks:
 class BookmarksReceivedEvent(NetworkIncomingEvent):
     name = 'bookmarks-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return Bookmarks(*args, **kwargs), 'Bookmarks'


=====================================
gajim/common/modules/entity_time.py
=====================================
--- a/gajim/common/modules/entity_time.py
+++ b/gajim/common/modules/entity_time.py
@@ -162,3 +162,7 @@ class contact_tz(datetime.tzinfo):
 
     def dst(self, dt):
         return ZERO
+
+
+def get_instance(*args, **kwargs):
+    return EntityTime(*args, **kwargs), 'EntityTime'


=====================================
gajim/common/modules/http_auth.py
=====================================
--- a/gajim/common/modules/http_auth.py
+++ b/gajim/common/modules/http_auth.py
@@ -75,3 +75,7 @@ class HTTPAuth:
 class HttpAuthReceivedEvent(NetworkIncomingEvent):
     name = 'http-auth-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return HTTPAuth(*args, **kwargs), 'HTTPAuth'


=====================================
gajim/common/modules/httpupload.py
=====================================
--- a/gajim/common/modules/httpupload.py
+++ b/gajim/common/modules/httpupload.py
@@ -436,3 +436,7 @@ class UploadAbortedException(Exception):
 class HTTPUploadProgressEvent(NetworkIncomingEvent):
     name = 'httpupload-progress'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return HTTPUpload(*args, **kwargs), 'HTTPUpload'


=====================================
gajim/common/modules/last_activity.py
=====================================
--- a/gajim/common/modules/last_activity.py
+++ b/gajim/common/modules/last_activity.py
@@ -50,3 +50,7 @@ class LastActivity:
         self._con.connection.send(iq)
 
         raise nbxmpp.NodeProcessed
+
+
+def get_instance(*args, **kwargs):
+    return LastActivity(*args, **kwargs), 'LastActivity'


=====================================
gajim/common/modules/pep.py
=====================================
--- a/gajim/common/modules/pep.py
+++ b/gajim/common/modules/pep.py
@@ -212,3 +212,7 @@ class AbstractPEPData:
 class PEPReceivedEvent(NetworkIncomingEvent):
     name = 'pep-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return PEP(*args, **kwargs), 'PEP'


=====================================
gajim/common/modules/ping.py
=====================================
--- a/gajim/common/modules/ping.py
+++ b/gajim/common/modules/ping.py
@@ -120,3 +120,7 @@ class PingReplyEvent(NetworkIncomingEvent):
 class PingErrorEvent(NetworkIncomingEvent):
     name = 'ping-error'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return Ping(*args, **kwargs), 'Ping'


=====================================
gajim/common/modules/pubsub.py
=====================================
--- a/gajim/common/modules/pubsub.py
+++ b/gajim/common/modules/pubsub.py
@@ -254,3 +254,7 @@ class PubSub:
 class PubSubConfigReceivedEvent(NetworkIncomingEvent):
     name = 'pubsub-config-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return PubSub(*args, **kwargs), 'PubSub'


=====================================
gajim/common/modules/roster_item_exchange.py
=====================================
--- a/gajim/common/modules/roster_item_exchange.py
+++ b/gajim/common/modules/roster_item_exchange.py
@@ -121,3 +121,7 @@ class RosterItemExchange:
 class RosterItemExchangeEvent(NetworkIncomingEvent):
     name = 'roster-item-exchange-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return RosterItemExchange(*args, **kwargs), 'RosterItemExchange'


=====================================
gajim/common/modules/search.py
=====================================
--- a/gajim/common/modules/search.py
+++ b/gajim/common/modules/search.py
@@ -112,3 +112,7 @@ class SearchFormReceivedEvent(NetworkIncomingEvent):
 class SearchResultReceivedEvent(NetworkIncomingEvent):
     name = 'search-result-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return Search(*args, **kwargs), 'Search'


=====================================
gajim/common/modules/software_version.py
=====================================
--- a/gajim/common/modules/software_version.py
+++ b/gajim/common/modules/software_version.py
@@ -104,3 +104,7 @@ class VersionResultReceivedEvent(NetworkIncomingEvent):
 
     def generate(self):
         return True
+
+
+def get_instance(*args, **kwargs):
+    return SoftwareVersion(*args, **kwargs), 'SoftwareVersion'


=====================================
gajim/common/modules/user_activity.py
=====================================
--- a/gajim/common/modules/user_activity.py
+++ b/gajim/common/modules/user_activity.py
@@ -99,3 +99,7 @@ class UserActivity(AbstractPEPModule):
             i = item.addChild('text')
             i.addData(message)
         return item
+
+
+def get_instance(*args, **kwargs):
+    return UserActivity(*args, **kwargs), 'UserActivity'


=====================================
gajim/common/modules/user_avatar.py
=====================================
--- a/gajim/common/modules/user_avatar.py
+++ b/gajim/common/modules/user_avatar.py
@@ -152,3 +152,7 @@ class UserAvatar(AbstractPEPModule):
     def retract(self):
         # Not implemented yet
         return
+
+
+def get_instance(*args, **kwargs):
+    return UserAvatar(*args, **kwargs), 'UserAvatar'


=====================================
gajim/common/modules/user_location.py
=====================================
--- a/gajim/common/modules/user_location.py
+++ b/gajim/common/modules/user_location.py
@@ -83,3 +83,7 @@ class UserLocation(AbstractPEPModule):
             if data.get(field, False):
                 item.addChild(field, payload=data[field])
         return item
+
+
+def get_instance(*args, **kwargs):
+    return UserLocation(*args, **kwargs), 'UserLocation'


=====================================
gajim/common/modules/user_mood.py
=====================================
--- a/gajim/common/modules/user_mood.py
+++ b/gajim/common/modules/user_mood.py
@@ -86,3 +86,7 @@ class UserMood(AbstractPEPModule):
         if text:
             item.addChild('text', payload=text)
         return item
+
+
+def get_instance(*args, **kwargs):
+    return UserMood(*args, **kwargs), 'UserMood'


=====================================
gajim/common/modules/user_nickname.py
=====================================
--- a/gajim/common/modules/user_nickname.py
+++ b/gajim/common/modules/user_nickname.py
@@ -76,3 +76,7 @@ class UserNickname(AbstractPEPModule):
             else:
                 app.nicks[self._account] = app.config.get_per(
                     'accounts', self._account, 'name')
+
+
+def get_instance(*args, **kwargs):
+    return UserNickname(*args, **kwargs), 'UserNickname'


=====================================
gajim/common/modules/user_tune.py
=====================================
--- a/gajim/common/modules/user_tune.py
+++ b/gajim/common/modules/user_tune.py
@@ -96,3 +96,7 @@ class UserTune(AbstractPEPModule):
         if length:
             item.addChild('length', payload=length)
         return item
+
+
+def get_instance(*args, **kwargs):
+    return UserTune(*args, **kwargs), 'UserTune'


=====================================
gajim/common/modules/vcard_avatars.py
=====================================
--- a/gajim/common/modules/vcard_avatars.py
+++ b/gajim/common/modules/vcard_avatars.py
@@ -190,3 +190,7 @@ class VCardAvatars:
                      node.getTo() or own_jid, sha or 'no sha advertised')
             update.setTagData('photo', sha)
         return node
+
+
+def get_instance(*args, **kwargs):
+    return VCardAvatars(*args, **kwargs), 'VCardAvatars'


=====================================
gajim/common/modules/vcard_temp.py
=====================================
--- a/gajim/common/modules/vcard_temp.py
+++ b/gajim/common/modules/vcard_temp.py
@@ -306,3 +306,7 @@ class VcardNotPublishedEvent(NetworkIncomingEvent):
 class VcardReceivedEvent(NetworkIncomingEvent):
     name = 'vcard-received'
     base_network_events = []
+
+
+def get_instance(*args, **kwargs):
+    return VCardTemp(*args, **kwargs), 'VCardTemp'



View it on GitLab: https://dev.gajim.org/gajim/gajim/commit/59428d2f2fd3fad2614cd82fe41e94c8c99933b1

-- 
View it on GitLab: https://dev.gajim.org/gajim/gajim/commit/59428d2f2fd3fad2614cd82fe41e94c8c99933b1
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/20180707/a6c99435/attachment-0001.html>


More information about the Commits mailing list