← Back to team overview

checkbox-dev team mailing list archive

[PATCH 04/11] plainbox:secure:config: add understands_Unset decorator

 

This decorator should be applied to validators that need to handle
the special Unset value.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@xxxxxxxxxxxxx>
---
 plainbox/plainbox/impl/secure/config.py      | 14 ++++++++++++++
 plainbox/plainbox/impl/secure/test_config.py | 20 ++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/plainbox/plainbox/impl/secure/config.py b/plainbox/plainbox/impl/secure/config.py
index 9917157..25af8f6 100644
--- a/plainbox/plainbox/impl/secure/config.py
+++ b/plainbox/plainbox/impl/secure/config.py
@@ -93,6 +93,20 @@ class UnsetType:
 Unset = UnsetType()
 
 
+def understands_Unset(cls_or_func):
+    """
+    Decorator for marking validators as supporting the special Unset value.
+
+    This decorator should be applied to every validator that natively supports
+    Unset values. Without it, Unset is never validated.
+
+    This decorator works by setting the ``understands_Unset`` attribute on the
+    decorated object and returning it intact.
+    """
+    cls_or_func.understands_Unset = True
+    return cls_or_func
+
+
 class Variable(INameTracking):
     """
     Variable that can be used in a configuration systems
diff --git a/plainbox/plainbox/impl/secure/test_config.py b/plainbox/plainbox/impl/secure/test_config.py
index bace195..a6ab64b 100644
--- a/plainbox/plainbox/impl/secure/test_config.py
+++ b/plainbox/plainbox/impl/secure/test_config.py
@@ -35,6 +35,7 @@ from plainbox.impl.secure.config import NotUnsetValidator
 from plainbox.impl.secure.config import PatternValidator
 from plainbox.impl.secure.config import PlainBoxConfigParser, Config
 from plainbox.impl.secure.config import Variable, Section, Unset
+from plainbox.impl.secure.config import understands_Unset
 
 
 class UnsetTests(TestCase):
@@ -46,6 +47,25 @@ class UnsetTests(TestCase):
         self.assertEqual(repr(Unset), "Unset")
 
 
+class understands_Unset_Tests(TestCase):
+
+    def test_func(self):
+        @understands_Unset
+        def func():
+            pass
+
+        self.assertTrue(hasattr(func, 'understands_Unset'))
+        self.assertTrue(getattr(func, 'understands_Unset'))
+
+    def test_cls(self):
+        @understands_Unset
+        class cls:
+            pass
+
+        self.assertTrue(hasattr(cls, 'understands_Unset'))
+        self.assertTrue(getattr(cls, 'understands_Unset'))
+
+
 class VariableTests(TestCase):
 
     def test_name(self):
-- 
1.9.0



References