2017-08-31 10:40:18 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import abc
|
|
|
|
import six
|
|
|
|
from .. import MISPObject
|
2018-03-27 14:57:07 +02:00
|
|
|
from ..exceptions import InvalidMISPObject
|
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.parser import parse
|
2017-08-31 10:40:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta) # Remove that line when discarding python2 support.
|
|
|
|
# Python3 way: class MISPObjectGenerator(metaclass=abc.ABCMeta):
|
|
|
|
class AbstractMISPObjectGenerator(MISPObject):
|
|
|
|
|
2018-03-27 14:57:07 +02:00
|
|
|
def _sanitize_timestamp(self, timestamp):
|
|
|
|
if not timestamp:
|
|
|
|
return datetime.now()
|
|
|
|
elif isinstance(timestamp, dict):
|
|
|
|
if not isinstance(timestamp['value'], datetime):
|
|
|
|
timestamp['value'] = parse(timestamp['value'])
|
|
|
|
return timestamp
|
|
|
|
elif not isinstance(timestamp, datetime):
|
|
|
|
return parse(timestamp)
|
|
|
|
return timestamp
|
|
|
|
|
2017-08-31 10:40:18 +02:00
|
|
|
def generate_attributes(self):
|
|
|
|
"""Contains the logic where all the values of the object are gathered"""
|
2018-03-27 14:57:07 +02:00
|
|
|
if hasattr(self, '_parameters'):
|
|
|
|
for object_relation in self._definition['attributes']:
|
|
|
|
value = self._parameters.pop(object_relation, None)
|
|
|
|
if not value:
|
|
|
|
continue
|
|
|
|
if isinstance(value, dict):
|
|
|
|
self.add_attribute(object_relation, **value)
|
|
|
|
else:
|
|
|
|
# Assume it is the value only
|
|
|
|
self.add_attribute(object_relation, value=value)
|
|
|
|
if self._strict and self._known_template and self._parameters:
|
|
|
|
raise InvalidMISPObject('Some object relations are unknown in the template and could not be attached: {}'.format(', '.join(self._parameters)))
|