fix: Attempt to fix memory footprint in MISPAttribute

Related: #304
pull/325/head
Raphaël Vinot 2019-01-22 13:25:26 +01:00
parent 57bb8cea1a
commit a8dea20570
1 changed files with 17 additions and 14 deletions

View File

@ -372,15 +372,18 @@ class MISPEvent(AbstractMISP):
self.__json_schema = json.loads(f.read().decode()) self.__json_schema = json.loads(f.read().decode())
else: else:
self.__json_schema = json.load(f) 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: with open(os.path.join(ressources_path, 'describeTypes.json'), 'rb') as f:
if OLD_PY3: if OLD_PY3:
t = json.loads(f.read().decode()) t = json.loads(f.read().decode())
else: else:
t = json.load(f) 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.Attribute = []
self.Object = [] self.Object = []
self.RelatedEvent = [] self.RelatedEvent = []
@ -596,10 +599,10 @@ class MISPEvent(AbstractMISP):
''' '''
tags = [] tags = []
for a in self.attributes + [attribute for o in self.objects for attribute in o.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 if ((hasattr(a, 'id') and a.id == attribute_identifier)
(hasattr(a, 'uuid') and a.uuid == attribute_identifier) or or (hasattr(a, 'uuid') and a.uuid == attribute_identifier)
(hasattr(a, 'value') and attribute_identifier == a.value or or (hasattr(a, 'value') and attribute_identifier == a.value
attribute_identifier in a.value.split('|'))): or attribute_identifier in a.value.split('|'))):
tags += a.tags tags += a.tags
return tags return tags
@ -610,10 +613,10 @@ class MISPEvent(AbstractMISP):
''' '''
attributes = [] attributes = []
for a in self.attributes + [attribute for o in self.objects for attribute in o.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 if ((hasattr(a, 'id') and a.id == attribute_identifier)
(hasattr(a, 'uuid') and a.uuid == attribute_identifier) or or (hasattr(a, 'uuid') and a.uuid == attribute_identifier)
(hasattr(a, 'value') and attribute_identifier == a.value or or (hasattr(a, 'value') and attribute_identifier == a.value
attribute_identifier in a.value.split('|'))): or attribute_identifier in a.value.split('|'))):
a.add_tag(tag) a.add_tag(tag)
attributes.append(a) attributes.append(a)
@ -634,8 +637,8 @@ class MISPEvent(AbstractMISP):
"""Delete an attribute, you can search by ID or UUID""" """Delete an attribute, you can search by ID or UUID"""
found = False found = False
for a in self.attributes: for a in self.attributes:
if ((hasattr(a, 'id') and a.id == attribute_id) or if ((hasattr(a, 'id') and a.id == attribute_id)
(hasattr(a, 'uuid') and a.uuid == attribute_id)): or (hasattr(a, 'uuid') and a.uuid == attribute_id)):
a.delete() a.delete()
found = True found = True
break break
@ -649,7 +652,7 @@ class MISPEvent(AbstractMISP):
if isinstance(value, list): if isinstance(value, list):
attr_list = [self.add_attribute(type=type, value=a, **kwargs) for a in value] attr_list = [self.add_attribute(type=type, value=a, **kwargs) for a in value]
else: else:
attribute = MISPAttribute() attribute = MISPAttribute(describe_types=self.describe_types)
attribute.from_dict(type=type, value=value, **kwargs) attribute.from_dict(type=type, value=value, **kwargs)
self.attributes.append(attribute) self.attributes.append(attribute)
self.edited = True self.edited = True