Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Simplified a complicated function
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyjrobins committed Dec 20, 2017
1 parent 52a8b54 commit d4f4afa
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions core/jsonelementcreator.py
Expand Up @@ -22,7 +22,6 @@ def create(cls, json_in, element_class=None):
(ExecutionElement) The constructed ExecutionElement
"""
from core.executionelements.playbook import Playbook
from core.argument import Argument
cls._setup_ordering()
if element_class is None:
element_class = Playbook
Expand All @@ -34,22 +33,27 @@ def create(cls, json_in, element_class=None):
except StopIteration:
raise ValueError('Unknown class {}'.format(element_class.__class__.__name__))
try:
if subfield_lookup is not None:
for subfield_name, next_class in subfield_lookup.items():
if subfield_name in json_in:
subfield_json = json_in[subfield_name]
if hasattr(current_class, '_templatable'):
json_in['raw_representation'] = dict(json_in)
json_in[subfield_name] = [next_class.create(element_json) for element_json in subfield_json]
if 'arguments' in json_in:
json_in['arguments'] = [Argument(**arg_json) for arg_json in json_in['arguments']]
return current_class(**json_in)
return cls.construct_current_class(current_class, json_in, subfield_lookup)
except (KeyError, TypeError) as e:
from core.helpers import format_exception_message
raise ValueError(
'Improperly formatted JSON for ExecutionElement {0} {1}'.format(current_class.__name__,
format_exception_message(e)))

@classmethod
def construct_current_class(cls, current_class, json_in, subfield_lookup):
from core.argument import Argument
if subfield_lookup is not None:
for subfield_name, next_class in subfield_lookup.items():
if subfield_name in json_in:
subfield_json = json_in[subfield_name]
if hasattr(current_class, '_templatable'):
json_in['raw_representation'] = dict(json_in)
json_in[subfield_name] = [next_class.create(element_json) for element_json in subfield_json]
if 'arguments' in json_in:
json_in['arguments'] = [Argument(**arg_json) for arg_json in json_in['arguments']]
return current_class(**json_in)

@classmethod
def _setup_ordering(cls):
if cls.playbook_class_ordering is None:
Expand Down

0 comments on commit d4f4afa

Please sign in to comment.