Mercurial > libervia-backend
annotate libervia/backend/tools/common/data_objects.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 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 | 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 |
4267
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
22 from libervia.backend.core.log import getLogger |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 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
|
24 from os.path import basename |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
25 |
2160
e67e8cd24141
core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 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
|
27 from markupsafe import Markup as safe |
2160
e67e8cd24141
core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 except ImportError: |
3028 | 29 safe = str |
2160
e67e8cd24141
core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.tools.common import uri as xmpp_uri |
3028 | 32 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
|
33 |
4267
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
34 |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
35 log = getLogger(__name__) |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
36 |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
37 |
3028 | 38 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
|
39 |
2160
e67e8cd24141
core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
41 class Message(object): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
42 def __init__(self, msg_data): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
43 self._uid = msg_data[0] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
44 self._timestamp = msg_data[1] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
45 self._from_jid = msg_data[2] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
46 self._to_jid = msg_data[3] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
47 self._message_data = msg_data[4] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
48 self._subject_data = msg_data[5] |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
49 self._type = msg_data[6] |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
50 self.extra = data_format.deserialise(msg_data[7]) |
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
51 self._html = dict(data_format.get_sub_dict("xhtml", self.extra)) |
4267
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
52 attachments = self.extra.get("attachments") |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
53 if attachments: |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
54 for attachment in attachments: |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
55 if "url" not in attachment: |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
56 try: |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
57 attachment["url"] = next( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4267
diff
changeset
|
58 s["url"] for s in attachment["sources"] if "url" in s |
4267
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
59 ) |
32388d743348
tools (common/data_objects): set attachments `url` from `sources` if missing.
Goffi <goffi@goffi.org>
parents:
4158
diff
changeset
|
60 except (StopIteration, KeyError): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4267
diff
changeset
|
61 log.warning(f"An attachment has no URL: {attachment}") |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
62 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
63 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
64 def id(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
65 return self._uid |
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 timestamp(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
69 return self._timestamp |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
70 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
71 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
72 def from_(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
73 return self._from_jid |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
74 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
75 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
76 def text(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
77 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 return self._message_data[""] |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
79 except KeyError: |
4110
b274f0d5c138
tools (common/data_objects): handle messages without body
Goffi <goffi@goffi.org>
parents:
4099
diff
changeset
|
80 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
|
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 subject(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
84 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 return self._subject_data[""] |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
86 except KeyError: |
3028 | 87 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
|
88 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
89 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
90 def type(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
91 return self._type |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
92 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
93 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
94 def thread(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
95 return self.extra.get("thread") |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
96 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
97 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
98 def thread_parent(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
99 return self.extra.get("thread_parent") |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
100 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
101 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
102 def received(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
103 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
|
104 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
105 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
106 def delay_sender(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
107 return self.extra.get("delay_sender") |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
108 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
109 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
110 def info_type(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
111 return self.extra.get("info_type") |
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 @property |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
114 def html(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
115 if not self._html: |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
116 return None |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
117 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 return safe(self._html[""]) |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
119 except KeyError: |
3028 | 120 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
|
121 |
4099
11e802089b19
tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents:
4087
diff
changeset
|
122 @property |
11e802089b19
tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents:
4087
diff
changeset
|
123 def attachments(self): |
4158
83d8d8500bc2
tools (common/data_objects): expose message's `extra` field.
Goffi <goffi@goffi.org>
parents:
4110
diff
changeset
|
124 return self.extra.get("attachments", []) |
4099
11e802089b19
tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents:
4087
diff
changeset
|
125 |
11e802089b19
tools (common/data_objects): add `attachments` in Message
Goffi <goffi@goffi.org>
parents:
4087
diff
changeset
|
126 |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
127 class Messages(object): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
128 def __init__(self, msgs_data): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
129 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
|
130 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
131 def __len__(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
132 return self.messages.__len__() |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
133 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
134 def __missing__(self, key): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
135 return self.messages.__missing__(key) |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
136 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
137 def __getitem__(self, key): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
138 return self.messages.__getitem__(key) |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
139 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
140 def __iter__(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
141 return self.messages.__iter__() |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
142 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
143 def __reversed__(self): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
144 return self.messages.__reversed__() |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
145 |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
146 def __contains__(self, item): |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
147 return self.messages.__contains__(item) |
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
148 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 |
2469
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
150 class Room(object): |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
151 def __init__(self, jid, name=None, url=None): |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
152 self.jid = jid |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
153 self.name = name or jid |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
154 if url is not None: |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
155 self.url = url |
adcc35625e17
tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents:
2464
diff
changeset
|
156 |
2464
8788c217a49e
tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
157 |
2257
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
158 class Identity(object): |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
159 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
|
160 self.jid_str = jid_str |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
161 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
|
162 |
3261
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
163 @property |
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
164 def avatar_basename(self): |
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
165 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4267
diff
changeset
|
166 return basename(self.data["avatar"]["path"]) |
3261
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
167 except (TypeError, KeyError): |
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
168 return None |
a9e8e925ad99
tools (common/data_objects): new `avatar_basename` property
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
169 |
2537
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
170 def __getitem__(self, key): |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
171 return self.data[key] |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
172 |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
173 def __getattr__(self, key): |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
174 try: |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
175 return self.data[key] |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
176 except KeyError: |
8cf58a415126
tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
177 raise AttributeError(key) |
2257
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 |
3264
9896589487ae
tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents:
3261
diff
changeset
|
180 class Identities: |
2257
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
181 def __init__(self): |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
182 self.identities = {} |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
183 |
3264
9896589487ae
tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents:
3261
diff
changeset
|
184 def __iter__(self): |
9896589487ae
tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents:
3261
diff
changeset
|
185 return iter(self.identities) |
9896589487ae
tools (common/data_objects): added __iter__ to Identities
Goffi <goffi@goffi.org>
parents:
3261
diff
changeset
|
186 |
2257
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
187 def __getitem__(self, jid_str): |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
188 try: |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
189 return self.identities[jid_str] |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
190 except KeyError: |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
191 return None |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
192 |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
193 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
|
194 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
|
195 |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
196 def __contains__(self, jid_str): |
0ffa8ba0857c
core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents:
2228
diff
changeset
|
197 return jid_str in self.identities |
2383
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
198 |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
199 |
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
|
200 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
|
201 """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
|
202 |
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 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
|
204 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
|
205 |
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
|
206 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
|
207 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
|
208 |
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
|
209 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
|
210 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
|
211 |
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 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
|
213 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
|
214 |
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
|
215 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
|
216 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
|
217 |
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
|
218 |
2383
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
219 class OnClick(object): |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
220 """Class to handle clickable elements targets""" |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
221 |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
222 def __init__(self, url=None): |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
223 self.url = url |
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
224 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
225 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
|
226 """format URL using Python formatting |
2383
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
227 |
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
|
228 values will be quoted before being used |
2383
a37457da2bb7
tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents:
2300
diff
changeset
|
229 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
230 return self.url.format( |
3028 | 231 *[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
|
232 ) |