4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Module implementing the parameter types code."""
24 from ganeti import compat
26 # Modifiable default values; need to define these here before the
30 """Returns an empty list.
37 """Returns an empty dict.
43 #: The without-default default value
47 #: The no-type (value to complex to check it in the type system)
53 """Checks if the given value is not None.
56 return val is not None
60 """Checks if the given value is None.
67 """Checks if the given value is a boolean.
70 return isinstance(val, bool)
74 """Checks if the given value is an integer.
77 return isinstance(val, int)
81 """Checks if the given value is a float.
84 return isinstance(val, float)
88 """Checks if the given value is a string.
91 return isinstance(val, basestring)
95 """Checks if a given value evaluates to a boolean True value.
101 def TElemOf(target_list):
102 """Builds a function that checks if a given value is a member of a list.
105 return lambda val: val in target_list
110 """Checks if the given value is a list.
113 return isinstance(val, list)
117 """Checks if the given value is a dictionary.
120 return isinstance(val, dict)
124 """Check is the given container is of the given size.
127 return lambda container: len(container) == size
132 """Combine multiple functions using an AND operation.
136 return compat.all(t(val) for t in args)
141 """Combine multiple functions using an AND operation.
145 return compat.any(t(val) for t in args)
150 """Checks that a modified version of the argument passes the given test.
153 return lambda val: test(fn(val))
158 #: a non-empty string
159 TNonEmptyString = TAnd(TString, TTrue)
162 #: a maybe non-empty string
163 TMaybeString = TOr(TNonEmptyString, TNone)
166 #: a maybe boolean (bool or none)
167 TMaybeBool = TOr(TBool, TNone)
170 #: a positive integer
171 TPositiveInt = TAnd(TInt, lambda v: v >= 0)
173 #: a strictly positive integer
174 TStrictPositiveInt = TAnd(TInt, lambda v: v > 0)
177 def TListOf(my_type):
178 """Checks if a given value is a list with all elements of the same type.
182 lambda lst: compat.all(my_type(v) for v in lst))
185 def TDictOf(key_type, val_type):
186 """Checks a dict type for the type of its key/values.
190 lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys())
191 and compat.all(val_type(v)
192 for v in my_dict.values())))