comparison libervia/web/pages/_browser/cache.py @ 1544:9b451115e726

browser (cache): replace `print` by `log` calls
author Goffi <goffi@goffi.org>
date Thu, 06 Jul 2023 12:07:29 +0200
parents d7c78722e4f8
children
comparison
equal deleted inserted replaced
1543:f00497c00e38 1544:9b451115e726
23 except KeyError: 23 except KeyError:
24 self.request_data_from_backend() 24 self.request_data_from_backend()
25 else: 25 else:
26 cache = json.loads(cache) 26 cache = json.loads(cache)
27 if cache['metadata']['session_uuid'] != session_uuid: 27 if cache['metadata']['session_uuid'] != session_uuid:
28 print("data in cache are not valid for this session, resetting") 28 log.debug("data in cache are not valid for this session, resetting")
29 del storage['libervia_cache'] 29 del storage['libervia_cache']
30 self.request_data_from_backend() 30 self.request_data_from_backend()
31 else: 31 else:
32 self._cache = cache 32 self._cache = cache
33 print("storage cache is used") 33 log.debug("storage cache is used")
34 34
35 @property 35 @property
36 def roster(self): 36 def roster(self):
37 return self._cache['roster'] 37 return self._cache['roster']
38 38
39 @property 39 @property
40 def identities(self): 40 def identities(self):
41 return self._cache['identities'] 41 return self._cache['identities']
42 42
43 def update(self): 43 def update(self):
44 print(f"updating: {self._cache}") 44 log.debug(f"updating: {self._cache}")
45 storage['libervia_cache'] = json.dumps(self._cache) 45 storage['libervia_cache'] = json.dumps(self._cache)
46 print("cache stored") 46 log.debug("cache stored")
47 47
48 def _store_if_complete(self): 48 def _store_if_complete(self):
49 self._completed_count -= 1 49 self._completed_count -= 1
50 if self._completed_count == 0: 50 if self._completed_count == 0:
51 del self._completed_count 51 del self._completed_count
52 self.update() 52 self.update()
53 53
54 def get_contacts_cb(self, contacts): 54 def get_contacts_cb(self, contacts):
55 print("roster received") 55 log.debug("roster received")
56 roster = self._cache['roster'] 56 roster = self._cache['roster']
57 for contact_jid, attributes, groups in contacts: 57 for contact_jid, attributes, groups in contacts:
58 roster[contact_jid] = { 58 roster[contact_jid] = {
59 'attributes': attributes, 59 'attributes': attributes,
60 'groups': groups, 60 'groups': groups,
61 } 61 }
62 self._store_if_complete() 62 self._store_if_complete()
63 63
64 def identities_base_get_cb(self, identities_raw): 64 def identities_base_get_cb(self, identities_raw):
65 print("base identities received") 65 log.debug("base identities received")
66 identities = json.loads(identities_raw) 66 identities = json.loads(identities_raw)
67 self._cache['identities'].update(identities) 67 self._cache['identities'].update(identities)
68 self._store_if_complete() 68 self._store_if_complete()
69 69
70 def request_failed(self, exc, message): 70 def request_failed(self, exc, message):
78 }, 78 },
79 'roster': {}, 79 'roster': {},
80 'identities': {}, 80 'identities': {},
81 } 81 }
82 self._completed_count = 2 82 self._completed_count = 2
83 print("requesting roster to backend") 83 log.debug("requesting roster to backend")
84 bridge.contacts_get( 84 bridge.contacts_get(
85 callback=self.get_contacts_cb, 85 callback=self.get_contacts_cb,
86 errback=lambda e: self.request_failed(e, "Can't get contacts: {exc}") 86 errback=lambda e: self.request_failed(e, "Can't get contacts: {exc}")
87 ) 87 )
88 print("requesting base identities to backend") 88 log.debug("requesting base identities to backend")
89 bridge.identities_base_get( 89 bridge.identities_base_get(
90 callback=self.identities_base_get_cb, 90 callback=self.identities_base_get_cb,
91 errback=lambda e: self.request_failed(e, "Can't get base identities: {exc}") 91 errback=lambda e: self.request_failed(e, "Can't get base identities: {exc}")
92 ) 92 )
93 93
94 async def fill_identities(self, entities) -> None: 94 async def fill_identities(self, entities) -> None:
95 """Check that identities for entities exist, request them otherwise""" 95 """Check that identities for entities exist, request them otherwise"""
96 to_get = {e for e in entities if e not in self._cache['identities']} 96 to_get = {e for e in entities if e not in self._cache['identities']}
97 if to_get: 97 if to_get:
98 log.debug(f"we don't have all identities in cache, getting {to_get}")
98 try: 99 try:
99 new_identities_raw = await async_bridge.identities_get( 100 new_identities_raw = await async_bridge.identities_get(
100 list(to_get), 101 list(to_get),
101 ['avatar', 'nicknames'], 102 ['avatar', 'nicknames'],
102 ) 103 )
105 f"Can't get identities: {e}", 106 f"Can't get identities: {e}",
106 "error" 107 "error"
107 ) 108 )
108 else: 109 else:
109 new_identities = json.loads(new_identities_raw) 110 new_identities = json.loads(new_identities_raw)
110 print(f"new identities: {new_identities.keys()}") 111 log.debug(f"new identities: {new_identities.keys()}")
111 self._cache['identities'].update(new_identities) 112 self._cache['identities'].update(new_identities)
112 self.update() 113 self.update()
113 else: 114 else:
114 # we already have all identities 115 # we already have all identities
115 log.debug("no missing identity") 116 log.debug("no missing identity")
128 return True 129 return True
129 if identity is None: 130 if identity is None:
130 try: 131 try:
131 identity = self.identities[entity_jid] 132 identity = self.identities[entity_jid]
132 except KeyError: 133 except KeyError:
133 print(f"missing identity: {entity_jid}") 134 log.debug(f"missing identity: {entity_jid}")
134 return False 135 return False
135 return any(text in n.lower() for n in identity['nicknames']) 136 return any(text in n.lower() for n in identity['nicknames'])
136 137
137 def matching_identities(self, text): 138 def matching_identities(self, text):
138 """Return identities corresponding to a text 139 """Return identities corresponding to a text