comparison sat/tools/common/data_objects.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 93a421de0e3d
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """Objects handling bridge data, with jinja2 safe markup handling""" 20 """Objects handling bridge data, with jinja2 safe markup handling"""
21 21
22 from sat.tools.common import data_format 22 from sat.tools.common import data_format
23
23 try: 24 try:
24 from jinja2 import Markup as safe 25 from jinja2 import Markup as safe
25 except ImportError: 26 except ImportError:
26 safe = unicode 27 safe = unicode
27 28
28 from sat.tools.common import uri as xmpp_uri 29 from sat.tools.common import uri as xmpp_uri
29 import urllib 30 import urllib
30 31
31 q = lambda value: urllib.quote(value.encode('utf-8'), safe='@') 32 q = lambda value: urllib.quote(value.encode("utf-8"), safe="@")
32 33
33 34
34 class BlogItem(object): 35 class BlogItem(object):
35
36 def __init__(self, mb_data, parent): 36 def __init__(self, mb_data, parent):
37 self.mb_data = mb_data 37 self.mb_data = mb_data
38 self.parent = parent 38 self.parent = parent
39 self._tags = None 39 self._tags = None
40 self._groups = None 40 self._groups = None
41 self._comments = None 41 self._comments = None
42 self._comments_items_list = None 42 self._comments_items_list = None
43 43
44 @property 44 @property
45 def id(self): 45 def id(self):
46 return self.mb_data.get(u'id') 46 return self.mb_data.get(u"id")
47 47
48 @property 48 @property
49 def atom_id(self): 49 def atom_id(self):
50 return self.mb_data.get(u'atom_id') 50 return self.mb_data.get(u"atom_id")
51 51
52 @property 52 @property
53 def uri(self): 53 def uri(self):
54 node = self.parent.node 54 node = self.parent.node
55 service = self.parent.service 55 service = self.parent.service
56 return xmpp_uri.buildXMPPUri(u'pubsub', 56 return xmpp_uri.buildXMPPUri(
57 subtype=u'microblog', 57 u"pubsub", subtype=u"microblog", path=service, node=node, item=self.id
58 path=service, 58 )
59 node=node,
60 item=self.id)
61 59
62 @property 60 @property
63 def published(self): 61 def published(self):
64 return self.mb_data.get(u'published') 62 return self.mb_data.get(u"published")
65 63
66 @property 64 @property
67 def updated(self): 65 def updated(self):
68 return self.mb_data.get(u'updated') 66 return self.mb_data.get(u"updated")
69 67
70 @property 68 @property
71 def language(self): 69 def language(self):
72 return self.mb_data.get(u'language') 70 return self.mb_data.get(u"language")
73 71
74 @property 72 @property
75 def author(self): 73 def author(self):
76 return self.mb_data.get(u'author') 74 return self.mb_data.get(u"author")
77 75
78 @property 76 @property
79 def author_jid(self): 77 def author_jid(self):
80 return self.mb_data.get(u'author_jid') 78 return self.mb_data.get(u"author_jid")
81 79
82 @property 80 @property
83 def author_jid_verified(self): 81 def author_jid_verified(self):
84 return self.mb_data.get(u'author_jid_verified') 82 return self.mb_data.get(u"author_jid_verified")
85 83
86 @property 84 @property
87 def author_email(self): 85 def author_email(self):
88 return self.mb_data.get(u'author_email') 86 return self.mb_data.get(u"author_email")
89 87
90 @property 88 @property
91 def tags(self): 89 def tags(self):
92 if self._tags is None: 90 if self._tags is None:
93 self._tags = list(data_format.dict2iter('tag', self.mb_data)) 91 self._tags = list(data_format.dict2iter("tag", self.mb_data))
94 return self._tags 92 return self._tags
95 93
96 @property 94 @property
97 def groups(self): 95 def groups(self):
98 if self._groups is None: 96 if self._groups is None:
99 self._groups = list(data_format.dict2iter('group', self.mb_data)) 97 self._groups = list(data_format.dict2iter("group", self.mb_data))
100 return self._groups 98 return self._groups
101 99
102 @property 100 @property
103 def title(self): 101 def title(self):
104 return self.mb_data.get(u'title') 102 return self.mb_data.get(u"title")
105 103
106 @property 104 @property
107 def title_xhtml(self): 105 def title_xhtml(self):
108 try: 106 try:
109 return safe(self.mb_data[u'title_xhtml']) 107 return safe(self.mb_data[u"title_xhtml"])
110 except KeyError: 108 except KeyError:
111 return None 109 return None
112 110
113 @property 111 @property
114 def content(self): 112 def content(self):
115 return self.mb_data.get(u'content') 113 return self.mb_data.get(u"content")
116 114
117 @property 115 @property
118 def content_xhtml(self): 116 def content_xhtml(self):
119 try: 117 try:
120 return safe(self.mb_data[u'content_xhtml']) 118 return safe(self.mb_data[u"content_xhtml"])
121 except KeyError: 119 except KeyError:
122 return None 120 return None
123 121
124 @property 122 @property
125 def comments(self): 123 def comments(self):
126 if self._comments is None: 124 if self._comments is None:
127 self._comments = data_format.dict2iterdict(u'comments', self.mb_data, (u'node', u'service')) 125 self._comments = data_format.dict2iterdict(
126 u"comments", self.mb_data, (u"node", u"service")
127 )
128 return self._comments 128 return self._comments
129 129
130 @property 130 @property
131 def comments_service(self): 131 def comments_service(self):
132 return self.mb_data.get(u'comments_service') 132 return self.mb_data.get(u"comments_service")
133 133
134 @property 134 @property
135 def comments_node(self): 135 def comments_node(self):
136 return self.mb_data.get(u'comments_node') 136 return self.mb_data.get(u"comments_node")
137 137
138 @property 138 @property
139 def comments_items_list(self): 139 def comments_items_list(self):
140 return [] if self._comments_items_list is None else self._comments_items_list 140 return [] if self._comments_items_list is None else self._comments_items_list
141 141
145 self._comments_items_list = [] 145 self._comments_items_list = []
146 self._comments_items_list.append(items) 146 self._comments_items_list.append(items)
147 147
148 148
149 class BlogItems(object): 149 class BlogItems(object):
150
151 def __init__(self, mb_data): 150 def __init__(self, mb_data):
152 self.items = [BlogItem(i, self) for i in mb_data[0]] 151 self.items = [BlogItem(i, self) for i in mb_data[0]]
153 self.metadata = mb_data[1] 152 self.metadata = mb_data[1]
154 153
155 @property 154 @property
156 def service(self): 155 def service(self):
157 return self.metadata[u'service'] 156 return self.metadata[u"service"]
158 157
159 @property 158 @property
160 def node(self): 159 def node(self):
161 return self.metadata[u'node'] 160 return self.metadata[u"node"]
162 161
163 @property 162 @property
164 def uri(self): 163 def uri(self):
165 return self.metadata[u'uri'] 164 return self.metadata[u"uri"]
166 165
167 def __len__(self): 166 def __len__(self):
168 return self.items.__len__() 167 return self.items.__len__()
169 168
170 def __missing__(self, key): 169 def __missing__(self, key):
182 def __contains__(self, item): 181 def __contains__(self, item):
183 return self.items.__contains__(item) 182 return self.items.__contains__(item)
184 183
185 184
186 class Message(object): 185 class Message(object):
187
188 def __init__(self, msg_data): 186 def __init__(self, msg_data):
189 self._uid = msg_data[0] 187 self._uid = msg_data[0]
190 self._timestamp = msg_data[1] 188 self._timestamp = msg_data[1]
191 self._from_jid = msg_data[2] 189 self._from_jid = msg_data[2]
192 self._to_jid = msg_data[3] 190 self._to_jid = msg_data[3]
193 self._message_data = msg_data[4] 191 self._message_data = msg_data[4]
194 self._subject_data = msg_data[5] 192 self._subject_data = msg_data[5]
195 self._type = msg_data[6] 193 self._type = msg_data[6]
196 self._extra = msg_data[7] 194 self._extra = msg_data[7]
197 self._html = dict(data_format.getSubDict('xhtml', self._extra)) 195 self._html = dict(data_format.getSubDict("xhtml", self._extra))
198 196
199 @property 197 @property
200 def id(self): 198 def id(self):
201 return self._uid 199 return self._uid
202 200
209 return self._from_jid 207 return self._from_jid
210 208
211 @property 209 @property
212 def text(self): 210 def text(self):
213 try: 211 try:
214 return self._message_data[''] 212 return self._message_data[""]
215 except KeyError: 213 except KeyError:
216 return next(self._message_data.itervalues()) 214 return next(self._message_data.itervalues())
217 215
218 @property 216 @property
219 def subject(self): 217 def subject(self):
220 try: 218 try:
221 return self._subject_data[''] 219 return self._subject_data[""]
222 except KeyError: 220 except KeyError:
223 return next(self._subject_data.itervalues()) 221 return next(self._subject_data.itervalues())
224 222
225 @property 223 @property
226 def type(self): 224 def type(self):
227 return self._type 225 return self._type
228 226
229 @property 227 @property
230 def thread(self): 228 def thread(self):
231 return self._extra.get('thread') 229 return self._extra.get("thread")
232 230
233 @property 231 @property
234 def thread_parent(self): 232 def thread_parent(self):
235 return self._extra.get('thread_parent') 233 return self._extra.get("thread_parent")
236 234
237 @property 235 @property
238 def received(self): 236 def received(self):
239 return self._extra.get('received_timestamp', self._timestamp) 237 return self._extra.get("received_timestamp", self._timestamp)
240 238
241 @property 239 @property
242 def delay_sender(self): 240 def delay_sender(self):
243 return self._extra.get('delay_sender') 241 return self._extra.get("delay_sender")
244 242
245 @property 243 @property
246 def info_type(self): 244 def info_type(self):
247 return self._extra.get('info_type') 245 return self._extra.get("info_type")
248 246
249 @property 247 @property
250 def html(self): 248 def html(self):
251 if not self._html: 249 if not self._html:
252 return None 250 return None
253 try: 251 try:
254 return safe(self._html['']) 252 return safe(self._html[""])
255 except KeyError: 253 except KeyError:
256 return safe(next(self._html.itervalues())) 254 return safe(next(self._html.itervalues()))
257 255
258 256
259 class Messages(object): 257 class Messages(object):
260
261 def __init__(self, msgs_data): 258 def __init__(self, msgs_data):
262 self.messages = [Message(m) for m in msgs_data] 259 self.messages = [Message(m) for m in msgs_data]
263 260
264 def __len__(self): 261 def __len__(self):
265 return self.messages.__len__() 262 return self.messages.__len__()
277 return self.messages.__reversed__() 274 return self.messages.__reversed__()
278 275
279 def __contains__(self, item): 276 def __contains__(self, item):
280 return self.messages.__contains__(item) 277 return self.messages.__contains__(item)
281 278
279
282 class Room(object): 280 class Room(object):
283
284 def __init__(self, jid, name=None, url=None): 281 def __init__(self, jid, name=None, url=None):
285 self.jid = jid 282 self.jid = jid
286 self.name = name or jid 283 self.name = name or jid
287 if url is not None: 284 if url is not None:
288 self.url = url 285 self.url = url
289 286
290 287
291 class Identity(object): 288 class Identity(object):
292
293 def __init__(self, jid_str, data=None): 289 def __init__(self, jid_str, data=None):
294 self.jid_str = jid_str 290 self.jid_str = jid_str
295 self.data = data if data is not None else {} 291 self.data = data if data is not None else {}
296 292
297 def __getitem__(self, key): 293 def __getitem__(self, key):
303 except KeyError: 299 except KeyError:
304 raise AttributeError(key) 300 raise AttributeError(key)
305 301
306 302
307 class Identities(object): 303 class Identities(object):
308
309 def __init__(self): 304 def __init__(self):
310 self.identities = {} 305 self.identities = {}
311 306
312 def __getitem__(self, jid_str): 307 def __getitem__(self, jid_str):
313 try: 308 try:
350 def formatUrl(self, *args, **kwargs): 345 def formatUrl(self, *args, **kwargs):
351 """format URL using Python formatting 346 """format URL using Python formatting
352 347
353 values will be quoted before being used 348 values will be quoted before being used
354 """ 349 """
355 return self.url.format(*[q(a) for a in args], 350 return self.url.format(
356 **{k: ObjectQuoter(v) for k,v in kwargs.iteritems()}) 351 *[q(a) for a in args], **{k: ObjectQuoter(v) for k, v in kwargs.iteritems()}
352 )