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())
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