From 0ab3e5493868a9c51f43be902eedab1aff80a908 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Tue, 27 Jun 2023 10:59:50 +0200
Subject: [PATCH] up

---
 src/caosdb/common/models.py | 50 ++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py
index 9a94bf96..a3159821 100644
--- a/src/caosdb/common/models.py
+++ b/src/caosdb/common/models.py
@@ -2465,6 +2465,7 @@ class Messages(list):
 
     def clear_server_messages(self):
         """Removes all messages of type error, warning and info."""
+        # TODO Why do we not remove ALL messages?
         rem = []
 
         for m in self:
@@ -2474,6 +2475,9 @@ class Messages(list):
         for m in rem:
             self.remove(m)
 
+    #######################################################################
+    # can be removed after 01.07.24
+    # default implementation of list is sufficient
     def __setitem__(self, key, value):  # @ReservedAssignment
         if not isinstance(value, Message):
             warn("__setitem__ will in future only accept Message objects as second argument. "
@@ -2537,7 +2541,7 @@ class Messages(list):
             else:
                 raise TypeError(
                     "('type', 'code'), ('type'), or 'type' expected.")
-        elif isinstance(key, int) and int(key) >= 0:
+        elif isinstance(key, int) and key >= 0:
             return super().__getitem__(key)
         else:
             type = key  # @ReservedAssignment
@@ -2566,15 +2570,6 @@ class Messages(list):
         else:
             super().remove(obj)
 
-    def get(self, type, code=None, default=None):  # @ReservedAssignment
-        for msg in self:
-            if msg.type == type and msg.code == code:
-                return msg
-        return default
-
-    def extend(self, messages):
-        super().extend(messages)
-
     def append(self, msg):
         if isinstance(msg, Messages) or isinstance(msg, list):
             warn("Supplying a list-like object to append is deprecated. Please use extend"
@@ -2585,6 +2580,34 @@ class Messages(list):
 
         super().append(msg)
 
+    def _hash(t, c):
+        return hash(str(t).lower() + (str(",") + str(c) if c is not None else ''))
+    # end remove
+    #######################################################################
+
+    def get(self, type, code=None, default=None, exact=False):  # @ReservedAssignment
+        """
+        returns a message from the list that kind of matches type and code
+
+        case and types (str/int) are ignored
+
+        If no suitable message is found, the default argument is returned
+        If exact=True, the message has to match code and type exactly
+        """
+        if not exact:
+            warn("The fuzzy mode (exact=False) is deprecated. Please use exact in future.",
+                 DeprecationWarning)
+
+        for msg in self:
+            if exact:
+                if self._hash(msg.t, msg.c) == self._hash(type, code):
+                    return msg
+            else:
+                if msg.type == type and msg.code == code:
+                    return msg
+
+        return default
+
     def to_xml(self, add_to_element):
         for m in self:
             melem = m.to_xml()
@@ -2596,6 +2619,8 @@ class Messages(list):
 
         return xml2str(xml)
 
+    #######################################################################
+    # can be removed after 01.07.24
     class _msg_key:
 
         def __init__(self, type, code):  # @ReservedAssignment
@@ -2611,12 +2636,13 @@ class Messages(list):
             return self.__hash__() == obj.__hash__()
 
         def __hash__(self):
-            return hash(str(self._type).lower() + (str(",") +
-                                                   str(self._code) if self._code is not None else ''))
+            return
 
         def __repr__(self):
             return str(self._type) + (str(",") + str(self._code)
                                       if self._code is not None else '')
+    # end remove
+    #######################################################################
 
 
 class _Messages(Messages):
-- 
GitLab