Follow up to pycodestyle fix

During pycodestyle fix [0], the max code complexity is temporarily
increased to 18.

This patch is a follow up to restore previous complexity (value 15),
and refactor code causing C901.

[0] https://review.openstack.org/#/c/567066

Story: #2001985
Task: #19604

Change-Id: Id64c31c449761024504284dcadd2b6c9f4a97f00
This commit is contained in:
Kaifeng Wang 2018-08-03 16:38:07 +08:00
parent ef0bc413ae
commit 292c168d9e
2 changed files with 44 additions and 30 deletions

View File

@ -240,41 +240,17 @@ def _parse_path(path):
return scheme, path return scheme, path
def create(conditions_json, actions_json, uuid=None, def _validate_conditions(conditions_json):
description=None): """Validates conditions from jsonschema.
"""Create a new rule in database.
:param conditions_json: list of dicts with the following keys: :returns: a list of conditions.
* op - operator
* field - JSON path to field to compare
Other keys are stored as is.
:param actions_json: list of dicts with the following keys:
* action - action type
Other keys are stored as is.
:param uuid: rule UUID, will be generated if empty
:param description: human-readable rule description
:returns: new IntrospectionRule object
:raises: utils.Error on failure
""" """
uuid = uuid or uuidutils.generate_uuid()
LOG.debug('Creating rule %(uuid)s with description "%(descr)s", '
'conditions %(conditions)s and actions %(actions)s',
{'uuid': uuid, 'descr': description,
'conditions': conditions_json, 'actions': actions_json})
try: try:
jsonschema.validate(conditions_json, conditions_schema()) jsonschema.validate(conditions_json, conditions_schema())
except jsonschema.ValidationError as exc: except jsonschema.ValidationError as exc:
raise utils.Error(_('Validation failed for conditions: %s') % exc) raise utils.Error(_('Validation failed for conditions: %s') % exc)
try:
jsonschema.validate(actions_json, actions_schema())
except jsonschema.ValidationError as exc:
raise utils.Error(_('Validation failed for actions: %s') % exc)
cond_mgr = plugins_base.rule_conditions_manager() cond_mgr = plugins_base.rule_conditions_manager()
act_mgr = plugins_base.rule_actions_manager()
conditions = [] conditions = []
reserved_params = {'op', 'field', 'multiple', 'invert'} reserved_params = {'op', 'field', 'multiple', 'invert'}
for cond_json in conditions_json: for cond_json in conditions_json:
@ -307,7 +283,20 @@ def create(conditions_json, actions_json, uuid=None,
cond_json.get('multiple', 'any'), cond_json.get('multiple', 'any'),
cond_json.get('invert', False), cond_json.get('invert', False),
params)) params))
return conditions
def _validate_actions(actions_json):
"""Validates actions from jsonschema.
:returns: a list of actions.
"""
try:
jsonschema.validate(actions_json, actions_schema())
except jsonschema.ValidationError as exc:
raise utils.Error(_('Validation failed for actions: %s') % exc)
act_mgr = plugins_base.rule_actions_manager()
actions = [] actions = []
for action_json in actions_json: for action_json in actions_json:
plugin = act_mgr[action_json['action']].obj plugin = act_mgr[action_json['action']].obj
@ -320,6 +309,33 @@ def create(conditions_json, actions_json, uuid=None,
{'act': action_json['action'], 'error': exc}) {'act': action_json['action'], 'error': exc})
actions.append((action_json['action'], params)) actions.append((action_json['action'], params))
return actions
def create(conditions_json, actions_json, uuid=None,
description=None):
"""Create a new rule in database.
:param conditions_json: list of dicts with the following keys:
* op - operator
* field - JSON path to field to compare
Other keys are stored as is.
:param actions_json: list of dicts with the following keys:
* action - action type
Other keys are stored as is.
:param uuid: rule UUID, will be generated if empty
:param description: human-readable rule description
:returns: new IntrospectionRule object
:raises: utils.Error on failure
"""
uuid = uuid or uuidutils.generate_uuid()
LOG.debug('Creating rule %(uuid)s with description "%(descr)s", '
'conditions %(conditions)s and actions %(actions)s',
{'uuid': uuid, 'descr': description,
'conditions': conditions_json, 'actions': actions_json})
conditions = _validate_conditions(conditions_json)
actions = _validate_actions(actions_json)
try: try:
with db.ensure_transaction() as session: with db.ensure_transaction() as session:

View File

@ -68,9 +68,7 @@ deps = {[testenv]deps}
commands = {toxinidir}/tools/states_to_dot.py -f {toxinidir}/doc/source/images/states.svg --format svg commands = {toxinidir}/tools/states_to_dot.py -f {toxinidir}/doc/source/images/states.svg --format svg
[flake8] [flake8]
# TODO(hjensas): ironic_inspector/rules.py:243:1: C901 'create' is too complex (18) max-complexity=15
# That method is to complex, it should be fixed.
max-complexity=18
# [H106] Don't put vim configuration in source files. # [H106] Don't put vim configuration in source files.
# [H203] Use assertIs(Not)None to check for None. # [H203] Use assertIs(Not)None to check for None.
# [H204] Use assert(Not)Equal to check for equality. # [H204] Use assert(Not)Equal to check for equality.