Change _STIXBase to output properties in spec order. closes #39

stix2.1
Emmanuelle Vargas-Gonzalez 2017-08-11 15:10:44 -04:00
parent 4763695ad5
commit 5172f86a7b
1 changed files with 10 additions and 5 deletions

View File

@ -3,7 +3,7 @@
import collections
import copy
import datetime as dt
import json
import simplejson as json
from .exceptions import (AtLeastOnePropertyError, DependentPropertiesError,
ExtraPropertiesError, ImmutableError,
@ -36,6 +36,9 @@ def get_required_properties(properties):
class _STIXBase(collections.Mapping):
"""Base class for STIX object types"""
def _object_properties(self):
return list(self._properties.keys())
def _check_property(self, prop_name, prop, kwargs):
if prop_name not in kwargs:
if hasattr(prop, 'default'):
@ -141,12 +144,14 @@ class _STIXBase(collections.Mapping):
super(_STIXBase, self).__setattr__(name, value)
def __str__(self):
# TODO: put keys in specific order. Probably need custom JSON encoder.
return json.dumps(self, indent=4, sort_keys=True, cls=STIXJSONEncoder,
separators=(",", ": ")) # Don't include spaces after commas.
properties = self._object_properties()
# separators kwarg -> don't include spaces after commas.
return json.dumps(self, indent=4, cls=STIXJSONEncoder,
item_sort_key=lambda x: properties.index(x[0]),
separators=(",", ": "))
def __repr__(self):
props = [(k, self[k]) for k in sorted(self._properties) if self.get(k)]
props = [(k, self[k]) for k in self._object_properties() if self.get(k)]
return "{0}({1})".format(self.__class__.__name__,
", ".join(["{0!s}={1!r}".format(k, v) for k, v in props]))