From fdb8b193bf8381eaf0d5f144567297191be6f8d6 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 21 Dec 2021 15:42:57 +0100
Subject: [PATCH] L2SS-544: Wrap repeating check to swallow exceptions that
 indicate we stop/remove an attribute that is not being archived.

---
 .../tangostationcontrol/toolkit/archiver.py   | 57 +++++++++++--------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
index 7a62c18fd..715ca039d 100644
--- a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
+++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
@@ -55,6 +55,27 @@ def split_tango_name(tango_fqname:str, tango_type:str):
     else:
         raise ValueError(f"Invalid value: {tango_type}. Please provide 'device' or 'attribute'.")
 
+ 
+def warn_if_attribute_not_found():
+    """
+      Log a warning if an exception is thrown indicating access to an non-existing attribute
+      was requested, and swallow the exception.
+    """
+    def inner(func):
+        @wraps(func)
+        def warn_wrapper(self, *args, **kwargs):
+            try:
+                return func(self, *args, **kwargs)
+            except DevFailed as e:
+                if e.args[0].reason == 'Attribute not found':
+                    logger.warning(f"Attribute {attribute_name} not found!")
+                else:
+                    raise
+
+        return warn_wrapper
+
+    return inner
+
 class Archiver():
     """
     The Archiver class implements the basic operations to perform attributes archiving
@@ -240,21 +261,17 @@ class Archiver():
                     raise Exception from e                        
             else:
                 logger.warning(f"Attribute {attr_fullname} will not be archived because polling is set to FALSE!")
-    
+
+    @warn_if_attribute_not_found() 
     def remove_attribute_from_archiver(self, attribute_name:str):
         """
         Stops the data archiving of the attribute passed as input, and remove it from the subscriber's list. 
         """
         attribute_name = attribute_name_from_url(attribute_name)
-        try:
-            self.cm.AttributeStop(attribute_name)
-            self.cm.AttributeRemove(attribute_name)
-            logger.warning(f"Attribute {attribute_name} removed!")
-        except DevFailed as e:
-            if e.args[0].reason == 'Attribute not found':
-                logger.warning(f"Attribute {attribute_name} not found!")
-            else:
-                raise
+
+        self.cm.AttributeStop(attribute_name)
+        self.cm.AttributeRemove(attribute_name)
+        logger.warning(f"Attribute {attribute_name} removed!")
     
     def remove_attributes_by_device(self,device_name:str,exclude:list=[]):
         """
@@ -289,33 +306,25 @@ class Archiver():
                 attr_fullname = attribute_name_from_url(a)
                 self.remove_attribute_from_archiver(attr_fullname)
 
+    @warn_if_attribute_not_found()
     def start_archiving_attribute(self, attribute_name:str):
         """
         Starts the archiving of the attribute passed as input.
         The attribute must be already present in the subscriber's list
         """
         attribute_name = attribute_name_from_url(attribute_name)
-        try:
-            self.cm.AttributeStart(attribute_name)
-        except DevFailed as e:
-            if e.args[0].reason == "Attribute not found":
-                logger.warning(f"Attribute {attribute_name} not found!")
-            else:
-                raise
+
+        self.cm.AttributeStart(attribute_name)
     
+    @warn_if_attribute_not_found()
     def stop_archiving_attribute(self, attribute_name:str):
         """
         Stops the archiving of the attribute passed as input.
         The attribute must be already present in the subscriber's list
         """
         attribute_name = attribute_name_from_url(attribute_name)
-        try:
-            self.cm.AttributeStop(attribute_name)
-        except DevFailed as e:
-            if e.args[0].reason == "Attribute not found":
-                logger.warning(f"Attribute {attribute_name} not found!")
-            else:
-                raise
+
+        self.cm.AttributeStop(attribute_name)
     
     def is_attribute_archived(self,attribute_name:str):
         """
-- 
GitLab