chg: Decode datetime without dateutils if possible

pull/475/head
Raphaël Vinot 2019-10-10 18:27:47 +02:00
parent c80d35fa75
commit 6cc7730d24
2 changed files with 15 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import datetime
from deprecated import deprecated
from json import JSONEncoder
from uuid import UUID
try:
from rapidjson import load
@ -123,6 +124,8 @@ class MISPEncode(JSONEncoder):
return obj.isoformat()
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, UUID):
return str(obj)
return JSONEncoder.default(self, obj)
@ -134,6 +137,8 @@ if HAS_RAPIDJSON:
return obj.isoformat()
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, UUID):
return str(obj)
return rapidjson.default(obj)
else:
def pymisp_json_default(obj):
@ -143,6 +148,8 @@ else:
return obj.isoformat()
elif isinstance(obj, Enum):
return obj.value
elif isinstance(obj, UUID):
return str(obj)
return json.default(obj)

View File

@ -220,7 +220,14 @@ class MISPAttribute(AbstractMISP):
if self.value is None:
raise NewAttributeError('The value of the attribute is required.')
if self.type == 'datetime' and isinstance(self.value, str):
self.value = parse(self.value)
try:
if '.' in self.value:
self.value = datetime.datetime.strptime(self.value, "%Y-%m-%dT%H:%M:%S.%f")
else:
self.value = datetime.datetime.strptime(self.value, "%Y-%m-%dT%H:%M:%S")
except ValueError:
# Slower, but if the other ones fail, that's a good fallback
self.value = parse(self.value)
# Default values
self.category = kwargs.pop('category', type_defaults['default_category'])