Moved Conditional class to netcfg.

This commit is contained in:
chouseknecht 2016-02-09 15:03:01 -05:00 committed by Matt Clay
parent ab210862f0
commit be2a50547e
2 changed files with 6 additions and 146 deletions

View file

@ -124,79 +124,6 @@ def to_lines(stdout):
item = str(item).split('\n')
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():
spec = dict(
commands=dict(type='list'),

View file

@ -127,79 +127,12 @@ def to_lines(stdout):
if isinstance(item, basestring):
item = str(item).split('\n')
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 get_response(data):
try:
json_data = json.loads(data)
except ValueError:
json_data = None
return dict(data=data, json=json_data)
def main():
spec = dict(