Statistics
| Branch: | Tag: | Revision:

root / lib / ht.py @ 82599b3e

History | View | Annotate | Download (3.9 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 8568de9e Michael Hanselmann
  # For backwards compatibility with older Python versions, boolean values are
78 8568de9e Michael Hanselmann
  # also integers and should be excluded in this test.
79 8568de9e Michael Hanselmann
  #
80 8568de9e Michael Hanselmann
  # >>> (isinstance(False, int), isinstance(True, int))
81 8568de9e Michael Hanselmann
  # (True, True)
82 8568de9e Michael Hanselmann
  return isinstance(val, int) and not isinstance(val, bool)
83 62e0e880 Iustin Pop
84 62e0e880 Iustin Pop
85 62e0e880 Iustin Pop
def TFloat(val):
86 62e0e880 Iustin Pop
  """Checks if the given value is a float.
87 62e0e880 Iustin Pop

88 62e0e880 Iustin Pop
  """
89 62e0e880 Iustin Pop
  return isinstance(val, float)
90 62e0e880 Iustin Pop
91 62e0e880 Iustin Pop
92 62e0e880 Iustin Pop
def TString(val):
93 62e0e880 Iustin Pop
  """Checks if the given value is a string.
94 62e0e880 Iustin Pop

95 62e0e880 Iustin Pop
  """
96 62e0e880 Iustin Pop
  return isinstance(val, basestring)
97 62e0e880 Iustin Pop
98 62e0e880 Iustin Pop
99 62e0e880 Iustin Pop
def TTrue(val):
100 62e0e880 Iustin Pop
  """Checks if a given value evaluates to a boolean True value.
101 62e0e880 Iustin Pop

102 62e0e880 Iustin Pop
  """
103 62e0e880 Iustin Pop
  return bool(val)
104 62e0e880 Iustin Pop
105 62e0e880 Iustin Pop
106 62e0e880 Iustin Pop
def TElemOf(target_list):
107 62e0e880 Iustin Pop
  """Builds a function that checks if a given value is a member of a list.
108 62e0e880 Iustin Pop

109 62e0e880 Iustin Pop
  """
110 62e0e880 Iustin Pop
  return lambda val: val in target_list
111 62e0e880 Iustin Pop
112 62e0e880 Iustin Pop
113 62e0e880 Iustin Pop
# Container types
114 62e0e880 Iustin Pop
def TList(val):
115 62e0e880 Iustin Pop
  """Checks if the given value is a list.
116 62e0e880 Iustin Pop

117 62e0e880 Iustin Pop
  """
118 62e0e880 Iustin Pop
  return isinstance(val, list)
119 62e0e880 Iustin Pop
120 62e0e880 Iustin Pop
121 62e0e880 Iustin Pop
def TDict(val):
122 62e0e880 Iustin Pop
  """Checks if the given value is a dictionary.
123 62e0e880 Iustin Pop

124 62e0e880 Iustin Pop
  """
125 62e0e880 Iustin Pop
  return isinstance(val, dict)
126 62e0e880 Iustin Pop
127 62e0e880 Iustin Pop
128 62e0e880 Iustin Pop
def TIsLength(size):
129 62e0e880 Iustin Pop
  """Check is the given container is of the given size.
130 62e0e880 Iustin Pop

131 62e0e880 Iustin Pop
  """
132 62e0e880 Iustin Pop
  return lambda container: len(container) == size
133 62e0e880 Iustin Pop
134 62e0e880 Iustin Pop
135 62e0e880 Iustin Pop
# Combinator types
136 62e0e880 Iustin Pop
def TAnd(*args):
137 62e0e880 Iustin Pop
  """Combine multiple functions using an AND operation.
138 62e0e880 Iustin Pop

139 62e0e880 Iustin Pop
  """
140 62e0e880 Iustin Pop
  def fn(val):
141 62e0e880 Iustin Pop
    return compat.all(t(val) for t in args)
142 62e0e880 Iustin Pop
  return fn
143 62e0e880 Iustin Pop
144 62e0e880 Iustin Pop
145 62e0e880 Iustin Pop
def TOr(*args):
146 62e0e880 Iustin Pop
  """Combine multiple functions using an AND operation.
147 62e0e880 Iustin Pop

148 62e0e880 Iustin Pop
  """
149 62e0e880 Iustin Pop
  def fn(val):
150 62e0e880 Iustin Pop
    return compat.any(t(val) for t in args)
151 62e0e880 Iustin Pop
  return fn
152 62e0e880 Iustin Pop
153 62e0e880 Iustin Pop
154 62e0e880 Iustin Pop
def TMap(fn, test):
155 62e0e880 Iustin Pop
  """Checks that a modified version of the argument passes the given test.
156 62e0e880 Iustin Pop

157 62e0e880 Iustin Pop
  """
158 62e0e880 Iustin Pop
  return lambda val: test(fn(val))
159 62e0e880 Iustin Pop
160 62e0e880 Iustin Pop
161 62e0e880 Iustin Pop
# Type aliases
162 62e0e880 Iustin Pop
163 62e0e880 Iustin Pop
#: a non-empty string
164 62e0e880 Iustin Pop
TNonEmptyString = TAnd(TString, TTrue)
165 62e0e880 Iustin Pop
166 62e0e880 Iustin Pop
167 62e0e880 Iustin Pop
#: a maybe non-empty string
168 62e0e880 Iustin Pop
TMaybeString = TOr(TNonEmptyString, TNone)
169 62e0e880 Iustin Pop
170 62e0e880 Iustin Pop
171 62e0e880 Iustin Pop
#: a maybe boolean (bool or none)
172 62e0e880 Iustin Pop
TMaybeBool = TOr(TBool, TNone)
173 62e0e880 Iustin Pop
174 62e0e880 Iustin Pop
175 62e0e880 Iustin Pop
#: a positive integer
176 62e0e880 Iustin Pop
TPositiveInt = TAnd(TInt, lambda v: v >= 0)
177 62e0e880 Iustin Pop
178 62e0e880 Iustin Pop
#: a strictly positive integer
179 62e0e880 Iustin Pop
TStrictPositiveInt = TAnd(TInt, lambda v: v > 0)
180 62e0e880 Iustin Pop
181 62e0e880 Iustin Pop
182 62e0e880 Iustin Pop
def TListOf(my_type):
183 62e0e880 Iustin Pop
  """Checks if a given value is a list with all elements of the same type.
184 62e0e880 Iustin Pop

185 62e0e880 Iustin Pop
  """
186 62e0e880 Iustin Pop
  return TAnd(TList,
187 62e0e880 Iustin Pop
               lambda lst: compat.all(my_type(v) for v in lst))
188 62e0e880 Iustin Pop
189 62e0e880 Iustin Pop
190 62e0e880 Iustin Pop
def TDictOf(key_type, val_type):
191 62e0e880 Iustin Pop
  """Checks a dict type for the type of its key/values.
192 62e0e880 Iustin Pop

193 62e0e880 Iustin Pop
  """
194 62e0e880 Iustin Pop
  return TAnd(TDict,
195 62e0e880 Iustin Pop
              lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys())
196 62e0e880 Iustin Pop
                               and compat.all(val_type(v)
197 62e0e880 Iustin Pop
                                              for v in my_dict.values())))