From be2a50547e1839b4fa54e4d209a1d0bd929e2177 Mon Sep 17 00:00:00 2001 From: chouseknecht Date: Tue, 9 Feb 2016 15:03:01 -0500 Subject: [PATCH] Moved Conditional class to netcfg. --- .../modules/network/eos/eos_command.py | 73 ----------------- .../modules/network/nxos/nxos_command.py | 79 ++----------------- 2 files changed, 6 insertions(+), 146 deletions(-) diff --git a/lib/ansible/modules/network/eos/eos_command.py b/lib/ansible/modules/network/eos/eos_command.py index 545c49d8ef0..a197dd921a6 100644 --- a/lib/ansible/modules/network/eos/eos_command.py +++ b/lib/ansible/modules/network/eos/eos_command.py @@ -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'), diff --git a/lib/ansible/modules/network/nxos/nxos_command.py b/lib/ansible/modules/network/nxos/nxos_command.py index feed205192d..21da8d70a3f 100644 --- a/lib/ansible/modules/network/nxos/nxos_command.py +++ b/lib/ansible/modules/network/nxos/nxos_command.py @@ -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(