annotate libervia/backend/tools/common/data_objects.py @ 4099:11e802089b19

tools (common/data_objects): add `attachments` in Message
author Goffi <goffi@goffi.org>
date Thu, 22 Jun 2023 15:47:17 +0200
parents f8284f994948
children b274f0d5c138
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3480
7550ae9cfbac Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Libervia: an XMPP client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3310
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
2300
173d56315529 jp (event/invitee): added list command to get R.S.V.P. :
Goffi <goffi@goffi.org>
parents: 2264
diff changeset
19 """Objects handling bridge data, with jinja2 safe markup handling"""
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.tools.common import data_format
3261
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
23 from os.path import basename
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 try:
4087
f8284f994948 tools (common/data_objects): fix import of Markup following change after Jinja2 version bump
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
26 from markupsafe import Markup as safe
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 except ImportError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
28 safe = str
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.tools.common import uri as xmpp_uri
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
31 import urllib.request, urllib.parse, urllib.error
2482
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
32
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
33 q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@")
2476
6607bb29ab7b tools (common/data_objects): BlogItem has now a uri property to generate its own XMPP URI
Goffi <goffi@goffi.org>
parents: 2469
diff changeset
34
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
36 class Message(object):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
37 def __init__(self, msg_data):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
38 self._uid = msg_data[0]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
39 self._timestamp = msg_data[1]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
40 self._from_jid = msg_data[2]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
41 self._to_jid = msg_data[3]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
42 self._message_data = msg_data[4]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
43 self._subject_data = msg_data[5]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
44 self._type = msg_data[6]
3170
39d7327583e1 core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
45 self._extra = data_format.deserialise(msg_data[7])
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
46 self._html = dict(data_format.get_sub_dict("xhtml", self._extra))
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
47
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
48 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
49 def id(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
50 return self._uid
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
51
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
52 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
53 def timestamp(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
54 return self._timestamp
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
55
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
56 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
57 def from_(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
58 return self._from_jid
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
59
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
60 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
61 def text(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
62 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 return self._message_data[""]
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
64 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
65 return next(iter(self._message_data.values()))
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
66
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
67 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
68 def subject(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
69 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 return self._subject_data[""]
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
71 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
72 return next(iter(self._subject_data.values()))
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
73
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
74 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
75 def type(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
76 return self._type
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
77
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
78 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
79 def thread(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 return self._extra.get("thread")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
81
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
82 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
83 def thread_parent(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 return self._extra.get("thread_parent")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
85
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
86 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
87 def received(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 return self._extra.get("received_timestamp", self._timestamp)
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
89
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
90 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
91 def delay_sender(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 return self._extra.get("delay_sender")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
93
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
94 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
95 def info_type(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 return self._extra.get("info_type")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
97
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
98 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
99 def html(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
100 if not self._html:
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
101 return None
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
102 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 return safe(self._html[""])
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
104 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
105 return safe(next(iter(self._html.values())))
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
106
4099
11e802089b19 tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents: 4087
diff changeset
107 @property
11e802089b19 tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents: 4087
diff changeset
108 def attachments(self):
11e802089b19 tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents: 4087
diff changeset
109 return self._extra.get("attachments", [])
11e802089b19 tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents: 4087
diff changeset
110
11e802089b19 tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents: 4087
diff changeset
111
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
112
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
113 class Messages(object):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
114 def __init__(self, msgs_data):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
115 self.messages = [Message(m) for m in msgs_data]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
116
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
117 def __len__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
118 return self.messages.__len__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
119
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
120 def __missing__(self, key):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
121 return self.messages.__missing__(key)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
122
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
123 def __getitem__(self, key):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
124 return self.messages.__getitem__(key)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
125
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
126 def __iter__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
127 return self.messages.__iter__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
128
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
129 def __reversed__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
130 return self.messages.__reversed__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
131
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
132 def __contains__(self, item):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
133 return self.messages.__contains__(item)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
134
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135
2469
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
136 class Room(object):
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
137 def __init__(self, jid, name=None, url=None):
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
138 self.jid = jid
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
139 self.name = name or jid
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
140 if url is not None:
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
141 self.url = url
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
142
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
143
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
144 class Identity(object):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
145 def __init__(self, jid_str, data=None):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
146 self.jid_str = jid_str
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
147 self.data = data if data is not None else {}
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
148
3261
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
149 @property
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
150 def avatar_basename(self):
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
151 try:
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
152 return basename(self.data['avatar']['path'])
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
153 except (TypeError, KeyError):
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
154 return None
a9e8e925ad99 tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents: 3170
diff changeset
155
2537
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
156 def __getitem__(self, key):
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
157 return self.data[key]
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
158
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
159 def __getattr__(self, key):
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
160 try:
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
161 return self.data[key]
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
162 except KeyError:
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
163 raise AttributeError(key)
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
164
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
165
3264
9896589487ae tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents: 3261
diff changeset
166 class Identities:
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
167 def __init__(self):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
168 self.identities = {}
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
169
3264
9896589487ae tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents: 3261
diff changeset
170 def __iter__(self):
9896589487ae tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents: 3261
diff changeset
171 return iter(self.identities)
9896589487ae tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents: 3261
diff changeset
172
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
173 def __getitem__(self, jid_str):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
174 try:
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
175 return self.identities[jid_str]
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
176 except KeyError:
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
177 return None
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
178
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
179 def __setitem__(self, jid_str, data):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
180 self.identities[jid_str] = Identity(jid_str, data)
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
181
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
182 def __contains__(self, jid_str):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
183 return jid_str in self.identities
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
184
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
185
2482
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
186 class ObjectQuoter(object):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
187 """object wrapper which quote attribues (to be used in templates)"""
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
188
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
189 def __init__(self, obj):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
190 self.obj = obj
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
191
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
192 def __unicode__(self):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
193 return q(self.obj.__unicode__())
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
194
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
195 def __str__(self):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
196 return self.__unicode__()
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
197
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
198 def __getattr__(self, name):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
199 return q(self.obj.__getattr__(name))
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
200
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
201 def __getitem__(self, key):
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
202 return q(self.obj.__getitem__(key))
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
203
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
204
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
205 class OnClick(object):
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
206 """Class to handle clickable elements targets"""
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
207
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
208 def __init__(self, url=None):
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
209 self.url = url
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
210
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
211 def format_url(self, *args, **kwargs):
2482
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
212 """format URL using Python formatting
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
213
2482
ac6097a66944 tools (common/data_objects): accept same parameters as format en quote their values in OnClick.formatUrl
Goffi <goffi@goffi.org>
parents: 2478
diff changeset
214 values will be quoted before being used
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
215 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 return self.url.format(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2942
diff changeset
217 *[q(a) for a in args], **{k: ObjectQuoter(v) for k, v in kwargs.items()}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
218 )