mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 22:00:22 -07:00
Moved Conditional class to netcfg.
This commit is contained in:
parent
ab210862f0
commit
be2a50547e
2 changed files with 6 additions and 146 deletions
|
@ -124,79 +124,6 @@ def to_lines(stdout):
|
||||||
item = str(item).split('\n')
|
item = str(item).split('\n')
|
||||||
yield item
|
yield item
|
||||||
|
|
||||||
class Conditional(object):
|
|
||||||
|
|
||||||
OPERATORS = {
|
|
||||||
'eq': ['eq', '=='],
|
|
||||||
'neq': ['neq', 'ne', '!='],
|
|
||||||
'gt': ['gt', '>'],
|
|
||||||
'ge': ['ge', '>='],
|
|
||||||
'lt': ['lt', '<'],
|
|
||||||
'le': ['le', '<='],
|
|
||||||
'contains': ['contains', 'in']
|
|
||||||
}
|
|
||||||
|
|
||||||
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):
|
|
||||||
value = self.get_value(dict(result=data))
|
|
||||||
return self.func(value)
|
|
||||||
|
|
||||||
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'^(\w+)\[(\d+)\]', key)
|
|
||||||
if match:
|
|
||||||
key, index = match.groups()
|
|
||||||
result = result[key][int(index)]
|
|
||||||
else:
|
|
||||||
result = result.get(key)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def eq(self, value):
|
|
||||||
return value == self.value
|
|
||||||
|
|
||||||
def neq(self, value):
|
|
||||||
return value != self.value
|
|
||||||
|
|
||||||
def gt(self, value):
|
|
||||||
return value > self.value
|
|
||||||
|
|
||||||
def ge(self, value):
|
|
||||||
return value >= self.value
|
|
||||||
|
|
||||||
def lt(self, value):
|
|
||||||
return value < self.value
|
|
||||||
|
|
||||||
def le(self, value):
|
|
||||||
return value <= self.value
|
|
||||||
|
|
||||||
def contains(self, value):
|
|
||||||
return self.value in value
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
spec = dict(
|
spec = dict(
|
||||||
commands=dict(type='list'),
|
commands=dict(type='list'),
|
||||||
|
|
|
@ -127,79 +127,12 @@ def to_lines(stdout):
|
||||||
if isinstance(item, basestring):
|
if isinstance(item, basestring):
|
||||||
item = str(item).split('\n')
|
item = str(item).split('\n')
|
||||||
yield item
|
yield item
|
||||||
|
def get_response(data):
|
||||||
class Conditional(object):
|
try:
|
||||||
|
json_data = json.loads(data)
|
||||||
OPERATORS = {
|
except ValueError:
|
||||||
'eq': ['eq', '=='],
|
json_data = None
|
||||||
'neq': ['neq', 'ne', '!='],
|
return dict(data=data, json=json_data)
|
||||||
'gt': ['gt', '>'],
|
|
||||||
'ge': ['ge', '>='],
|
|
||||||
'lt': ['lt', '<'],
|
|
||||||
'le': ['le', '<='],
|
|
||||||
'contains': ['contains', 'in']
|
|
||||||
}
|
|
||||||
|
|
||||||
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):
|
|
||||||
value = self.get_value(dict(result=data))
|
|
||||||
return self.func(value)
|
|
||||||
|
|
||||||
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'^(\w+)\[(\d+)\]', key)
|
|
||||||
if match:
|
|
||||||
key, index = match.groups()
|
|
||||||
result = result[key][int(index)]
|
|
||||||
else:
|
|
||||||
result = result.get(key)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def eq(self, value):
|
|
||||||
return value == self.value
|
|
||||||
|
|
||||||
def neq(self, value):
|
|
||||||
return value != self.value
|
|
||||||
|
|
||||||
def gt(self, value):
|
|
||||||
return value > self.value
|
|
||||||
|
|
||||||
def ge(self, value):
|
|
||||||
return value >= self.value
|
|
||||||
|
|
||||||
def lt(self, value):
|
|
||||||
return value < self.value
|
|
||||||
|
|
||||||
def le(self, value):
|
|
||||||
return value <= self.value
|
|
||||||
|
|
||||||
def contains(self, value):
|
|
||||||
return self.value in value
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
spec = dict(
|
spec = dict(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue