cti-python-stix2/stix2/v21/bundle.py

54 lines
1.8 KiB
Python
Raw Normal View History

2018-11-28 22:51:00 +01:00
"""STIX 2.1 Bundle Representation."""
from collections import OrderedDict
from ..properties import (
IDProperty, ListProperty, STIXObjectProperty, TypeProperty,
)
from .base import _STIXBase21
class Bundle(_STIXBase21):
"""For more detailed information on this object's properties, see
2021-02-20 05:27:41 +01:00
`the STIX 2.1 specification <https://docs.oasis-open.org/cti/stix/v2.1/cs02/stix-v2.1-cs02.html#_gms872kuzdmg>`__.
"""
_type = 'bundle'
_properties = OrderedDict([
('type', TypeProperty(_type, spec_version='2.1')),
('id', IDProperty(_type, spec_version='2.1')),
('objects', ListProperty(STIXObjectProperty(spec_version='2.1'))),
])
def __init__(self, *args, **kwargs):
# Add any positional arguments to the 'objects' kwarg.
if args:
obj_list = []
for arg in args:
if isinstance(arg, list):
obj_list = obj_list + arg
else:
obj_list.append(arg)
kwargs['objects'] = obj_list + kwargs.get('objects', [])
super(Bundle, self).__init__(**kwargs)
def get_obj(self, obj_uuid):
2019-05-20 22:29:01 +02:00
if "objects" in self._inner:
found_objs = [elem for elem in self.objects if elem['id'] == obj_uuid]
if not found_objs:
2019-05-20 22:29:01 +02:00
raise KeyError("'%s' does not match the id property of any of the bundle's objects" % obj_uuid)
return found_objs
else:
raise KeyError("There are no objects in this empty bundle")
2019-05-17 21:21:35 +02:00
def __getitem__(self, key):
try:
return super(Bundle, self).__getitem__(key)
except KeyError:
try:
return self.get_obj(key)
except KeyError:
2019-05-22 17:05:01 +02:00
raise KeyError("'%s' is neither a property on the bundle nor does it match the id property of any of the bundle's objects" % key)