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