diff --git a/stix2/base.py b/stix2/base.py index cda1766..292185e 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -36,34 +36,13 @@ def get_required_properties(properties): class _STIXBase(collections.Mapping): """Base class for STIX object types""" - # TODO: remove this - def _handle_old_style_property(self, prop_name, prop_metadata, kwargs): - cls = self.__class__ - class_name = cls.__name__ - - if prop_name not in kwargs: - if prop_metadata.get('default'): - default = prop_metadata['default'] - if default == NOW: - kwargs[prop_name] = self.__now - - if prop_metadata.get('validate'): - if (prop_name in kwargs and - not prop_metadata['validate'](cls, kwargs[prop_name])): - msg = prop_metadata.get('error_msg', DEFAULT_ERROR).format( - type=class_name, - field=prop_name, - expected=prop_metadata.get('expected', - prop_metadata.get('default', lambda x: ''))(cls), - ) - raise ValueError(msg) - def _check_property(self, prop_name, prop, kwargs): if prop_name not in kwargs: if hasattr(prop, 'default'): - kwargs[prop_name] = prop.default() - # if default == NOW: - # kwargs[prop_name] = self.__now + value = prop.default() + if value == NOW: + value = self.__now + kwargs[prop_name] = value if prop_name in kwargs: try: @@ -94,11 +73,7 @@ class _STIXBase(collections.Mapping): raise ValueError(msg.format(type=class_name, fields=field_list)) for prop_name, prop_metadata in cls._properties.items(): - - if isinstance(prop_metadata, dict): - self._handle_old_style_property(prop_name, prop_metadata, kwargs) - else: # This is a Property Subclasses - self._check_property(prop_name, prop_metadata, kwargs) + self._check_property(prop_name, prop_metadata, kwargs) self._inner = kwargs diff --git a/stix2/common.py b/stix2/common.py index 78a5e7a..43f3105 100644 --- a/stix2/common.py +++ b/stix2/common.py @@ -8,12 +8,8 @@ from .utils import NOW COMMON_PROPERTIES = { # 'type' and 'id' should be defined on each individual type - 'created': { - 'default': NOW, - }, - 'modified': { - 'default': NOW, - }, + 'created': Property(default=lambda: NOW), + 'modified': Property(default=lambda: NOW), 'external_references': Property(), 'revoked': BooleanProperty(), 'created_by_ref': ReferenceProperty(), diff --git a/stix2/properties.py b/stix2/properties.py index 3bb2633..bf49175 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -1,6 +1,8 @@ import re import uuid +from .utils import NOW + class Property(object): """Represent a property of STIX data type. diff --git a/stix2/sdo.py b/stix2/sdo.py index c21cb47..6be1518 100644 --- a/stix2/sdo.py +++ b/stix2/sdo.py @@ -125,9 +125,7 @@ class Indicator(_STIXBase): 'name': Property(), 'description': Property(), 'pattern': Property(required=True), - 'valid_from': { - 'default': NOW, - }, + 'valid_from': Property(default=lambda: NOW), 'valid_until': Property(), 'kill_chain_phases': Property(), })