def mime_data_get_data_source(mime_data: UserInterface.MimeData, document_model: DocumentModel.DocumentModel) -> typing.Tuple[typing.Optional[DisplayItem.DisplayItem], typing.Optional[Graphics.Graphic]]: display_item = None graphic = None if mime_data.has_format(DATA_SOURCE_MIME_TYPE): data_source_mime_data = json.loads(mime_data.data_as_string(DATA_SOURCE_MIME_TYPE)) display_item_specifier = Persistence.read_persistent_specifier(data_source_mime_data["display_item_specifier"]) display_item = typing.cast(typing.Optional[DisplayItem.DisplayItem], document_model.resolve_item_specifier(display_item_specifier)) if display_item_specifier else None if "graphic_specifier" in data_source_mime_data: graphic_specifier = Persistence.read_persistent_specifier(data_source_mime_data["graphic_specifier"]) graphic = typing.cast(typing.Optional[Graphics.Graphic], document_model.resolve_item_specifier(graphic_specifier)) if graphic_specifier else None return display_item, graphic
def mime_data_get_display_items(mime_data: UserInterface.MimeData, document_model: DocumentModel.DocumentModel) -> typing.List[DisplayItem.DisplayItem]: display_items : typing.List[DisplayItem.DisplayItem] = list() if mime_data.has_format(DISPLAY_ITEMS_MIME_TYPE): data_sources_mime_data = json.loads(mime_data.data_as_string(DISPLAY_ITEMS_MIME_TYPE)) for data_source_mime_data in data_sources_mime_data: display_item_specifier = Persistence.read_persistent_specifier(data_source_mime_data["display_item_specifier"]) display_item = typing.cast(typing.Optional[DisplayItem.DisplayItem], document_model.resolve_item_specifier(display_item_specifier)) if display_item_specifier else None if display_item: display_items.append(display_item) if mime_data.has_format(DISPLAY_ITEM_MIME_TYPE): data_source_mime_data = json.loads(mime_data.data_as_string(DISPLAY_ITEM_MIME_TYPE)) display_item_specifier = Persistence.read_persistent_specifier(data_source_mime_data["display_item_specifier"]) display_item = typing.cast(typing.Optional[DisplayItem.DisplayItem], document_model.resolve_item_specifier(display_item_specifier)) if display_item_specifier else None if display_item: display_items.append(display_item) return display_items
def test_persistent_object_proxy_does_not_call_listener_when_removed_from_unregistered_parent(self): r_count = 0 u_count = 0 def r(x): nonlocal r_count; r_count += 1 def u(x): nonlocal u_count; u_count += 1 persistent_object_context = Persistence.PersistentObjectContext() object0 = Persistence.PersistentObject() object1 = Persistence.PersistentObject() object1_proxy = object0.create_item_proxy(item_specifier=Persistence.read_persistent_specifier(object1.uuid)) with contextlib.closing(object0), contextlib.closing(object1), contextlib.closing(object1_proxy): object1_proxy.on_item_registered = r object1_proxy.on_item_unregistered = u object0.persistent_object_context = persistent_object_context # only parent has been registered self.assertEqual(0, r_count) self.assertEqual(0, u_count) object1.persistent_object_context = persistent_object_context # now object has been registered self.assertEqual(object1, object1_proxy.item) self.assertEqual(1, r_count) self.assertEqual(0, u_count) # unregistered parent first, then object object0.persistent_object_context = None object1.persistent_object_context = None self.assertEqual(1, r_count) self.assertEqual(0, u_count) # parent was already unregistered
def test_persistent_object_proxy_not_registered_when_already_registered(self): r_count = 0 u_count = 0 def r(x): nonlocal r_count; r_count += 1 def u(x): nonlocal u_count; u_count += 1 persistent_object_context = Persistence.PersistentObjectContext() object0 = Persistence.PersistentObject() object1 = Persistence.PersistentObject() object0.persistent_object_context = persistent_object_context object1.persistent_object_context = persistent_object_context # both objects are already registered object1_proxy = object0.create_item_proxy(item_specifier=Persistence.read_persistent_specifier(object1.uuid)) with contextlib.closing(object0), contextlib.closing(object1), contextlib.closing(object1_proxy): object1_proxy.on_item_registered = r object1_proxy.on_item_unregistered = u # registered should not be called because it was already registered self.assertEqual(object1, object1_proxy.item) self.assertEqual(0, r_count) self.assertEqual(0, u_count) object1.persistent_object_context = None # but unregistered should be called self.assertEqual(0, r_count) self.assertEqual(1, u_count)
def test_persistent_object_proxy_calls_listeners_once_during_lifecycle(self): r_count = 0 u_count = 0 def r(x): nonlocal r_count; r_count += 1 def u(x): nonlocal u_count; u_count += 1 persistent_object_context = Persistence.PersistentObjectContext() object0 = Persistence.PersistentObject() object1 = Persistence.PersistentObject() object1_proxy = object0.create_item_proxy(item_specifier=Persistence.read_persistent_specifier(object1.uuid)) with contextlib.closing(object0), contextlib.closing(object1), contextlib.closing(object1_proxy): object1_proxy.on_item_registered = r object1_proxy.on_item_unregistered = u # register parent, but not child object0.persistent_object_context = persistent_object_context self.assertEqual(0, r_count) self.assertEqual(0, u_count) # now register child and ensure register was called object1.persistent_object_context = persistent_object_context self.assertEqual(object1, object1_proxy.item) self.assertEqual(1, r_count) self.assertEqual(0, u_count) # unregister child and ensure unregistered was called object1.persistent_object_context = None self.assertEqual(1, r_count) self.assertEqual(1, u_count)
def mime_data_get_panel(mime_data: UserInterface.MimeData, document_model: DocumentModel.DocumentModel) -> typing.Tuple[typing.Optional[DisplayItem.DisplayItem], Persistence.PersistentDictType]: display_item = None d: Persistence.PersistentDictType = dict() if mime_data.has_format(DISPLAY_PANEL_MIME_TYPE): d = json.loads(mime_data.data_as_string(DISPLAY_PANEL_MIME_TYPE)) if "display_item_specifier" in d: display_item_specifier = Persistence.read_persistent_specifier(d["display_item_specifier"]) display_item = typing.cast(typing.Optional[DisplayItem.DisplayItem], document_model.resolve_item_specifier(display_item_specifier)) if display_item_specifier else None return display_item, d
def test_persistent_object_proxy_updates_when_registered(self): persistent_object_context = Persistence.PersistentObjectContext() object0 = Persistence.PersistentObject() object1 = Persistence.PersistentObject() object1_proxy = object0.create_item_proxy(item_specifier=Persistence.read_persistent_specifier(object1.uuid)) with contextlib.closing(object0), contextlib.closing(object1), contextlib.closing(object1_proxy): object0.persistent_object_context = persistent_object_context self.assertIsNone(object1_proxy.item) object1.persistent_object_context = persistent_object_context self.assertEqual(object1, object1_proxy.item)
def __configure_reference_proxy( self, property_name: str, value: typing.Any, item: typing.Optional[Persistence.PersistentObject]) -> None: if isinstance(value, dict) and value.get("type") in { "data_item", "display_item", "data_source", "graphic", "structure" } and "uuid" in value: self.__referenced_object_proxies[ property_name] = self.create_item_proxy( item_specifier=Persistence.read_persistent_specifier( value["uuid"]), item=item)
def test_deep_copy_should_not_deep_copy_data_items(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_group = DataGroup.DataGroup() document_model.append_data_group(data_group) data_item = DataItem.DataItem(numpy.zeros((8, 8), numpy.uint32)) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) data_group.append_display_item(display_item) data_group_copy = copy.deepcopy(data_group) document_model.append_data_group(data_group_copy) display_item_specifier = Persistence.read_persistent_specifier( data_group_copy.display_item_specifiers[0]) self.assertEqual( display_item, document_model.resolve_item_specifier(display_item_specifier))
def mime_data_get_layer(mime_data: UserInterface.MimeData, document_model: DocumentModel.DocumentModel) -> typing.Tuple[Persistence.PersistentDictType, typing.Optional[DisplayItem.DisplayItem]]: mime_dict = json.loads(mime_data.data_as_string(LAYER_MIME_TYPE)) legend_data = mime_dict["legend_data"] display_item_specifier = Persistence.read_persistent_specifier(mime_dict["display_item_specifier"]) display_item = typing.cast(typing.Optional[DisplayItem.DisplayItem], document_model.resolve_item_specifier(display_item_specifier)) if display_item_specifier else None return legend_data, display_item
def __source_specifier_changed(self, name: str, d: Persistence._SpecifierType) -> None: self.__source_reference.item_specifier = Persistence.read_persistent_specifier( d)