annotate libervia/frontends/quick_frontend/quick_contact_management.py @ 4326:5fd6a4dc2122

cli (output/std): use `rich` to output JSON.
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 11:38:44 +0100
parents 26b7ed2817da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
0
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
4 # helper class for making a SAT frontend
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
22
1009
d1084f7e56a5 quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents: 811
diff changeset
23 log = getLogger(__name__)
4074
26b7ed2817da refactoring: rename `sat_frontends` to `libervia.frontends`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
24 from libervia.frontends.tools.jid import JID
0
goffi@necton2
parents:
diff changeset
25
goffi@necton2
parents:
diff changeset
26
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
27 class QuickContactManagement(object):
0
goffi@necton2
parents:
diff changeset
28 """This helper class manage the contacts and ease the use of nicknames and shortcuts"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
29
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
30 ### FIXME: is SàT a better place for all this stuff ??? ###
0
goffi@necton2
parents:
diff changeset
31
goffi@necton2
parents:
diff changeset
32 def __init__(self):
goffi@necton2
parents:
diff changeset
33 self.__contactlist = {}
goffi@necton2
parents:
diff changeset
34
347
ea3e1b82dd79 core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents: 228
diff changeset
35 def __contains__(self, entity):
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 609
diff changeset
36 return entity.bare in self.__contactlist
347
ea3e1b82dd79 core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents: 228
diff changeset
37
52
6455fb62ff83 Connection/disconnection signals
Goffi <goffi@goffi.org>
parents: 51
diff changeset
38 def clear(self):
6455fb62ff83 Connection/disconnection signals
Goffi <goffi@goffi.org>
parents: 51
diff changeset
39 """Clear all the contact list"""
6455fb62ff83 Connection/disconnection signals
Goffi <goffi@goffi.org>
parents: 51
diff changeset
40 self.__contactlist.clear()
6455fb62ff83 Connection/disconnection signals
Goffi <goffi@goffi.org>
parents: 51
diff changeset
41
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
42 def add(self, entity):
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
43 """Add contact to the list, update resources"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
44 if entity.bare not in self.__contactlist:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 self.__contactlist[entity.bare] = {"resources": []}
347
ea3e1b82dd79 core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents: 228
diff changeset
46 if not entity.resource:
ea3e1b82dd79 core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents: 228
diff changeset
47 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 if entity.resource in self.__contactlist[entity.bare]["resources"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 self.__contactlist[entity.bare]["resources"].remove(entity.resource)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 self.__contactlist[entity.bare]["resources"].append(entity.resource)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
51
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
52 def get_cont_from_group(self, group):
54
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
53 """Return all contacts which are in given group"""
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
54 result = []
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
55 for contact in self.__contactlist:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 if "groups" in self.__contactlist[contact]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 if group in self.__contactlist[contact]["groups"]:
54
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
58 result.append(JID(contact))
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
59 return result
2ce9e350cdf9 Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents: 52
diff changeset
60
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
61 def get_attr(self, entity, name):
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
62 """Return a specific attribute of contact, or all attributes
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
63 @param entity: jid of the contact
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
64 @param name: name of the attribute
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
65 @return: asked attribute"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
66 if entity.bare in self.__contactlist:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 if name == "status": # FIXME: for the moment, we only use the first status
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 if self.__contactlist[entity.bare]["statuses"]:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 return list(self.__contactlist[entity.bare]["statuses"].values())[0]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70 if name in self.__contactlist[entity.bare]:
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 609
diff changeset
71 return self.__contactlist[entity.bare][name]
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
72 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 log.debug(_("Trying to get attribute for an unknown contact"))
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
74 return None
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
75
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 def is_connected(self, entity):
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
77 """Tell if the contact is online"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
78 return entity.bare in self.__contactlist
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
79
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
80 def remove(self, entity):
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
81 """remove resource. If no more resource is online or is no resource is specified, contact is deleted"""
0
goffi@necton2
parents:
diff changeset
82 try:
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
83 if entity.resource:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 self.__contactlist[entity.bare]["resources"].remove(entity.resource)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 if not entity.resource or not self.__contactlist[entity.bare]["resources"]:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 # no more resource available: the contact seems really disconnected
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 609
diff changeset
87 del self.__contactlist[entity.bare]
0
goffi@necton2
parents:
diff changeset
88 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 log.error(_("INTERNAL ERROR: Key log.error"))
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 70
diff changeset
90 raise
0
goffi@necton2
parents:
diff changeset
91
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
92 def update(self, entity, key, value):
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
93 """Update attribute of contact
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
94 @param entity: jid of the contact
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
95 @param key: name of the attribute
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
96 @param value: value of the attribute
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
97 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 if entity.bare in self.__contactlist:
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 609
diff changeset
99 self.__contactlist[entity.bare][key] = value
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
100 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 log.debug(_("Trying to update an unknown contact: %s") % entity.bare)
51
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
102
8c67ea98ab91 frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents: 0
diff changeset
103 def get_full(self, entity):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 return entity.bare + "/" + self.__contactlist[entity.bare]["resources"][-1]