Tighten sanity checks for '_GetLvmVgSpaceInfo'
[ganeti-local] / lib / ht.py
index f558a8b..a452239 100644 (file)
--- a/lib/ht.py
+++ b/lib/ht.py
@@ -187,6 +187,14 @@ def TNone(val):
   return val is None
 
 
+@WithDesc("ValueNone")
+def TValueNone(val):
+  """Checks if the given value is L{constants.VALUE_NONE}.
+
+  """
+  return val == constants.VALUE_NONE
+
+
 @WithDesc("Boolean")
 def TBool(val):
   """Checks if the given value is a boolean.
@@ -251,6 +259,14 @@ def TList(val):
   return isinstance(val, list)
 
 
+@WithDesc("Tuple")
+def TTuple(val):
+  """Checks if the given value is a tuple.
+
+  """
+  return isinstance(val, tuple)
+
+
 @WithDesc("Dictionary")
 def TDict(val):
   """Checks if the given value is a dictionary.
@@ -281,7 +297,7 @@ def TAnd(*args):
 
 
 def TOr(*args):
-  """Combine multiple functions using an AND operation.
+  """Combine multiple functions using an OR operation.
 
   """
   def fn(val):
@@ -319,6 +335,13 @@ def TMaybe(test):
   return TOr(TNone, test)
 
 
+def TMaybeValueNone(test):
+  """Used for unsetting values.
+
+  """
+  return TMaybe(TOr(TValueNone, test))
+
+
 # Type aliases
 
 #: a non-empty string
@@ -333,6 +356,9 @@ TMaybeBool = TMaybe(TBool)
 #: Maybe a dictionary (dict or None)
 TMaybeDict = TMaybe(TDict)
 
+#: Maybe a list (list or None)
+TMaybeList = TMaybe(TList)
+
 #: a non-negative integer (value >= 0)
 TNonNegativeInt = \
   TAnd(TInt, WithDesc("EqualOrGreaterThanZero")(lambda v: v >= 0))
@@ -364,12 +390,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):