changeset 16:ba14b596b90e

host can now be get as a global value: instead of always copying host from class to class, it can now be gotten from a global class with: from cagou import G then G.host will give host. This will probably be used on the long term on all SàT (backend + frontends). As host is currently needed in several places (most importantly in QuickFrontend), the argument is still present, and must be there even is unused on class inheriting from QuickSomething.
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 18:41:52 +0200
parents 56838ad5c84b
children 5c9feaa060a5
files src/cagou/__init__.py src/cagou/core/cagou_main.py src/cagou/core/cagou_widget.py src/cagou/core/profile_manager.py src/cagou/core/widgets_handler.py src/cagou/plugins/plugin_wid_contact_list.py src/cagou/plugins/plugin_wid_widget_selector.py
diffstat 7 files changed, 46 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/cagou/__init__.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/__init__.py	Sat Jul 09 18:41:52 2016 +0200
@@ -17,8 +17,17 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+
+class Global(object):
+    @property
+    def host(self):
+        return self._host
+G = Global()
+
+
 from core import cagou_main
 
+
 def run():
-    host = cagou_main.Cagou()
+    host = G._host = cagou_main.Cagou()
     host.run()
--- a/src/cagou/core/cagou_main.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/core/cagou_main.py	Sat Jul 09 18:41:52 2016 +0200
@@ -61,7 +61,7 @@
     """Kivy App for Cagou"""
 
     def build(self):
-        return CagouRootWidget([ProfileManager(self.host)])
+        return CagouRootWidget([ProfileManager()])
 
 
 class Cagou(QuickApp):
@@ -71,7 +71,6 @@
         super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui)
         self._import_kv()
         self.app = CagouApp()
-        self.app.host = self
         media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir")
         self.app.default_avatar = os.path.join(media_dir, "misc/default_avatar.png")
         self._plg_wids = []  # widget plugins
@@ -80,7 +79,7 @@
     def run(self):
         self.app.run()
 
-    def _defaultFactory(self, host, plugin_info, target, profiles):
+    def _defaultFactory(self, plugin_info, target, profiles):
         """factory used to create widget instance when PLUGIN_INFO["factory"] is not set"""
         main_cls = plugin_info['main']
         return self.widgets.getOrCreateWidget(main_cls, target, on_new_widget=None, profiles=profiles)
@@ -164,7 +163,7 @@
         return lst
 
     def plugging_profiles(self):
-        self.app.root.change_widgets([WidgetsHandler(self)])
+        self.app.root.change_widgets([WidgetsHandler()])
 
     def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
         log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status))
--- a/src/cagou/core/cagou_widget.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/core/cagou_widget.py	Sat Jul 09 18:41:52 2016 +0200
@@ -23,6 +23,7 @@
 from kivy.uix.button import Button
 from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.dropdown import DropDown
+from cagou import G
 
 
 class HeaderWidgetButton(Button):
@@ -33,8 +34,7 @@
 
     def __init__(self, cagou_widget):
         super(HeaderWidgetSelector, self).__init__()
-        host = cagou_widget.host
-        for plugin_info in host.getPluggedWidgets(except_cls=cagou_widget.__class__):
+        for plugin_info in G.host.getPluggedWidgets(except_cls=cagou_widget.__class__):
             btn = HeaderWidgetButton(text=plugin_info["name"])
             btn.bind(on_release=lambda btn, plugin_info=plugin_info: cagou_widget.switchWidget(plugin_info))
             self.add_widget(btn)
@@ -42,13 +42,12 @@
 
 class CagouWidget(BoxLayout):
 
-    def __init__(self, host):
-        self.host = host
+    def __init__(self):
         BoxLayout.__init__(self, orientation="vertical")
         self.selector = HeaderWidgetSelector(self)
 
     def switchWidget(self, plugin_info):
         self.selector.dismiss()
         factory = plugin_info["factory"]
-        new_widget = factory(self.host, plugin_info, None, None)
-        self.host.switchWidget(self, new_widget)
+        new_widget = factory(plugin_info, None, None)
+        G.host.switchWidget(self, new_widget)
--- a/src/cagou/core/profile_manager.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/core/profile_manager.py	Sat Jul 09 18:41:52 2016 +0200
@@ -28,6 +28,7 @@
 from kivy.uix.screenmanager import ScreenManager, Screen
 from kivy.adapters import listadapter
 from kivy import properties
