add: Add __eq__ to AbstractMISP

Allow to discard duplicate tags.
pull/280/head
Raphaël Vinot 2018-09-23 17:39:20 -04:00
parent 532157f3c9
commit 9a6761e817
3 changed files with 15 additions and 6 deletions

View File

@ -244,8 +244,9 @@ class AbstractMISP(collections.MutableMapping):
misp_tag.from_dict(**kwargs) misp_tag.from_dict(**kwargs)
else: else:
raise PyMISPInvalidFormat("The tag is in an invalid format (can be either string, MISPTag, or an expanded dict): {}".format(tag)) 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) if misp_tag not in self.tags:
self.edited = True self.Tag.append(misp_tag)
self.edited = True
def __get_tags(self): def __get_tags(self):
"""Returns a lost of tags associated to this Attribute""" """Returns a lost of tags associated to this Attribute"""
@ -258,6 +259,14 @@ class AbstractMISP(collections.MutableMapping):
else: else:
raise PyMISPInvalidFormat('All the attributes have to be of type MISPTag.') 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): class MISPTag(AbstractMISP):
def __init__(self): def __init__(self):

View File

@ -169,7 +169,7 @@ class ExpandedPyMISP(PyMISP):
:param eventinfo: Search in the eventinfo field :param eventinfo: Search in the eventinfo field
:param searchall: Set to run a full text search on the whole database (slow) :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 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: Deprecated:

View File

@ -7,7 +7,6 @@ import os
import base64 import base64
from io import BytesIO from io import BytesIO
from zipfile import ZipFile from zipfile import ZipFile
import hashlib
import sys import sys
import uuid import uuid
from collections import defaultdict from collections import defaultdict
@ -450,7 +449,7 @@ class MISPEvent(AbstractMISP):
with open(event_path, 'rb') as f: with open(event_path, 'rb') as f:
self.load(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""" """Load a JSON dump from a pseudo file or a JSON string"""
if hasattr(json_event, 'read'): if hasattr(json_event, 'read'):
# python2 and python3 compatible to find if we have a file # 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 'attribute_count' in event.get('Event') and
event.get('Event').get('attribute_count') is None): event.get('Event').get('attribute_count') is None):
event['Event']['attribute_count'] = '0' event['Event']['attribute_count'] = '0'
jsonschema.validate(event, self.__json_schema)
e = event.get('Event') e = event.get('Event')
self.from_dict(**e) self.from_dict(**e)
if validate:
jsonschema.validate(json.loads(self.to_json()), self.__json_schema)
def set_date(self, date, ignore_invalid=False): def set_date(self, date, ignore_invalid=False):
"""Set a date for the event (string, datetime, or date object)""" """Set a date for the event (string, datetime, or date object)"""