|
|
|
@ -38,6 +38,7 @@ class ConfigLine(object):
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse(lines, indent):
|
|
|
|
|
toplevel = re.compile(r'\S')
|
|
|
|
|
childline = re.compile(r'^\s*(.+)$')
|
|
|
|
@ -83,3 +84,87 @@ def parse(lines, indent):
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Conditional(object):
|
|
|
|
|
'''
|
|
|
|
|
Used in command modules to evaluate waitfor conditions
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
OPERATORS = {
|
|
|
|
|
'eq': ['eq', '=='],
|
|
|
|
|
'neq': ['neq', 'ne', '!='],
|
|
|
|
|
'gt': ['gt', '>'],
|
|
|
|
|
'ge': ['ge', '>='],
|
|
|
|
|
'lt': ['lt', '<'],
|
|
|
|
|
'le': ['le', '<='],
|
|
|
|
|
'contains': ['contains']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def __init__(self, conditional):
|
|
|
|
|
self.raw = conditional
|
|
|
|
|
|
|
|
|
|
key, op, val = shlex.split(conditional)
|
|
|
|
|
self.key = key
|
|
|
|
|
self.func = self.func(op)
|
|
|
|
|
self.value = self._cast_value(val)
|
|
|
|
|
|
|
|
|
|
def __call__(self, data):
|
|
|
|
|
try:
|
|
|
|
|
value = self.get_value(dict(result=data))
|
|
|
|
|
return self.func(value)
|
|
|
|
|
except Exception:
|
|
|
|
|
raise ValueError(self.key)
|
|
|
|
|
|
|
|
|
|
def _cast_value(self, value):
|
|
|
|
|
if value in BOOLEANS_TRUE:
|
|
|
|
|
return True
|
|
|
|
|
elif value in BOOLEANS_FALSE:
|
|
|
|
|
return False
|
|
|
|
|
elif re.match(r'^\d+\.d+$', value):
|
|
|
|
|
return float(value)
|
|
|
|
|
elif re.match(r'^\d+$', value):
|
|
|
|
|
return int(value)
|
|
|
|
|
else:
|
|
|
|
|
return unicode(value)
|
|
|
|
|
|
|
|
|
|
def func(self, oper):
|
|
|
|
|
for func, operators in self.OPERATORS.items():
|
|
|
|
|
if oper in operators:
|
|
|
|
|
return getattr(self, func)
|
|
|
|
|
raise AttributeError('unknown operator: %s' % oper)
|
|
|
|
|
|
|
|
|
|
def get_value(self, result):
|
|
|
|
|
for key in self.key.split('.'):
|
|
|
|
|
match = re.match(r'^(.+)\[(\d+)\]', key)
|
|
|
|
|
if match:
|
|
|
|
|
key, index = match.groups()
|
|
|
|
|
result = result[key][int(index)]
|
|
|
|
|
else:
|
|
|
|
|
result = result.get(key)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def number(self, value):
|
|
|
|
|
if '.' in str(value):
|
|
|
|
|
return float(value)
|
|
|
|
|
else:
|
|
|
|
|
return int(value)
|
|
|
|
|
|
|
|
|
|
def eq(self, value):
|
|
|
|
|
return value == self.value
|
|
|
|
|
|
|
|
|
|
def neq(self, value):
|
|
|
|
|
return value != self.value
|
|
|
|
|
|
|
|
|
|
def gt(self, value):
|
|
|
|
|
return self.number(value) > self.value
|
|
|
|
|
|
|
|
|
|
def ge(self, value):
|
|
|
|
|
return self.number(value) >= self.value
|
|
|
|
|
|
|
|
|
|
def lt(self, value):
|
|
|
|
|
return self.number(value) < self.value
|
|
|
|
|
|
|
|
|
|
def le(self, value):
|
|
|
|
|
return self.number(value) <= self.value
|
|
|
|
|
|
|
|
|
|
def contains(self, value):
|
|
|
|
|
return self.value in value
|
|
|
|
|