From bae942d2ecf9839a37e248c002f43952a83a2a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 3 Oct 2019 13:54:39 -0700 Subject: [PATCH] fix: Cache object templates at AbstractMISP level Related #468 and #471 --- pymisp/abstract.py | 20 ++++++++++++++++++++ pymisp/mispevent.py | 12 ++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index 0657765..e49adba 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -88,6 +88,12 @@ if sys.version_info >= (3, 6): with (resources_path / 'describeTypes.json').open() as f: dt = json.load(f) return dt['result'] + + def load_template(path): + with open(path) as f: + t = json.load(f) + return t + else: import os if (3, 0) <= sys.version_info < (3, 6): @@ -104,10 +110,19 @@ else: t = json.load(f) 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): __describe_types = cache_describe_types() + __object_templates = {} def __init__(self, **kwargs): """Abstract class for all the MISP objects""" @@ -133,6 +148,11 @@ class AbstractMISP(MutableMapping): def describe_types(self): 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 def properties(self): """All the class public properties that will be dumped in the dictionary, and the JSON export. diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index 15af455..7939813 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -111,8 +111,8 @@ class MISPAttribute(AbstractMISP): super(MISPAttribute, self).__init__() if not describe_types: describe_types = self.describe_types - self.__categories = frozenset(describe_types['categories']) - self._types = frozenset(describe_types['types']) + self.__categories = describe_types['categories'] + self._types = describe_types['types'] self.__category_type_mapping = describe_types['category_type_mappings'] self.__sane_default = describe_types['sane_defaults'] self.__strict = strict @@ -438,7 +438,7 @@ class MISPEvent(AbstractMISP): else: self._describe_types = self.describe_types - self._types = frozenset(self._describe_types['types']) + self._types = self._describe_types['types'] self.Attribute = [] self.Object = [] self.RelatedEvent = [] @@ -1195,11 +1195,7 @@ class MISPObject(AbstractMISP): def _load_template_path(self, template_path): if not os.path.exists(template_path): return False - with open(template_path, 'rb') as f: - if OLD_PY3: - self._definition = json.loads(f.read().decode()) - else: - self._definition = json.load(f) + self._definition = self.get_template_definition(template_path) setattr(self, 'meta-category', self._definition['meta-category']) self.template_uuid = self._definition['uuid'] self.description = self._definition['description']