From 6c1f988b131ffa4f861983aa6693caf192e09950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 3 Oct 2019 13:23:00 -0700 Subject: [PATCH] fix: Cache describeTypes at AbstractMISP level. --- pymisp/abstract.py | 31 +++++++++++++++++++++++++++++++ pymisp/mispevent.py | 21 +++++---------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index 39c6165..0657765 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -80,8 +80,35 @@ class MISPEncode(JSONEncoder): return JSONEncoder.default(self, obj) +if sys.version_info >= (3, 6): + from pathlib import Path + + def cache_describe_types(): + resources_path = Path(__file__).parent / 'data' + with (resources_path / 'describeTypes.json').open() as f: + dt = json.load(f) + return dt['result'] +else: + import os + if (3, 0) <= sys.version_info < (3, 6): + OLD_PY3 = True + else: + OLD_PY3 = False + + def cache_describe_types(): + ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') + 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) + return t['result'] + + class AbstractMISP(MutableMapping): + __describe_types = cache_describe_types() + def __init__(self, **kwargs): """Abstract class for all the MISP objects""" super(AbstractMISP, self).__init__() @@ -102,6 +129,10 @@ class AbstractMISP(MutableMapping): setattr(AbstractMISP, 'add_tag', AbstractMISP.__add_tag) setattr(AbstractMISP, 'tags', property(AbstractMISP.__get_tags, AbstractMISP.__set_tags)) + @property + def describe_types(self): + return self.__describe_types + @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 d5ef5ab..15af455 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -110,15 +110,9 @@ class MISPAttribute(AbstractMISP): """ super(MISPAttribute, self).__init__() if not describe_types: - ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') - 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.__categories = describe_types['categories'] - self._types = describe_types['types'] + describe_types = self.describe_types + self.__categories = frozenset(describe_types['categories']) + self._types = frozenset(describe_types['types']) self.__category_type_mapping = describe_types['category_type_mappings'] self.__sane_default = describe_types['sane_defaults'] self.__strict = strict @@ -442,14 +436,9 @@ class MISPEvent(AbstractMISP): # 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) - self._describe_types = t['result'] + self._describe_types = self.describe_types - self._types = self._describe_types['types'] + self._types = frozenset(self._describe_types['types']) self.Attribute = [] self.Object = [] self.RelatedEvent = []