annotate cagou/core/utils.py @ 391:841045d5a937

common: when possible, a more user friendly name is displayed for the contact, instead of the jid.
author Goffi <goffi@goffi.org>
date Thu, 06 Feb 2020 21:16:21 +0100
parents 3d009fec34da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr//bin/env python2
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
2
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 312
diff changeset
5 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """misc utils/behaviors"""
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.animation import Animation
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class FilterBehavior(object):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 """class to handle items filtering with animation"""
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def __init__(self, *args, **kwargs):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 super(FilterBehavior, self).__init__(*args, **kwargs)
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
31 self._filter_last = ''
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self._filter_anim = Animation(width = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 height = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 opacity = 0,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 d = 0.5)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
219
9faccd140119 plugin contact list: refactoring:
Goffi <goffi@goffi.org>
parents: 196
diff changeset
37 def do_filter(self, children, text, get_child_text, width_cb, height_cb,
9faccd140119 plugin contact list: refactoring:
Goffi <goffi@goffi.org>
parents: 196
diff changeset
38 continue_tests=None):
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """filter the children
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 filtered children will have a animation to set width, height and opacity to 0
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @param children(kivy.uix.widget.Widget): widgets to filter
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 @param text(unicode): filter text (if this text is not present in a child,
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 the child is filtered out)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 @param get_child_text(callable): must retrieve child text
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 child is used as sole argument
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @param width_cb(callable, int, None): method to retrieve width when opened
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 child is used as sole argument, int can be used instead of callable
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param height_cb(callable, int, None): method to retrieve height when opened
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 child is used as sole argument, int can be used instead of callable
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 @param continue_tests(list[callable]): list of test to skip the item
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 all callables take child as sole argument.
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 if any of the callable return True, the child is skipped (i.e. not filtered)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 """
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 text = text.strip().lower()
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 filtering = len(text)>len(self._filter_last)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self._filter_last = text
390
3d009fec34da core (utils): fixed FilterBehavior use of "children" attribute
Goffi <goffi@goffi.org>
parents: 379
diff changeset
58 for child in children:
253
112ba3295f8f core (utils): use generator when checking continue_tests in FilterBehavior:
Goffi <goffi@goffi.org>
parents: 219
diff changeset
59 if continue_tests is not None and any((t(child) for t in continue_tests)):
196
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 continue
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 if text in get_child_text(child).lower():
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self._filter_anim.cancel(child)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 for key, method in (('width', width_cb),
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 ('height', height_cb),
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 ('opacity', lambda c: 1)):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 setattr(child, key, method(child))
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except TypeError:
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # method is not a callable, must be an int
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 setattr(child, key, method)
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 elif (filtering
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 and child.opacity > 0
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 and not self._filter_anim.have_properties_to_animate(child)):
519b3a29743c utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self._filter_anim.start(child)