comparison src/cagou.py @ 14:21a432afd06d

plugin system, first draft: - widgets are now handled with plugins, ContactList and WidgetSelector have been changed to be pluggins - everything in PLUGIN_INFO (including PLUGIN_INFO itself) is optional. Best guess is used if a value is missing - WidgetsHandler use default widget from plugins, which is WidgetSelector for now - PLUGIN_INFO is used in the same way as for backed plugins, with (for now) following possible keys: - "name": human readable name - "description": long description of what widget do - "import_name": unique short name used as reference for import - "main": main class name - "factory": callback used to instanciate a widget (see Cagou._defaultFactory) - "kv_file": path to the kv language file to load (same filename with ".kv" will be used if key is not found) other data should be added quickly, as a path to a file used as icon - host.getPluggedWidgets can be used to find loaded widgets. except_cls can be used to excluse a class (self.__class__ usually) - fixed host.switchWidget when old widget is already a CagouWidget - CagouWidget header new display the available widgets
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 16:02:44 +0200
parents 12a189fbb9ba
children 56838ad5c84b
comparison
equal deleted inserted replaced
13:12a189fbb9ba 14:21a432afd06d
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 from sat.core.i18n import _
21 import logging_setter 22 import logging_setter
22 logging_setter.set_logging() 23 logging_setter.set_logging()
23 from constants import Const as C 24 from constants import Const as C
24 from sat.core import log as logging 25 from sat.core import log as logging
25 log = logging.getLogger(__name__) 26 log = logging.getLogger(__name__)
29 import kivy 30 import kivy
30 kivy.require('1.9.1') 31 kivy.require('1.9.1')
31 import kivy.support 32 import kivy.support
32 kivy.support.install_gobject_iteration() 33 kivy.support.install_gobject_iteration()
33 from kivy.app import App 34 from kivy.app import App
35 from kivy.lang import Builder
34 import xmlui 36 import xmlui
35 from profile_manager import ProfileManager 37 from profile_manager import ProfileManager
36 from widgets_handler import WidgetsHandler 38 from widgets_handler import WidgetsHandler
37 from kivy.uix.boxlayout import BoxLayout 39 from kivy.uix.boxlayout import BoxLayout
38 from widget_selector import WidgetSelector
39 from cagou_widget import CagouWidget 40 from cagou_widget import CagouWidget
41 from importlib import import_module
40 import os.path 42 import os.path
43 import glob
41 44
42 45
43 class CagouRootWidget(BoxLayout): 46 class CagouRootWidget(BoxLayout):
44 47
45 def __init__(self, widgets): 48 def __init__(self, widgets):
67 super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui) 70 super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui)
68 self.app = CagouApp() 71 self.app = CagouApp()
69 self.app.host = self 72 self.app.host = self
70 media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir") 73 media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir")
71 self.app.default_avatar = os.path.join(media_dir, "misc/default_avatar.png") 74 self.app.default_avatar = os.path.join(media_dir, "misc/default_avatar.png")
75 self._plg_wids = [] # widget plugins
76 self._import_plugins()
72 77
73 def run(self): 78 def run(self):
74 self.app.run() 79 self.app.run()
75 80
81 def _defaultFactory(self, host, plugin_info, target, profiles):
82 """factory used to create widget instance when PLUGIN_INFO["factory"] is not set"""
83 main_cls = plugin_info['main']
84 return self.widgets.getOrCreateWidget(main_cls, target, on_new_widget=None, profiles=profiles)
85
86 def _import_plugins(self):
87 """Import all plugins"""
88 self.default_wid = None
89 plugins_path = os.path.dirname(__file__)
90 plug_lst = [os.path.splitext(p)[0] for p in map(os.path.basename, glob.glob(os.path.join(plugins_path, "plugin*.py")))]
91 imported_names = set() # use to avoid loading 2 times plugin with same import name
92 for plug in plug_lst:
93 mod = import_module(plug)
94 try:
95 plugin_info = mod.PLUGIN_INFO
96 except AttributeError:
97 plugin_info = {}
98
99 # import name is used to differentiate plugins
100 if 'import_name' not in plugin_info:
101 plugin_info['import_name'] = plug
102 if 'import_name' in imported_names:
103 log.warning(_(u"there is already a plugin named {}, ignoring new one").format(plugin_info['import_name']))
104 continue
105 if plugin_info['import_name'] == C.WID_SELECTOR:
106 # if WidgetSelector exists, it will be our default widget
107 self.default_wid = plugin_info
108
109 # we want everything optional, so we use plugin file name
110 # if actual name is not found
111 if 'name' not in plugin_info:
112 plugin_info['name'] = plug[plug.rfind('_')+1:]
113
114 # we need to load the kv file
115 if 'kv_file' not in plugin_info:
116 plugin_info['kv_file'] = u'{}.kv'.format(plug)
117 Builder.load_file(plugin_info['kv_file'])
118
119 # what is the main class ?
120 main_cls = getattr(mod, plugin_info['main'])
121 plugin_info['main'] = main_cls
122
123 # factory is used to create the instance
124 # if not found, we use a defaut one with getOrCreateWidget
125 if 'factory' not in plugin_info:
126 plugin_info['factory'] = self._defaultFactory
127
128 self._plg_wids.append(plugin_info)
129 if not self._plg_wids:
130 log.error(_(u"no widget plugin found"))
131 return
132
133 # we want widgets sorted by names
134 self._plg_wids.sort(key=lambda p: p['name'].lower())
135
136 if self.default_wid is None:
137 # we have no selector widget, we use the first widget as default
138 self.default_wid = self._plg_wids[0]
139
140 def getPluggedWidgets(self, except_cls=None):
141 """get available widgets plugin infos
142
143 @param except_cls(None, class): if not None,
144 widgets from this class will be excluded
145 @return (list[dict]): available widgets plugin infos
146 """
147 lst = self._plg_wids[:]
148 if except_cls is not None:
149 for plugin_info in lst:
150 if plugin_info['main'] == except_cls:
151 lst.remove(plugin_info)
152 break
153 return lst
154
76 def plugging_profiles(self): 155 def plugging_profiles(self):
77 widget_selector = WidgetSelector(self) 156 self.app.root.change_widgets([WidgetsHandler(self)])
78 self.app.root.change_widgets([WidgetsHandler(self, widget_selector)])
79 157
80 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE): 158 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
81 log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status)) 159 log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status))
82 160
83 def switchWidget(self, old, new): 161 def switchWidget(self, old, new):
84 """Replace old widget by new one 162 """Replace old widget by new one
85 163
86 old(CagouWidget): CagouWidget instance or a child 164 old(CagouWidget): CagouWidget instance or a child
87 new(CagouWidget): new widget instance 165 new(CagouWidget): new widget instance
88 """ 166 """
89 for w in old.walk_reverse(): 167 to_change = None
90 if isinstance(w, CagouWidget): 168 if isinstance(old, CagouWidget):
91 parent = w.parent 169 to_change = old
92 idx = parent.children.index(w) 170 else:
93 parent.remove_widget(w) 171 for w in old.walk_reverse():
94 parent.add_widget(new, index=idx) 172 if isinstance(w, CagouWidget):
95 break 173 to_change = w
174 break
175
176 if to_change is None:
177 log.error(u"no CagouWidget found when trying to switch widget")
178 else:
179 parent = to_change.parent
180 idx = parent.children.index(to_change)
181 parent.remove_widget(to_change)
182 parent.add_widget(new, index=idx)
96 183
97 184
98 if __name__ == '__main__': 185 if __name__ == '__main__':
99 Cagou().run() 186 Cagou().run()