Skip to content

Commit

Permalink
chg: Multiple changes
Browse files Browse the repository at this point in the history
* Fix timestamp dump (properly enforce UTC)
* Properly handle proposals
* Add many getter/setter
* Add dedicated test cases for MISPEvent and other objects
  • Loading branch information
Rafiot committed Jan 4, 2018
1 parent c68b69b commit bb1aac5
Show file tree
Hide file tree
Showing 16 changed files with 5,363 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -26,7 +26,7 @@ install:
- popd

script:
- nosetests --with-coverage --cover-package=pymisp tests/test_offline.py
- nosetests --with-coverage --cover-package=pymisp tests/test_*.py

after_success:
- codecov
Expand Down
25 changes: 21 additions & 4 deletions pymisp/abstract.py
Expand Up @@ -15,6 +15,21 @@
if six.PY2:
logger.warning("You're using python 2, it is strongly recommended to use python >=3.5")

# This is required because Python 2 is a pain.
from datetime import tzinfo, timedelta

class UTC(tzinfo):
"""UTC"""

def utcoffset(self, dt):
return timedelta(0)

def tzname(self, dt):
return "UTC"

def dst(self, dt):
return timedelta(0)


class MISPEncode(JSONEncoder):

Expand Down Expand Up @@ -80,6 +95,8 @@ def to_dict(self):
val = getattr(self, attribute, None)
if val is None:
continue
elif isinstance(val, list) and len(val) == 0:
continue
if attribute == 'timestamp':
if self.edited:
# In order to be accepted by MISP, the timestamp of an object
Expand All @@ -98,7 +115,7 @@ def jsonable(self):

def to_json(self):
"""Dump recursively any class of type MISPAbstract to a json string"""
return json.dumps(self, cls=MISPEncode)
return json.dumps(self, cls=MISPEncode, sort_keys=True, indent=2)

def __getitem__(self, key):
try:
Expand Down Expand Up @@ -150,10 +167,10 @@ def __setattr__(self, name, value):

def _datetime_to_timestamp(self, d):
"""Convert a datetime.datetime object to a timestamp (int)"""
if isinstance(d, (int, str)):
if isinstance(d, (int, str)) or (sys.version_info < (3, 0) and isinstance(d, unicode)):
# Assume we already have a timestamp
return d
if sys.version_info >= (3, 3):
return d.timestamp()
return int(d.timestamp())
else:
return (d - datetime.datetime.utcfromtimestamp(0)).total_seconds()
return int((d - datetime.datetime.fromtimestamp(0, UTC())).total_seconds())

0 comments on commit bb1aac5

Please sign in to comment.