Add helper for declaring all locks shared
[ganeti-local] / lib / ht.py
index d5ae544..ff9d6da 100644 (file)
--- a/lib/ht.py
+++ b/lib/ht.py
@@ -22,6 +22,7 @@
 """Module implementing the parameter types code."""
 
 import re
+import operator
 
 from ganeti import compat
 from ganeti import utils
@@ -45,6 +46,29 @@ def Parens(text):
     return "(%s)" % text
 
 
+class _DescWrapper(object):
+  __slots__ = [
+    "_fn",
+    "_text",
+    ]
+
+  def __init__(self, text, fn):
+    """Initializes this class.
+
+    @param text: Description
+    @param fn: Wrapped function
+
+    """
+    self._text = text
+    self._fn = fn
+
+  def __call__(self, *args):
+    return self._fn(*args)
+
+  def __str__(self):
+    return self._text
+
+
 def WithDesc(text):
   """Builds wrapper class with description text.
 
@@ -55,21 +79,7 @@ def WithDesc(text):
   """
   assert text[0] == text[0].upper()
 
-  class wrapper(object): # pylint: disable-msg=C0103
-    __slots__ = ["__call__"]
-
-    def __init__(self, fn):
-      """Initializes this class.
-
-      @param fn: Wrapped function
-
-      """
-      self.__call__ = fn
-
-    def __str__(self):
-      return text
-
-  return wrapper
+  return compat.partial(_DescWrapper, text)
 
 
 def CombinationDesc(op, args, fn):
@@ -161,7 +171,7 @@ def TInt(val):
   #
   # >>> (isinstance(False, int), isinstance(True, int))
   # (True, True)
-  return isinstance(val, int) and not isinstance(val, bool)
+  return isinstance(val, (int, long)) and not isinstance(val, bool)
 
 
 @WithDesc("Float")
@@ -288,6 +298,10 @@ TPositiveInt = \
 TStrictPositiveInt = \
   TAnd(TInt, WithDesc("GreaterThanZero")(lambda v: v > 0))
 
+#: a strictly negative integer (0 > value)
+TStrictNegativeInt = \
+  TAnd(TInt, WithDesc("LessThanZero")(compat.partial(operator.gt, 0)))
+
 #: a positive float
 TPositiveFloat = \
   TAnd(TFloat, WithDesc("EqualGreaterZero")(lambda v: v >= 0.0))
@@ -296,6 +310,12 @@ TPositiveFloat = \
 TJobId = TOr(TPositiveInt,
              TRegex(re.compile("^%s$" % constants.JOB_ID_TEMPLATE)))
 
+#: Number
+TNumber = TOr(TInt, TFloat)
+
+#: Relative job ID
+TRelativeJobId = WithDesc("RelativeJobId")(TStrictNegativeInt)
+
 
 def TListOf(my_type):
   """Checks if a given value is a list with all elements of the same type.