fix: Cache object templates at AbstractMISP level

Related #468 and  #471
pull/470/head
Raphaël Vinot 2019-10-03 13:54:39 -07:00
parent 6c1f988b13
commit bae942d2ec
2 changed files with 24 additions and 8 deletions

View File

@ -88,6 +88,12 @@ if sys.version_info >= (3, 6):
with (resources_path / 'describeTypes.json').open() as f: with (resources_path / 'describeTypes.json').open() as f:
dt = json.load(f) dt = json.load(f)
return dt['result'] return dt['result']
def load_template(path):
with open(path) as f:
t = json.load(f)
return t
else: else:
import os import os
if (3, 0) <= sys.version_info < (3, 6): if (3, 0) <= sys.version_info < (3, 6):
@ -104,10 +110,19 @@ else:
t = json.load(f) t = json.load(f)
return t['result'] return t['result']
def load_template(path):
with open(path, 'rb') as f:
if OLD_PY3:
t = json.loads(f.read().decode())
else:
t = json.load(f)
return t
class AbstractMISP(MutableMapping): class AbstractMISP(MutableMapping):
__describe_types = cache_describe_types() __describe_types = cache_describe_types()
__object_templates = {}
def __init__(self, **kwargs): def __init__(self, **kwargs):
"""Abstract class for all the MISP objects""" """Abstract class for all the MISP objects"""
@ -133,6 +148,11 @@ class AbstractMISP(MutableMapping):
def describe_types(self): def describe_types(self):
return self.__describe_types return self.__describe_types
def get_template_definition(self, template_path):
if template_path not in self.__object_templates:
self.__object_templates[template_path] = load_template(template_path)
return self.__object_templates[template_path]
@property @property
def properties(self): def properties(self):
"""All the class public properties that will be dumped in the dictionary, and the JSON export. """All the class public properties that will be dumped in the dictionary, and the JSON export.

View File

@ -111,8 +111,8 @@ class MISPAttribute(AbstractMISP):
super(MISPAttribute, self).__init__() super(MISPAttribute, self).__init__()
if not describe_types: if not describe_types:
describe_types = self.describe_types describe_types = self.describe_types
self.__categories = frozenset(describe_types['categories']) self.__categories = describe_types['categories']
self._types = frozenset(describe_types['types']) self._types = describe_types['types']
self.__category_type_mapping = describe_types['category_type_mappings'] self.__category_type_mapping = describe_types['category_type_mappings']
self.__sane_default = describe_types['sane_defaults'] self.__sane_default = describe_types['sane_defaults']
self.__strict = strict self.__strict = strict
@ -438,7 +438,7 @@ class MISPEvent(AbstractMISP):
else: else:
self._describe_types = self.describe_types self._describe_types = self.describe_types
self._types = frozenset(self._describe_types['types']) self._types = self._describe_types['types']
self.Attribute = [] self.Attribute = []
self.Object = [] self.Object = []
self.RelatedEvent = [] self.RelatedEvent = []
@ -1195,11 +1195,7 @@ class MISPObject(AbstractMISP):
def _load_template_path(self, template_path): def _load_template_path(self, template_path):
if not os.path.exists(template_path): if not os.path.exists(template_path):
return False return False
with open(template_path, 'rb') as f: self._definition = self.get_template_definition(template_path)
if OLD_PY3:
self._definition = json.loads(f.read().decode())
else:
self._definition = json.load(f)
setattr(self, 'meta-category', self._definition['meta-category']) setattr(self, 'meta-category', self._definition['meta-category'])
self.template_uuid = self._definition['uuid'] self.template_uuid = self._definition['uuid']
self.description = self._definition['description'] self.description = self._definition['description']