From 9a6761e8177265264a8da039964eeab840c3e0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 23 Sep 2018 17:39:20 -0400 Subject: [PATCH] add: Add __eq__ to AbstractMISP Allow to discard duplicate tags. --- pymisp/abstract.py | 13 +++++++++++-- pymisp/aping.py | 2 +- pymisp/mispevent.py | 6 +++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index 3fe4085..55b504b 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -244,8 +244,9 @@ class AbstractMISP(collections.MutableMapping): misp_tag.from_dict(**kwargs) else: raise PyMISPInvalidFormat("The tag is in an invalid format (can be either string, MISPTag, or an expanded dict): {}".format(tag)) - self.Tag.append(misp_tag) - self.edited = True + if misp_tag not in self.tags: + self.Tag.append(misp_tag) + self.edited = True def __get_tags(self): """Returns a lost of tags associated to this Attribute""" @@ -258,6 +259,14 @@ class AbstractMISP(collections.MutableMapping): else: raise PyMISPInvalidFormat('All the attributes have to be of type MISPTag.') + def __eq__(self, other): + if isinstance(other, AbstractMISP): + return self.to_dict() == other.to_dict() + elif isinstance(other, dict): + return self.to_dict() == other + else: + return False + class MISPTag(AbstractMISP): def __init__(self): diff --git a/pymisp/aping.py b/pymisp/aping.py index 81f3342..7478e8d 100644 --- a/pymisp/aping.py +++ b/pymisp/aping.py @@ -169,7 +169,7 @@ class ExpandedPyMISP(PyMISP): :param eventinfo: Search in the eventinfo field :param searchall: Set to run a full text search on the whole database (slow) :param sg_reference_only: Only return a reference to the sharing groups the responses are sharing in (avoid leaking org names) - :param pythonify: Returns a list of PyMISP Objects the the plain json output + :param pythonify: Returns a list of PyMISP Objects the the plain json output. Warning: it might use a lot of RAM Deprecated: diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index 29cb501..ca99339 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -7,7 +7,6 @@ import os import base64 from io import BytesIO from zipfile import ZipFile -import hashlib import sys import uuid from collections import defaultdict @@ -450,7 +449,7 @@ class MISPEvent(AbstractMISP): with open(event_path, 'rb') as f: self.load(f) - def load(self, json_event): + def load(self, json_event, validate=False): """Load a JSON dump from a pseudo file or a JSON string""" if hasattr(json_event, 'read'): # python2 and python3 compatible to find if we have a file @@ -470,9 +469,10 @@ class MISPEvent(AbstractMISP): 'attribute_count' in event.get('Event') and event.get('Event').get('attribute_count') is None): event['Event']['attribute_count'] = '0' - jsonschema.validate(event, self.__json_schema) e = event.get('Event') self.from_dict(**e) + if validate: + jsonschema.validate(json.loads(self.to_json()), self.__json_schema) def set_date(self, date, ignore_invalid=False): """Set a date for the event (string, datetime, or date object)"""