Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #195 - improved retcode handling #196

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions pepper/retcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ def validate(self, options, result):
return self.validate_fail_all_none(result)
return 0

@staticmethod
def gen_dict_extract(var, key):
'''
Generator function that yields all values for key in dictionary

:param var: dictionary or list of dictionaries

:param key: key name

:return: generator object containing values of key
'''
if isinstance(var, dict):
for k, v in var.items():
if k == key:
yield v
if isinstance(v, (dict, list)):
for result in PepperRetcode.gen_dict_extract(v, key):
yield result
elif isinstance(var, list):
for d in var:
for result in PepperRetcode.gen_dict_extract(d, key):
yield result

@staticmethod
def validate_fail_any(result):
'''
Expand All @@ -40,14 +63,8 @@ def validate_fail_any(result):

:return: exit code
'''
if isinstance(result, list):
if isinstance(result[0], dict):
minion = result[0]
retcodes = list(minion[name].get('retcode')
for name in minion if isinstance(minion[name], dict) and
minion[name].get('retcode') is not None)
return next((r for r in retcodes if r != 0), 0)
return 0
retcodes = list(PepperRetcode.gen_dict_extract(result, 'retcode'))
return next((r for r in retcodes if r != 0), 0)

@staticmethod
def validate_fail_any_none(result):
Expand All @@ -60,16 +77,10 @@ def validate_fail_any_none(result):

:return: exit code
'''
if isinstance(result, list):
if isinstance(result[0], dict):
minion = result[0]
retcodes = list(minion[name].get('retcode')
for name in minion if isinstance(minion[name], dict) and
minion[name].get('retcode') is not None)
if not retcodes:
return -1 # there are no retcodes
return next((r for r in retcodes if r != 0), 0)
return -1
retcodes = list(PepperRetcode.gen_dict_extract(result, 'retcode'))
if not retcodes:
return -1 # there are no retcodes
return next((r for r in retcodes if r != 0), 0)

@staticmethod
def validate_fail_all(result):
Expand All @@ -82,14 +93,9 @@ def validate_fail_all(result):

:return: exit code
'''
if isinstance(result, list):
if isinstance(result[0], dict):
minion = result[0]
retcodes = list(minion[name].get('retcode')
for name in minion if isinstance(minion[name], dict) and
minion[name].get('retcode') is not None)
if all(r != 0 for r in retcodes):
return next((r for r in retcodes if r != 0), 0)
retcodes = list(PepperRetcode.gen_dict_extract(result, 'retcode'))
if all(r != 0 for r in retcodes):
return next((r for r in retcodes if r != 0), 0)
return 0

@staticmethod
Expand All @@ -103,16 +109,9 @@ def validate_fail_all_none(result):

:return: exit code
'''
if isinstance(result, list):
if isinstance(result[0], dict):
minion = result[0]
retcodes = list(minion[name].get('retcode')
for name in minion if isinstance(minion[name], dict) and
minion[name].get('retcode') is not None)
if not retcodes:
return -1 # there are no retcodes
if all(r != 0 for r in retcodes):
return next((r for r in retcodes if r != 0), 0)
else:
return 0
return -1
retcodes = list(PepperRetcode.gen_dict_extract(result, 'retcode'))
if not retcodes:
return -1 # there are no retcodes
if all(r != 0 for r in retcodes):
return next((r for r in retcodes if r != 0), 0)
return 0