From a8dea2057007c6d50322928401ae1a3f33b54ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 22 Jan 2019 13:25:26 +0100 Subject: [PATCH] fix: Attempt to fix memory footprint in MISPAttribute Related: #304 --- pymisp/mispevent.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index ff7ad33..3721972 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -372,15 +372,18 @@ class MISPEvent(AbstractMISP): self.__json_schema = json.loads(f.read().decode()) else: self.__json_schema = json.load(f) - if not describe_types: + if describe_types: + # This variable is used in add_attribute in order to avoid duplicating the structure + self.describe_types = describe_types + else: with open(os.path.join(ressources_path, 'describeTypes.json'), 'rb') as f: if OLD_PY3: t = json.loads(f.read().decode()) else: t = json.load(f) - describe_types = t['result'] + self.describe_types = t['result'] - self._types = describe_types['types'] + self._types = self.describe_types['types'] self.Attribute = [] self.Object = [] self.RelatedEvent = [] @@ -596,10 +599,10 @@ class MISPEvent(AbstractMISP): ''' tags = [] for a in self.attributes + [attribute for o in self.objects for attribute in o.attributes]: - if ((hasattr(a, 'id') and a.id == attribute_identifier) or - (hasattr(a, 'uuid') and a.uuid == attribute_identifier) or - (hasattr(a, 'value') and attribute_identifier == a.value or - attribute_identifier in a.value.split('|'))): + if ((hasattr(a, 'id') and a.id == attribute_identifier) + or (hasattr(a, 'uuid') and a.uuid == attribute_identifier) + or (hasattr(a, 'value') and attribute_identifier == a.value + or attribute_identifier in a.value.split('|'))): tags += a.tags return tags @@ -610,10 +613,10 @@ class MISPEvent(AbstractMISP): ''' attributes = [] for a in self.attributes + [attribute for o in self.objects for attribute in o.attributes]: - if ((hasattr(a, 'id') and a.id == attribute_identifier) or - (hasattr(a, 'uuid') and a.uuid == attribute_identifier) or - (hasattr(a, 'value') and attribute_identifier == a.value or - attribute_identifier in a.value.split('|'))): + if ((hasattr(a, 'id') and a.id == attribute_identifier) + or (hasattr(a, 'uuid') and a.uuid == attribute_identifier) + or (hasattr(a, 'value') and attribute_identifier == a.value + or attribute_identifier in a.value.split('|'))): a.add_tag(tag) attributes.append(a) @@ -634,8 +637,8 @@ class MISPEvent(AbstractMISP): """Delete an attribute, you can search by ID or UUID""" found = False for a in self.attributes: - if ((hasattr(a, 'id') and a.id == attribute_id) or - (hasattr(a, 'uuid') and a.uuid == attribute_id)): + if ((hasattr(a, 'id') and a.id == attribute_id) + or (hasattr(a, 'uuid') and a.uuid == attribute_id)): a.delete() found = True break @@ -649,7 +652,7 @@ class MISPEvent(AbstractMISP): if isinstance(value, list): attr_list = [self.add_attribute(type=type, value=a, **kwargs) for a in value] else: - attribute = MISPAttribute() + attribute = MISPAttribute(describe_types=self.describe_types) attribute.from_dict(type=type, value=value, **kwargs) self.attributes.append(attribute) self.edited = True