dara-core 1.16.13__py3-none-any.whl → 1.16.15__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dara/core/logging.py CHANGED
@@ -254,13 +254,14 @@ class DaraDevFormatter(logging.Formatter):
254
254
  'INFO': (Back.GREEN, Fore.GREEN),
255
255
  'WARNING': (Back.YELLOW, Fore.YELLOW),
256
256
  }
257
+ default_color = (Back.GREEN, Fore.GREEN)
257
258
 
258
259
  def format(self, record: logging.LogRecord):
260
+ colors = self.level_to_color_map.get(record.levelname, self.default_color)
259
261
  if isinstance(record.msg, dict):
260
262
  payload = {**record.msg}
261
263
  fmt_time = self.formatTime(record, self.datefmt)
262
264
  spacer = ' ' * 4
263
- colors = self.level_to_color_map[record.levelname]
264
265
  base_msg = self.base_message_template % (
265
266
  fmt_time,
266
267
  colors[0],
@@ -303,7 +304,6 @@ class DaraDevFormatter(logging.Formatter):
303
304
  description = self.extra_template % (spacer, f'Description: {payload["description"]}')
304
305
  return base_msg + '\r\n' + file_details + description + content + '\r\n' + self.message_end
305
306
 
306
- colors = self.level_to_color_map[record.levelname]
307
307
  return self.base_message_template % (
308
308
  self.formatTime(record, self.datefmt),
309
309
  colors[0],
dara/core/persistence.py CHANGED
@@ -18,6 +18,7 @@ from pydantic import (
18
18
  from dara.core.auth.definitions import USER
19
19
  from dara.core.internal.utils import run_user_handler
20
20
  from dara.core.internal.websocket import WS_CHANNEL
21
+ from dara.core.logging import dev_logger
21
22
 
22
23
  if TYPE_CHECKING:
23
24
  from dara.core.interactivity.plain_variable import Variable
@@ -264,7 +265,9 @@ class BackendStore(PersistenceStore):
264
265
  def _register(self):
265
266
  """
266
267
  Register this store in the backend store registry.
267
- Raises ValueError if the uid is not unique, i.e. another store with the same uid already exists
268
+ Warns if the uid is not unique, i.e. another store with the same uid already exists.
269
+
270
+ :return: True if the store was registered, False if it was already registered previously
268
271
  """
269
272
  from dara.core.internal.registries import backend_store_registry
270
273
 
@@ -276,8 +279,10 @@ class BackendStore(PersistenceStore):
276
279
  store=self,
277
280
  ),
278
281
  )
279
- except ValueError as e:
280
- raise ValueError('BackendStore uid must be unique') from e
282
+ return True
283
+ except ValueError:
284
+ dev_logger.info(f'BackendStore with uid "{self.uid}" already exists, reusing the same instance')
285
+ return False
281
286
 
282
287
  @property
283
288
  def ws_mgr(self) -> 'WebsocketManager':
@@ -292,26 +297,30 @@ class BackendStore(PersistenceStore):
292
297
  """
293
298
  return {'store_uid': self.uid, 'value': value}
294
299
 
295
- async def _notify_user(self, user_identifier: str, value: Any):
300
+ async def _notify_user(self, user_identifier: str, value: Any, ignore_current_channel: bool = True):
296
301
  """
297
302
  Notify a given user about the new value for this store.
303
+
298
304
  :param user_identifier: user to notify
299
305
  :param value: value to notify about
306
+ :param ignore_current_channel: if True, ignore the current websocket channel
300
307
  """
301
308
  return await self.ws_mgr.send_message_to_user(
302
309
  user_identifier,
303
310
  self._create_msg(value),
304
- ignore_channel=WS_CHANNEL.get(),
311
+ ignore_channel=WS_CHANNEL.get() if ignore_current_channel else None,
305
312
  )
306
313
 
307
- async def _notify_global(self, value: Any):
314
+ async def _notify_global(self, value: Any, ignore_current_channel: bool = True):
308
315
  """
309
316
  Notify all users about the new value for this store.
317
+
310
318
  :param value: value to notify about
319
+ :param ignore_current_channel: if True, ignore the current websocket channel
311
320
  """
312
321
  return await self.ws_mgr.broadcast(
313
322
  self._create_msg(value),
314
- ignore_channel=WS_CHANNEL.get(),
323
+ ignore_channel=WS_CHANNEL.get() if ignore_current_channel else None,
315
324
  )
316
325
 
317
326
  async def _notify_value(self, value: Any):
@@ -339,15 +348,18 @@ class BackendStore(PersistenceStore):
339
348
 
340
349
  :param variable: the variable to initialize the store for
341
350
  """
342
- self._register()
343
351
  self.default_value = variable.default
344
352
 
345
- async def _on_value(key: str, value: Any):
346
- if user := self._get_user(key):
347
- return await self._notify_user(user, value)
348
- return await self._notify_global(value)
353
+ # only if successfully registered, subscribe to the backend - this makes sure we do it once
354
+ if self._register():
355
+
356
+ async def _on_value(key: str, value: Any):
357
+ # here we explicitly DON'T ignore the current channel, in case we created this variable inside e.g. a py_component we want to notify its creator as well
358
+ if user := self._get_user(key):
359
+ return await self._notify_user(user, value, ignore_current_channel=False)
360
+ return await self._notify_global(value, ignore_current_channel=False)
349
361
 
350
- await self.backend.subscribe(_on_value)
362
+ await self.backend.subscribe(_on_value)
351
363
 
352
364
  async def write(self, value: Any, notify=True):
353
365
  """