Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3d7876

Browse files
committed
Updated get_all_actions() to support binning actions by name, and also added new get_action_object() method
1 parent 85c5d26 commit f3d7876

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

maec/bundle/bundle.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def add_named_object_collection(self, collection_name, collection_id):
9191
self.collections.object_collections.append(ObjectCollection(collection_name, collection_id))
9292

9393
# return a list of all abjects from self.actions and all action collections
94-
def get_all_actions(self):
94+
def get_all_actions(self, bin = False):
9595
all_actions = []
96+
9697
for action in self.actions:
9798
all_actions.append(action)
9899

@@ -101,7 +102,16 @@ def get_all_actions(self):
101102
for action in collection.action_list:
102103
all_actions.append(action)
103104

104-
return all_actions
105+
if bin:
106+
binned_actions = {}
107+
for action in all_actions:
108+
if action.name and action.name.value not in binned_actions:
109+
binned_actions[action.name.value] = [action]
110+
elif action.name and action.name.value in binned_actions:
111+
binned_actions[action.name.value].append(action)
112+
return binned_actions
113+
else:
114+
return all_actions
105115

106116
#Add an Object to an existing named collection; if it does not exist, add it to the top-level <Objects> element
107117
def add_object(self, object, object_collection_name = None):
@@ -295,6 +305,18 @@ def compare(cls, bundle_list, match_on = None, case_sensitive = True):
295305
def deduplicate(self):
296306
BundleDeduplicator.deduplicate(self)
297307

308+
def get_action_objects(self, action_name_list):
309+
"""Get all Objects corresponding to one or more types of Actions, specified via a list of Action names"""
310+
action_objects = {}
311+
all_actions = self.get_all_actions(bin=True)
312+
for action_name in action_name_list:
313+
if action_name in all_actions:
314+
associated_objects = []
315+
associated_object_lists = [[y for y in x.associated_objects if x.associated_objects] for x in all_actions[action_name]]
316+
for associated_object_list in associated_object_lists:
317+
associated_objects += associated_object_list
318+
action_objects[action_name] = associated_objects
319+
return action_objects
298320

299321
class BehaviorList(maec.EntityList):
300322
_contained_type = Behavior

0 commit comments

Comments
 (0)