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
changes/76/588476/2
Kaifeng Wang 4 years ago
parent ef0bc413ae
commit 292c168d9e
  1. 70
      ironic_inspector/rules.py
  2. 4
      tox.ini

@ -240,41 +240,17 @@ def _parse_path(path):
return scheme, path
def create(conditions_json, actions_json, uuid=None,
description=None):
"""Create a new rule in database.
def _validate_conditions(conditions_json):
"""Validates conditions from jsonschema.
: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
:returns: a list of conditions.
"""
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:
jsonschema.validate(conditions_json, conditions_schema())
except jsonschema.ValidationError as 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()
act_mgr = plugins_base.rule_actions_manager()
conditions = []
reserved_params = {'op', 'field', 'multiple', 'invert'}
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('invert', False),
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 = []
for action_json in actions_json:
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})
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:
with db.ensure_transaction() as session:

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

Loading…
Cancel
Save