[diagnostic] Allow to specify custom arguments on CompatError instantation
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 02 Nov 2017 20:55:49 +0100
changeset 2828 4921e19f5e62
parent 2827 0f1331287a4c
child 2829 5c0fd172aa59
[diagnostic] Allow to specify custom arguments on CompatError instantation that will override rule's default settings. Related to #39302963
cubicweb_seda/entities/diag.py
--- a/cubicweb_seda/entities/diag.py	Thu Nov 02 20:44:54 2017 +0100
+++ b/cubicweb_seda/entities/diag.py	Thu Nov 02 20:55:49 2017 +0100
@@ -1,4 +1,4 @@
-# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -120,10 +120,13 @@
     * `entity`: 1st class entity where the error lies (one of archive unit, data object, etc.),
     * `tab`: entity's tab where the problem may be fixed.
     """
-    def __new__(cls, rule_id, entity):
+    def __new__(cls, rule_id, entity, **kwargs):
         rule = RULES[rule_id]
-        return super(CompatError, cls).__new__(cls, rule.impacted_formats, rule.message,
-                                               rule.tab_id, entity)
+        impacted_formats = kwargs.get('impacted_formats', rule.impacted_formats)
+        message = kwargs.get('message', rule.message)
+        tab_id = kwargs.get('tab_id', rule.tab_id)
+        return super(CompatError, cls).__new__(
+            cls, impacted_formats, message, tab_id, entity)
 
 
 class ISEDACompatAnalyzer(EntityAdapter):
@@ -144,8 +147,10 @@
         """Yield :class:`CompatError` describing a problem that prevents the given profile to be
         compatible with some format.
         """
-        for rule_id, entity in self.failing_rules():
-            yield CompatError(rule_id, entity)
+        for args in self.failing_rules():
+            assert len(args) in (2, 3)
+            extra_kwargs = args[2] if len(args) == 3 else {}
+            yield CompatError(*args[:2], **extra_kwargs)
 
     def failing_rules(self):
         """Yield (rule identifier, problematic entity) describing a problem that prevents the given