Skip to content

Commit 91d5bea

Browse files
author
shacharl
committed
fix: correctly handle event type in as_aapi_dict
1 parent 0062a4f commit 91d5bea

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

src/aapi/bases.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,70 @@ class AAPIJob:
1414

1515

1616
class AAPIObject:
17-
def as_aapi_dict(self):
17+
def as_aapi_dict(self, ignore_event_type=True):
18+
from aapi.ifbase import IfCompletionStatus
1819
res = {}
1920

20-
if "_type" in attrs.fields_dict(self.__class__):
21-
if not self._type.startswith("Condition") or self._type.startswith("Event"):
22-
res["Type"] = self._type
23-
21+
if '_type' in attrs.fields_dict(self.__class__):
22+
if ignore_event_type and not self._type.startswith('Condition') and not self._type.startswith('Event'):
23+
res['Type'] = self._type
24+
if not ignore_event_type and self._type.startswith('Event'):
25+
res['Type'] = self._type
2426
if attrs.has(self):
2527
for field in attrs.fields(self.__class__):
2628
value = self.__getattribute__(field.name)
27-
aapi_repr = field.metadata.get("_aapi_repr_")
29+
aapi_repr = field.metadata.get('_aapi_repr_')
2830

2931
if value in [None, [], {}]:
3032
continue
3133

3234
if aapi_repr:
33-
if "_type_aapi_" in field.metadata:
34-
if "_hide_type_" in field.metadata:
35-
if "Type" in res:
36-
res.__delitem__("Type")
35+
if '_type_aapi_' in field.metadata:
36+
if '_hide_type_' in field.metadata:
37+
if 'Type' in res:
38+
res.__delitem__('Type')
3739
continue
38-
if "as_aapi_dict" in dir(value):
40+
if 'as_aapi_dict' in dir(value):
3941
res[aapi_repr] = value.as_aapi_dict()
4042
elif isinstance(value, list):
4143
res[aapi_repr] = [
42-
(o.as_aapi_dict() if "as_aapi_dict" in dir(o) else o)
43-
for o in value
44+
(o.as_aapi_dict() if 'as_aapi_dict' in dir(o) else o) for o in value
4445
]
4546
elif isinstance(value, enum.Enum):
4647
res[aapi_repr] = value.value
4748
else:
4849
res[aapi_repr] = value
4950

50-
elif field.metadata.get("_abstract_aapi_container_"):
51+
elif field.metadata.get('_abstract_aapi_container_'):
5152
for obj in value:
52-
res[obj.object_name] = obj.as_aapi_dict()
53+
obj_ignore_type = ignore_event_type
54+
if isinstance(self, IfCompletionStatus):
55+
obj_ignore_type = False # Suppress "Type" for Events in these containers
56+
res[obj.object_name] = obj.as_aapi_dict(ignore_event_type=obj_ignore_type)
5357

5458
else:
55-
res["attrsibutes_valid"] = False
59+
res['attrsibutes_valid'] = False
5660

5761
return res
5862

63+
64+
def from_aapi_dict(self, aapi_dict):
65+
# Convert a JSON-like dictionary back into Python objects
66+
for key, value in aapi_dict.items():
67+
if isinstance(value, dict):
68+
# If the value is a dictionary, attempt to find the appropriate class
69+
# You need a way to figure out which class to map this dictionary to
70+
if "Type" in value:
71+
type_ = value["Type"]
72+
cls = AAPIObject.get_class_by_type(type_)
73+
if cls:
74+
value = cls.from_aapi_dict(value)
75+
elif isinstance(value, list):
76+
# If the value is a list, recursively process each item in the list
77+
value = [self.from_aapi_dict(v) if isinstance(v, dict) else v for v in value]
78+
setattr(self, key, value)
79+
80+
5981
def dumps_aapi(self, indent=None):
6082
return json.dumps(self.as_aapi_dict(), indent=indent)
6183

0 commit comments

Comments
 (0)