Skip to content

Commit

Permalink
fix: Cache object templates at AbstractMISP level
Browse files Browse the repository at this point in the history
Related #468 and  #471
  • Loading branch information
Rafiot committed Oct 3, 2019
1 parent 6c1f988 commit bae942d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 20 additions & 0 deletions pymisp/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def cache_describe_types():
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):
Expand All @@ -104,10 +110,19 @@ def cache_describe_types():
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"""
Expand All @@ -133,6 +148,11 @@ def __init__(self, **kwargs):
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.
Expand Down
12 changes: 4 additions & 8 deletions pymisp/mispevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def __init__(self, describe_types=None, strict=False):
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
Expand Down Expand Up @@ -438,7 +438,7 @@ def __init__(self, describe_types=None, strict_validation=False, **kwargs):
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 = []
Expand Down Expand Up @@ -1195,11 +1195,7 @@ def __init__(self, name, strict=False, standalone=False, default_attributes_para
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']
Expand Down

0 comments on commit bae942d

Please sign in to comment.