annotate sat/tools/common/data_objects.py @ 2766:93a421de0e3d

tools (common/data_objects): metadata parsing in BlogItems: metadata are parsed to deserialise some well known values like rsm index and count, and some properties have been added to have easier access. A "complete" property (or item in metadata) is set to True if we are on the last page, False if we are not, or None if we don't have enough data to know if we are on the last page or not.
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 16:35:13 +0100
parents 56f94936df1e
children 2d353b74010e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT: a jabber client
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2482
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # (at your option) any later version.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # 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
13 # 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
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # 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
18 # 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
19
2300
173d56315529 jp (event/invitee): added list command to get R.S.V.P. :
Goffi <goffi@goffi.org>
parents: 2264
diff changeset
20 """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
21
2766
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
22 from sat.core.constants import Const as C
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.tools.common import data_format
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:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from jinja2 import Markup as safe
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 except ImportError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 safe = unicode
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
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
30 from sat.tools.common import uri as xmpp_uri
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
31 import urllib
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
33 q = lambda value: urllib.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
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class BlogItem(object):
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
37 def __init__(self, mb_data, parent):
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.mb_data = mb_data
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 self.parent = parent
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self._tags = None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self._groups = None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self._comments = None
2228
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
43 self._comments_items_list = None
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def id(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 return self.mb_data.get(u"id")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def atom_id(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 return self.mb_data.get(u"atom_id")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @property
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
54 def uri(self):
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
55 node = self.parent.node
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
56 service = self.parent.service
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 return xmpp_uri.buildXMPPUri(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 u"pubsub", subtype=u"microblog", path=service, node=node, item=self.id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 )
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
60
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
61 @property
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def published(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 return self.mb_data.get(u"published")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def updated(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 return self.mb_data.get(u"updated")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 @property
2264
a8eaaac4d80f plugin XEP-0277, tools/common/data_objects: language handling
Goffi <goffi@goffi.org>
parents: 2257
diff changeset
70 def language(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 return self.mb_data.get(u"language")
2264
a8eaaac4d80f plugin XEP-0277, tools/common/data_objects: language handling
Goffi <goffi@goffi.org>
parents: 2257
diff changeset
72
a8eaaac4d80f plugin XEP-0277, tools/common/data_objects: language handling
Goffi <goffi@goffi.org>
parents: 2257
diff changeset
73 @property
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def author(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 return self.mb_data.get(u"author")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def author_jid(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 return self.mb_data.get(u"author_jid")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def author_jid_verified(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 return self.mb_data.get(u"author_jid_verified")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def author_email(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 return self.mb_data.get(u"author_email")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def tags(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if self._tags is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 self._tags = list(data_format.dict2iter("tag", self.mb_data))
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return self._tags
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def groups(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if self._groups is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 self._groups = list(data_format.dict2iter("group", self.mb_data))
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return self._groups
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def title(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 return self.mb_data.get(u"title")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def title_xhtml(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 return safe(self.mb_data[u"title_xhtml"])
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except KeyError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 return None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def content(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 return self.mb_data.get(u"content")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def content_xhtml(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 return safe(self.mb_data[u"content_xhtml"])
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 except KeyError:
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 return None
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 def comments(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if self._comments is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 self._comments = data_format.dict2iterdict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 u"comments", self.mb_data, (u"node", u"service")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 )
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return self._comments
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def comments_service(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 return self.mb_data.get(u"comments_service")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 def comments_node(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 return self.mb_data.get(u"comments_node")
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138
2228
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
139 @property
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
140 def comments_items_list(self):
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
141 return [] if self._comments_items_list is None else self._comments_items_list
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
142
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
143 def appendCommentsItems(self, items):
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
144 """append comments items to self.comments_items"""
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
145 if self._comments_items_list is None:
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
146 self._comments_items_list = []
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
147 self._comments_items_list.append(items)
cf6c539672c7 tools (common/data_objects): comments items can now be chained to their parent
Goffi <goffi@goffi.org>
parents: 2160
diff changeset
148
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 class BlogItems(object):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def __init__(self, mb_data):
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
152 self.items = [BlogItem(i, self) for i in mb_data[0]]
2766
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
153 self.metadata = self._parseMetadata(mb_data[1])
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 def service(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 return self.metadata[u"service"]
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def node(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 return self.metadata[u"node"]
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 @property
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 def uri(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
165 return self.metadata[u"uri"]
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
2766
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
167 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
168 def with_rsm(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
169 """Return True if RSM is activated on this request"""
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
170 return u"rsm_first" in self.metadata
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
171
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
172 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
173 def rsm_first(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
174 return self.metadata[u"rsm_first"]
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
175
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
176 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
177 def rsm_last(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
178 return self.metadata[u"rsm_last"]
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
179
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
180 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
181 def rsm_index(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
182 return self.metadata[u"rsm_index"]
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
183
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
184 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
185 def rsm_count(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
186 return self.metadata[u"rsm_count"]
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
187
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
188 @property
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
189 def complete(self):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
190 return self.metadata[u"complete"]
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
191
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
192 def _parseMetadata(self, metadata):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
193 data = {}
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
194
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
195 for key, value in metadata.iteritems():
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
196 if key in (u"rsm_index", u"rsm_count"):
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
197 value = int(value)
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
198 elif key == u"mam_stable":
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
199 value = C.bool(value)
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
200 elif key == u"mam_complete":
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
201 key = u"complete"
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
202 value = C.bool(value)
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
203 data[key] = value
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
204 if u"complete" not in data:
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
205 index = data.get(u"rsm_index")
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
206 count = data.get(u"rsm_count")
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
207 if index is None or count is None:
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
208 # we don't have enough information to know if the data is complete or not
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
209 data[u"complete"] = None
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
210 else:
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
211 # normally we have a strict equality here but XEP-0059 states
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
212 # that index MAY be approximative, so just in case…
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
213 data[u"complete"] = index + len(self.items) >= count
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
214 return data
93a421de0e3d tools (common/data_objects): metadata parsing in BlogItems:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
215
2160
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 def __len__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 return self.items.__len__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 def __missing__(self, key):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 return self.items.__missing__(key)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 def __getitem__(self, key):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 return self.items.__getitem__(key)
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 def __iter__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return self.items.__iter__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 def __reversed__(self):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 return self.items.__reversed__()
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 def __contains__(self, item):
e67e8cd24141 core (tools/common): data objects first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 return self.items.__contains__(item)
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
233
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
234
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
235 class Message(object):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
236 def __init__(self, msg_data):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
237 self._uid = msg_data[0]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
238 self._timestamp = msg_data[1]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
239 self._from_jid = msg_data[2]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
240 self._to_jid = msg_data[3]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
241 self._message_data = msg_data[4]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
242 self._subject_data = msg_data[5]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
243 self._type = msg_data[6]
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
244 self._extra = msg_data[7]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 self._html = dict(data_format.getSubDict("xhtml", self._extra))
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
246
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
247 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
248 def id(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
249 return self._uid
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
250
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
251 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
252 def timestamp(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
253 return self._timestamp
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
254
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
255 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
256 def from_(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
257 return self._from_jid
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
258
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
259 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
260 def text(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
261 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
262 return self._message_data[""]
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
263 except KeyError:
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
264 return next(self._message_data.itervalues())
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
265
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
266 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
267 def subject(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
268 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
269 return self._subject_data[""]
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
270 except KeyError:
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
271 return next(self._subject_data.itervalues())
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
272
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
273 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
274 def type(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
275 return self._type
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
276
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
277 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
278 def thread(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279 return self._extra.get("thread")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
280
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
281 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
282 def thread_parent(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
283 return self._extra.get("thread_parent")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
284
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
285 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
286 def received(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
287 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
288
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
289 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
290 def delay_sender(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
291 return self._extra.get("delay_sender")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
292
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
293 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
294 def info_type(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 return self._extra.get("info_type")
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
296
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
297 @property
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
298 def html(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
299 if not self._html:
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
300 return None
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
301 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
302 return safe(self._html[""])
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
303 except KeyError:
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
304 return safe(next(self._html.itervalues()))
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
305
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
306
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
307 class Messages(object):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
308 def __init__(self, msgs_data):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
309 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
310
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
311 def __len__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
312 return self.messages.__len__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
313
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
314 def __missing__(self, key):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
315 return self.messages.__missing__(key)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
316
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
317 def __getitem__(self, key):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
318 return self.messages.__getitem__(key)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
319
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
320 def __iter__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
321 return self.messages.__iter__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
322
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
323 def __reversed__(self):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
324 return self.messages.__reversed__()
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
325
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
326 def __contains__(self, item):
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
327 return self.messages.__contains__(item)
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
328
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
329
2469
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
330 class Room(object):
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
331 def __init__(self, jid, name=None, url=None):
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
332 self.jid = jid
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
333 self.name = name or jid
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
334 if url is not None:
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
335 self.url = url
adcc35625e17 tools (common/data_objects): added Room class
Goffi <goffi@goffi.org>
parents: 2464
diff changeset
336
2464
8788c217a49e tools (common/data_objects): added classes for messages
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
337
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
338 class Identity(object):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
339 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
340 self.jid_str = jid_str
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
341 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
342
2537
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
343 def __getitem__(self, key):
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
344 return self.data[key]
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
345
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
346 def __getattr__(self, key):
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
347 try:
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
348 return self.data[key]
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
349 except KeyError:
8cf58a415126 tools (common/data_objects): fixed Identity
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
350 raise AttributeError(key)
2257
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
351
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
352
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
353 class Identities(object):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
354 def __init__(self):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
355 self.identities = {}
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
356
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
357 def __getitem__(self, jid_str):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
358 try:
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
359 return self.identities[jid_str]
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
360 except KeyError:
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
361 return None
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
362
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
363 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
364 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
365
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
366 def __contains__(self, jid_str):
0ffa8ba0857c core (tools/common/data_objects): added classes to handle Identities
Goffi <goffi@goffi.org>
parents: 2228
diff changeset
367 return jid_str in self.identities
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
368
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
369
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
370 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
371 """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
372
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
373 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
374 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
375
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
376 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
377 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
378
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
379 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
380 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
381
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
382 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
383 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
384
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
385 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
386 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
387
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
388
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
389 class OnClick(object):
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
390 """Class to handle clickable elements targets"""
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
391
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
392 def __init__(self, url=None):
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
393 self.url = url
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
394
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
395 def formatUrl(self, *args, **kwargs):
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
396 """format URL using Python formatting
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
397
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
398 values will be quoted before being used
2383
a37457da2bb7 tools (common/data_objects): new OnClick class
Goffi <goffi@goffi.org>
parents: 2300
diff changeset
399 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
400 return self.url.format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
401 *[q(a) for a in args], **{k: ObjectQuoter(v) for k, v in kwargs.iteritems()}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
402 )