Mercurial > libervia-desktop-kivy
annotate src/cagou/core/cagou_main.py @ 28:9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 21 Aug 2016 15:02:18 +0200 |
parents | d09bd16dbbe2 |
children | 8b5827c43155 |
rev | line source |
---|---|
6
85649eca9f9b
core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
1 #!/usr//bin/env python2 |
0 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
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/>. | |
19 | |
20 | |
14 | 21 from sat.core.i18n import _ |
6
85649eca9f9b
core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
22 import logging_setter |
85649eca9f9b
core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
23 logging_setter.set_logging() |
85649eca9f9b
core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
24 from constants import Const as C |
0 | 25 from sat.core import log as logging |
26 log = logging.getLogger(__name__) | |
27 from sat_frontends.quick_frontend.quick_app import QuickApp | |
28 from sat_frontends.bridge.DBus import DBusBridgeFrontend | |
29 import kivy | |
30 kivy.require('1.9.1') | |
31 import kivy.support | |
32 kivy.support.install_gobject_iteration() | |
33 from kivy.app import App | |
14 | 34 from kivy.lang import Builder |
0 | 35 import xmlui |
36 from profile_manager import ProfileManager | |
13 | 37 from widgets_handler import WidgetsHandler |
9 | 38 from kivy.uix.boxlayout import BoxLayout |
11
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
39 from cagou_widget import CagouWidget |
14 | 40 from importlib import import_module |
9 | 41 import os.path |
14 | 42 import glob |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
43 import cagou.plugins |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
44 import cagou.kv |
9 | 45 |
46 | |
47 class CagouRootWidget(BoxLayout): | |
48 | |
49 def __init__(self, widgets): | |
50 super(CagouRootWidget, self).__init__(orientation=("vertical")) | |
51 for wid in widgets: | |
52 self.add_widget(wid) | |
53 | |
54 def change_widgets(self, widgets): | |
55 self.clear_widgets() | |
56 for wid in widgets: | |
57 self.add_widget(wid) | |
0 | 58 |
59 | |
60 class CagouApp(App): | |
61 """Kivy App for Cagou""" | |
62 | |
63 def build(self): | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
64 return CagouRootWidget([ProfileManager()]) |
0 | 65 |
28
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
66 def expand(self, path): |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
67 """expand path and replace known values |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
68 |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
69 useful in kv. Values which can be used: |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
70 - {media}: media dir |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
71 """ |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
72 return os.path.expanduser(path).format(media=self.host.media_dir) |
9f9532eb835f
core: added expand method to expand filename with magic values, specially useful in kv
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
73 |
0 | 74 |
75 class Cagou(QuickApp): | |
76 MB_HANDLE = False | |
77 | |
78 def __init__(self): | |
79 super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui) | |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
80 self._import_kv() |
0 | 81 self.app = CagouApp() |
25
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
82 self.media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir") |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
83 self.app.default_avatar = os.path.join(self.media_dir, "misc/default_avatar.png") |
14 | 84 self._plg_wids = [] # widget plugins |
85 self._import_plugins() | |
0 | 86 |
87 def run(self): | |
88 self.app.run() | |
89 | |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
90 def _defaultFactory(self, plugin_info, target, profiles): |
14 | 91 """factory used to create widget instance when PLUGIN_INFO["factory"] is not set""" |
92 main_cls = plugin_info['main'] | |
19
c58b522607f4
main: fixed profiles value in _defaultFactory + getPluggedWidgets is now a generator
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
93 return self.widgets.getOrCreateWidget(main_cls, target, on_new_widget=None, profiles=iter(self.profiles)) |
14 | 94 |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
95 def _import_kv(self): |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
96 """import all kv files in cagou.kv""" |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
97 path = os.path.dirname(cagou.kv.__file__) |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
98 for kv_path in glob.glob(os.path.join(path, "*.kv")): |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
99 Builder.load_file(kv_path) |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
100 log.debug(u"kv file {} loaded".format(kv_path)) |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
101 |
14 | 102 def _import_plugins(self): |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
103 """import all plugins""" |
14 | 104 self.default_wid = None |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
105 plugins_path = os.path.dirname(cagou.plugins.__file__) |
14 | 106 plug_lst = [os.path.splitext(p)[0] for p in map(os.path.basename, glob.glob(os.path.join(plugins_path, "plugin*.py")))] |
107 imported_names = set() # use to avoid loading 2 times plugin with same import name | |
108 for plug in plug_lst: | |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
109 plugin_path = 'cagou.plugins.' + plug |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
110 mod = import_module(plugin_path) |
14 | 111 try: |
112 plugin_info = mod.PLUGIN_INFO | |
113 except AttributeError: | |
114 plugin_info = {} | |
115 | |
116 # import name is used to differentiate plugins | |
117 if 'import_name' not in plugin_info: | |
118 plugin_info['import_name'] = plug | |
119 if 'import_name' in imported_names: | |
120 log.warning(_(u"there is already a plugin named {}, ignoring new one").format(plugin_info['import_name'])) | |
121 continue | |
122 if plugin_info['import_name'] == C.WID_SELECTOR: | |
123 # if WidgetSelector exists, it will be our default widget | |
124 self.default_wid = plugin_info | |
125 | |
126 # we want everything optional, so we use plugin file name | |
127 # if actual name is not found | |
128 if 'name' not in plugin_info: | |
129 plugin_info['name'] = plug[plug.rfind('_')+1:] | |
130 | |
131 # we need to load the kv file | |
132 if 'kv_file' not in plugin_info: | |
133 plugin_info['kv_file'] = u'{}.kv'.format(plug) | |
15
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
134 kv_path = os.path.join(plugins_path, plugin_info['kv_file']) |
56838ad5c84b
files reorganisation, cagou is now launched with python2 cagou.py in src/
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
135 Builder.load_file(kv_path) |
14 | 136 |
137 # what is the main class ? | |
138 main_cls = getattr(mod, plugin_info['main']) | |
139 plugin_info['main'] = main_cls | |
140 | |
141 # factory is used to create the instance | |
142 # if not found, we use a defaut one with getOrCreateWidget | |
143 if 'factory' not in plugin_info: | |
144 plugin_info['factory'] = self._defaultFactory | |
145 | |
25
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
146 # icons |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
147 for size in ('small', 'medium'): |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
148 key = u'icon_{}'.format(size) |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
149 try: |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
150 path = plugin_info[key] |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
151 except KeyError: |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
152 path = C.DEFAULT_WIDGET_ICON.format(media=self.media_dir) |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
153 else: |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
154 path = path.format(media=self.media_dir) |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
155 if not os.path.isfile(path): |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
156 path = C.DEFAULT_WIDGET_ICON.format(media=self.media_dir) |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
157 plugin_info[key] = path |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
158 |
14 | 159 self._plg_wids.append(plugin_info) |
160 if not self._plg_wids: | |
161 log.error(_(u"no widget plugin found")) | |
162 return | |
163 | |
164 # we want widgets sorted by names | |
165 self._plg_wids.sort(key=lambda p: p['name'].lower()) | |
166 | |
167 if self.default_wid is None: | |
168 # we have no selector widget, we use the first widget as default | |
169 self.default_wid = self._plg_wids[0] | |
170 | |
171 def getPluggedWidgets(self, except_cls=None): | |
172 """get available widgets plugin infos | |
173 | |
174 @param except_cls(None, class): if not None, | |
175 widgets from this class will be excluded | |
176 @return (list[dict]): available widgets plugin infos | |
177 """ | |
19
c58b522607f4
main: fixed profiles value in _defaultFactory + getPluggedWidgets is now a generator
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
178 for plugin_data in self._plg_wids: |
c58b522607f4
main: fixed profiles value in _defaultFactory + getPluggedWidgets is now a generator
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
179 if plugin_data['main'] == except_cls: |
c58b522607f4
main: fixed profiles value in _defaultFactory + getPluggedWidgets is now a generator
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
180 continue |
c58b522607f4
main: fixed profiles value in _defaultFactory + getPluggedWidgets is now a generator
Goffi <goffi@goffi.org>
parents:
16
diff
changeset
|
181 yield plugin_data |
14 | 182 |
9 | 183 def plugging_profiles(self): |
16
ba14b596b90e
host can now be get as a global value:
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
184 self.app.root.change_widgets([WidgetsHandler()]) |
9 | 185 |
186 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE): | |
187 log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status)) | |
188 | |
11
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
189 def switchWidget(self, old, new): |
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
190 """Replace old widget by new one |
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
191 |
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
192 old(CagouWidget): CagouWidget instance or a child |
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
193 new(CagouWidget): new widget instance |
49d30fc15884
core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
194 """ |
14 | 195 to_change = None |
196 if isinstance(old, CagouWidget): | |
197 to_change = old | |
198 else: | |
199 for w in old.walk_reverse(): | |
200 if isinstance(w, CagouWidget): | |
201 to_change = w | |
202 break | |
203 | |
204 if to_change is None: | |
205 log.error(u"no CagouWidget found when trying to switch widget") | |
206 else: | |
207 parent = to_change.parent | |
208 idx = parent.children.index(to_change) | |
209 parent.remove_widget(to_change) | |
210 parent.add_widget(new, index=idx) |