mirror of https://github.com/MISP/PyMISP
fix: Cache describeTypes at AbstractMISP level.
parent
a2b66e943b
commit
6c1f988b13
|
@ -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.
|
||||
|
|
|
@ -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 = []
|
||||
|
|
Loading…
Reference in New Issue