This patch adds descriptors to the “_CheckCIDR*” functions in opcodes
and improves the descriptions generated by “ht.TInstanceOf”, thereby
indirectly fixing bad type descriptions in the RAPI documentation.

Before this patch:
- (String and (<function _CheckCIDRAddrNotation at 0x2f…>))
- (Instance of (<class 'ganeti.opcodes.OpInstanceCreate'>))

After this patch:
- (String and (IPv4 address))
- (Instance of (ganeti.opcodes.OpInstanceCreate))

Signed-off-by: Michael Hanselmann <[email protected]>
---
 lib/ht.py                  |   14 ++++++++++----
 lib/opcodes.py             |   12 ++++++++----
 test/ganeti.ht_unittest.py |    7 +++++++
 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/lib/ht.py b/lib/ht.py
index f558a8b..85d3b82 100644
--- a/lib/ht.py
+++ b/lib/ht.py
@@ -364,12 +364,18 @@ TNumber = TOr(TInt, TFloat)
 TRelativeJobId = WithDesc("RelativeJobId")(TNegativeInt)
 
 
-def TInstanceOf(my_inst):
-  """Checks if a given value is an instance of my_inst.
+def TInstanceOf(cls):
+  """Checks if a given value is an instance of C{cls}.
+
+  @type cls: class
+  @param cls: Class object
 
   """
-  desc = WithDesc("Instance of %s" % (Parens(my_inst), ))
-  return desc(lambda val: isinstance(val, my_inst))
+  name = "%s.%s" % (cls.__module__, cls.__name__)
+
+  desc = WithDesc("Instance of %s" % (Parens(name), ))
+
+  return desc(lambda val: isinstance(val, cls))
 
 
 def TListOf(my_type):
diff --git a/lib/opcodes.py b/lib/opcodes.py
index 9a9c956..2fae23f 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -359,8 +359,9 @@ _PNetworkType = ("network_type", None, 
ht.TMaybe(_CheckNetworkType),
                  "Network type")
 
 
[email protected]("IPv4 network")
 def _CheckCIDRNetNotation(value):
-  """Ensure a given cidr notation type is valid.
+  """Ensure a given CIDR notation type is valid.
 
   """
   try:
@@ -370,8 +371,9 @@ def _CheckCIDRNetNotation(value):
   return True
 
 
[email protected]("IPv4 address")
 def _CheckCIDRAddrNotation(value):
-  """Ensure a given cidr notation type is valid.
+  """Ensure a given CIDR notation type is valid.
 
   """
   try:
@@ -381,8 +383,9 @@ def _CheckCIDRAddrNotation(value):
   return True
 
 
[email protected]("IPv6 address")
 def _CheckCIDR6AddrNotation(value):
-  """Ensure a given cidr notation type is valid.
+  """Ensure a given CIDR notation type is valid.
 
   """
   try:
@@ -392,8 +395,9 @@ def _CheckCIDR6AddrNotation(value):
   return True
 
 
[email protected]("IPv6 network")
 def _CheckCIDR6NetNotation(value):
-  """Ensure a given cidr notation type is valid.
+  """Ensure a given CIDR notation type is valid.
 
   """
   try:
diff --git a/test/ganeti.ht_unittest.py b/test/ganeti.ht_unittest.py
index e60a750..8e7b910 100755
--- a/test/ganeti.ht_unittest.py
+++ b/test/ganeti.ht_unittest.py
@@ -275,6 +275,13 @@ class TestTypeChecks(unittest.TestCase):
     self.assertTrue(fn([{}, -938210858947, None]))
     self.assertFalse(fn(["Hello", []]))
 
+  def testInstanceOf(self):
+    fn = ht.TInstanceOf(self.__class__)
+    self.assertTrue(fn(self))
+    self.assertTrue(str(fn).startswith("Instance of "))
+
+    self.assertFalse(fn(None))
+
 
 if __name__ == "__main__":
   testutils.GanetiTestProgram()
-- 
1.7.7.3


Reply via email to