+from cagou import G
 
 
 class ProfileItem(listview.ListItemButton):
@@ -39,7 +40,6 @@
     def __init__(self, pm, *args, **kwargs):
         super(ProfileListAdapter, self).__init__(*args, **kwargs)
         self.pm = pm
-        self.host = pm.host
 
     def closeUI(self, xmlui):
         self.pm.screen_manager.transition.direction = 'right'
@@ -58,9 +58,9 @@
         def authenticate_cb(data, cb_id, profile):
             if C.bool(data.pop('validated', C.BOOL_FALSE)):
                 super(ProfileListAdapter, self).select_item_view(view)
-            self.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile)
+            G.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile)
 
-        self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text)
+        G.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text)
 
 
 class ConnectButton(Button):
@@ -80,7 +80,6 @@
     def __init__(self, pm):
         super(NewProfileScreen, self).__init__(name=u'new_profile')
         self.pm = pm
-        self.host = pm.host
 
     def onCreationFailure(self, failure):
         msg = [l for l in unicode(failure).split('\n') if l][-1]
@@ -88,12 +87,12 @@
 
     def onCreationSuccess(self, profile):
         self.pm.profiles_screen.reload()
-        self.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure)
+        G.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure)
 
     def _sessionStarted(self, profile):
         jid = self.jid.text.strip()
-        self.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
-        self.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile)
+        G.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
+        G.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile)
         self.pm.screen_manager.transition.direction = 'right'
         self.pm.screen_manager.current = 'profiles'
 
@@ -101,14 +100,13 @@
         name = self.profile_name.text.strip()
         # XXX: we use XMPP password for profile password to simplify
         #      if user want to change profile password, he can do it in preferences
-        self.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure)
+        G.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure)
 
 
 class DeleteProfilesScreen(Screen):
 
     def __init__(self, pm):
         self.pm = pm
-        self.host = pm.host
         super(DeleteProfilesScreen, self).__init__(name=u'delete_profiles')
 
     def doDelete(self):
@@ -125,7 +123,7 @@
 
         for profile in to_delete:
             log.info(u"Deleteing profile [{}]".format(profile))
-            self.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc)
+            G.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc)
 
 
 class ProfilesScreen(Screen):
@@ -133,7 +131,7 @@
 
     def __init__(self, pm):
         self.pm = pm
