Package ganeti :: Module ht
[hide private]
[frames] | no frames]

Source Code for Module ganeti.ht

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2010 Google Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, but 
 12  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 14  # General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 19  # 02110-1301, USA. 
 20   
 21   
 22  """Module implementing the parameter types code.""" 
 23   
 24  from ganeti import compat 
 25   
 26  # Modifiable default values; need to define these here before the 
 27  # actual LUs 
 28   
29 -def EmptyList():
30 """Returns an empty list. 31 32 """ 33 return []
34 35
36 -def EmptyDict():
37 """Returns an empty dict. 38 39 """ 40 return {}
41 42 43 #: The without-default default value 44 NoDefault = object() 45 46 47 #: The no-type (value to complex to check it in the type system) 48 NoType = object() 49 50 51 # Some basic types
52 -def TNotNone(val):
53 """Checks if the given value is not None. 54 55 """ 56 return val is not None
57 58
59 -def TNone(val):
60 """Checks if the given value is None. 61 62 """ 63 return val is None
64 65
66 -def TBool(val):
67 """Checks if the given value is a boolean. 68 69 """ 70 return isinstance(val, bool)
71 72
73 -def TInt(val):
74 """Checks if the given value is an integer. 75 76 """ 77 # For backwards compatibility with older Python versions, boolean values are 78 # also integers and should be excluded in this test. 79 # 80 # >>> (isinstance(False, int), isinstance(True, int)) 81 # (True, True) 82 return isinstance(val, int) and not isinstance(val, bool)
83 84
85 -def TFloat(val):
86 """Checks if the given value is a float. 87 88 """ 89 return isinstance(val, float)
90 91
92 -def TString(val):
93 """Checks if the given value is a string. 94 95 """ 96 return isinstance(val, basestring)
97 98
99 -def TTrue(val):
100 """Checks if a given value evaluates to a boolean True value. 101 102 """ 103 return bool(val)
104 105
106 -def TElemOf(target_list):
107 """Builds a function that checks if a given value is a member of a list. 108 109 """ 110 return lambda val: val in target_list
111 112 113 # Container types
114 -def TList(val):
115 """Checks if the given value is a list. 116 117 """ 118 return isinstance(val, list)
119 120
121 -def TDict(val):
122 """Checks if the given value is a dictionary. 123 124 """ 125 return isinstance(val, dict)
126 127
128 -def TIsLength(size):
129 """Check is the given container is of the given size. 130 131 """ 132 return lambda container: len(container) == size
133 134 135 # Combinator types
136 -def TAnd(*args):
137 """Combine multiple functions using an AND operation. 138 139 """ 140 def fn(val): 141 return compat.all(t(val) for t in args)
142 return fn 143 144
145 -def TOr(*args):
146 """Combine multiple functions using an AND operation. 147 148 """ 149 def fn(val): 150 return compat.any(t(val) for t in args)
151 return fn 152 153
154 -def TMap(fn, test):
155 """Checks that a modified version of the argument passes the given test. 156 157 """ 158 return lambda val: test(fn(val))
159 160 161 # Type aliases 162 163 #: a non-empty string 164 TNonEmptyString = TAnd(TString, TTrue) 165 166 #: a maybe non-empty string 167 TMaybeString = TOr(TNonEmptyString, TNone) 168 169 #: a maybe boolean (bool or none) 170 TMaybeBool = TOr(TBool, TNone) 171 172 #: Maybe a dictionary (dict or None) 173 TMaybeDict = TOr(TDict, TNone) 174 175 #: a positive integer 176 TPositiveInt = TAnd(TInt, lambda v: v >= 0) 177 178 #: a strictly positive integer 179 TStrictPositiveInt = TAnd(TInt, lambda v: v > 0) 180 181 #: Number 182 TNumber = TOr(TInt, TFloat) 183 184
185 -def TListOf(my_type):
186 """Checks if a given value is a list with all elements of the same type. 187 188 """ 189 return TAnd(TList, 190 lambda lst: compat.all(my_type(v) for v in lst))
191 192
193 -def TDictOf(key_type, val_type):
194 """Checks a dict type for the type of its key/values. 195 196 """ 197 return TAnd(TDict, 198 lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys()) 199 and compat.all(val_type(v) 200 for v in my_dict.values())))
201