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,6 +244,7 @@ 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))
if misp_tag not in self.tags:
self.Tag.append(misp_tag)
self.edited = True
@ -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):

View File

@ -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:

View File

@ -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)"""