-        profiles = pm.host.bridge.getProfilesList()
+        profiles = G.host.bridge.getProfilesList()
         profiles.sort()
         self.list_adapter = ProfileListAdapter(pm,
                                                data=profiles,
@@ -148,15 +146,15 @@
 
     def reload(self):
         """Reload profiles list"""
-        profiles = self.pm.host.bridge.getProfilesList()
+        profiles = G.host.bridge.getProfilesList()
         profiles.sort()
         self.list_adapter.data = profiles
 
 
 class ProfileManager(QuickProfileManager, BoxLayout):
 
-    def __init__(self, host, autoconnect=None):
-        QuickProfileManager.__init__(self, host, autoconnect)
+    def __init__(self, autoconnect=None):
+        QuickProfileManager.__init__(self, G.host, autoconnect)
         BoxLayout.__init__(self, orientation="vertical")
         self.screen_manager = ScreenManager()
         self.profiles_screen = ProfilesScreen(self)
--- a/src/cagou/core/widgets_handler.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/core/widgets_handler.py	Sat Jul 09 18:41:52 2016 +0200
@@ -23,6 +23,7 @@
 from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.button import Button
 from kivy import properties
+from cagou import G
 
 
 NEW_WIDGET_DIST = 10
@@ -67,8 +68,7 @@
 
 class WidgetsHandler(BoxLayout):
 
-    def __init__(self, host, wid=None, **kw):
-        self.host = host
+    def __init__(self, wid=None, **kw):
         if wid is None:
             wid=self.default_widget
         self.vert_wid = self.hor_wid = None
@@ -83,7 +83,7 @@
 
     @property
     def default_widget(self):
-        return self.host.default_wid['factory'](self.host, self.host.default_wid, None, None)
+        return G.host.default_wid['factory'](G.host.default_wid, None, None)
 
     def removeWidget(self, vertical):
         if vertical and self.vert_wid is not None:
@@ -96,11 +96,11 @@
     def setWidgetSize(self, vertical, size):
         if vertical:
             if self.vert_wid is None:
-                self.vert_wid = WidgetsHandler(self.host, self.default_widget, size_hint=(1, None))
+                self.vert_wid = WidgetsHandler(self.default_widget, size_hint=(1, None))
                 self.add_widget(self.vert_wid, len(self.children))
             self.vert_wid.height=size
         else:
             if self.hor_wid is None:
-                self.hor_wid = WidgetsHandler(self.host, self.default_widget, size_hint=(None, 1))
+                self.hor_wid = WidgetsHandler(self.default_widget, size_hint=(None, 1))
                 self.blh.add_widget(self.hor_wid, len(self.blh.children))
             self.hor_wid.width=size
--- a/src/cagou/plugins/plugin_wid_contact_list.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/plugins/plugin_wid_contact_list.py	Sat Jul 09 18:41:52 2016 +0200
@@ -28,6 +28,7 @@
 from kivy import properties
 from cagou.core import cagou_widget
 from cagou.core import image
+from cagou import G
 
 
 PLUGIN_INFO = {
@@ -52,8 +53,8 @@
 class ContactList(QuickContactList, cagou_widget.CagouWidget):
 
     def __init__(self, host, target, profiles):
-        QuickContactList.__init__(self, host, profiles)
-        cagou_widget.CagouWidget.__init__(self, host)
+        QuickContactList.__init__(self, G.host, profiles)
+        cagou_widget.CagouWidget.__init__(self)
         self.adapter = ListAdapter(data={},
                                    cls=ContactItem,
                                    args_converter=self.contactDataConverter,
--- a/src/cagou/plugins/plugin_wid_widget_selector.py	Sat Jul 09 17:24:01 2016 +0200
+++ b/src/cagou/plugins/plugin_wid_widget_selector.py	Sat Jul 09 18:41:52 2016 +0200
@@ -28,6 +28,7 @@
 from kivy import properties
 from kivy.uix.behaviors import ButtonBehavior
 from cagou.core import cagou_widget
+from cagou import G
 
 
 PLUGIN_INFO = {
@@ -43,12 +44,11 @@
 
     def __init__(self, **kwargs):
         super(WidgetSelItem, self).__init__(**kwargs)
-        self.host = kwargs['host']
 
     def select(self, *args):
         log.debug(u"widget selection: {}".format(self.plugin_info["name"]))
         factory = self.plugin_info["factory"]
-        self.host.switchWidget(self, factory(self.host, self.plugin_info, None, None))
+        G.host.switchWidget(self, factory(self.plugin_info, None, None))
 
     def deselect(self, *args):
         pass
@@ -56,11 +56,10 @@
 
 class WidgetSelector(cagou_widget.CagouWidget):
 
-    def __init__(self, host):
-        super(WidgetSelector, self).__init__(host)
-        self.host = host
+    def __init__(self):
+        super(WidgetSelector, self).__init__()
         self.adapter = ListAdapter(
-            data=host.getPluggedWidgets(except_cls=self.__class__),
+            data=G.host.getPluggedWidgets(except_cls=self.__class__),
             cls=WidgetSelItem,
             args_converter=self.dataConverter,
             selection_mode='single',
@@ -69,14 +68,11 @@
         self.add_widget(ListView(adapter=self.adapter))
 
     @classmethod
-    def factory(cls, host, plugin_info, target, profiles):
-        return cls(host)
+    def factory(cls, plugin_info, target, profiles):
+        return cls()
 
     def dataConverter(self, idx, plugin_info):
-        return {
-            "host": self.host,
-            "plugin_info": plugin_info,
-            }
+        return {"plugin_info": plugin_info}
 
 
 PLUGIN_INFO["factory"] = WidgetSelector.factory