1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Module implementing the parameter types code."""
23
24 from ganeti import compat
25
26
27
28
30 """Returns an empty list.
31
32 """
33 return []
34
35
37 """Returns an empty dict.
38
39 """
40 return {}
41
42
43
44 NoDefault = object()
45
46
47
48 NoType = object()
49
50
51
53 """Checks if the given value is not None.
54
55 """
56 return val is not None
57
58
60 """Checks if the given value is None.
61
62 """
63 return val is None
64
65
67 """Checks if the given value is a boolean.
68
69 """
70 return isinstance(val, bool)
71
72
74 """Checks if the given value is an integer.
75
76 """
77 return isinstance(val, int)
78
79
81 """Checks if the given value is a float.
82
83 """
84 return isinstance(val, float)
85
86
88 """Checks if the given value is a string.
89
90 """
91 return isinstance(val, basestring)
92
93
95 """Checks if a given value evaluates to a boolean True value.
96
97 """
98 return bool(val)
99
100
102 """Builds a function that checks if a given value is a member of a list.
103
104 """
105 return lambda val: val in target_list
106
107
108
110 """Checks if the given value is a list.
111
112 """
113 return isinstance(val, list)
114
115
117 """Checks if the given value is a dictionary.
118
119 """
120 return isinstance(val, dict)
121
122
124 """Check is the given container is of the given size.
125
126 """
127 return lambda container: len(container) == size
128
129
130
132 """Combine multiple functions using an AND operation.
133
134 """
135 def fn(val):
136 return compat.all(t(val) for t in args)
137 return fn
138
139
141 """Combine multiple functions using an AND operation.
142
143 """
144 def fn(val):
145 return compat.any(t(val) for t in args)
146 return fn
147
148
150 """Checks that a modified version of the argument passes the given test.
151
152 """
153 return lambda val: test(fn(val))
154
155
156
157
158
159 TNonEmptyString = TAnd(TString, TTrue)
160
161
162
163 TMaybeString = TOr(TNonEmptyString, TNone)
164
165
166
167 TMaybeBool = TOr(TBool, TNone)
168
169
170
171 TPositiveInt = TAnd(TInt, lambda v: v >= 0)
172
173
174 TStrictPositiveInt = TAnd(TInt, lambda v: v > 0)
175
176
178 """Checks if a given value is a list with all elements of the same type.
179
180 """
181 return TAnd(TList,
182 lambda lst: compat.all(my_type(v) for v in lst))
183
184
186 """Checks a dict type for the type of its key/values.
187
188 """
189 return TAnd(TDict,
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())))
193