Convert remaining properties from dicts to classes

stix2.1
Greg Back 2017-03-21 20:15:06 -04:00
parent 9bcf064213
commit 79554a6491
4 changed files with 10 additions and 39 deletions

View File

@ -36,34 +36,13 @@ def get_required_properties(properties):
class _STIXBase(collections.Mapping): class _STIXBase(collections.Mapping):
"""Base class for STIX object types""" """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): def _check_property(self, prop_name, prop, kwargs):
if prop_name not in kwargs: if prop_name not in kwargs:
if hasattr(prop, 'default'): if hasattr(prop, 'default'):
kwargs[prop_name] = prop.default() value = prop.default()
# if default == NOW: if value == NOW:
# kwargs[prop_name] = self.__now value = self.__now
kwargs[prop_name] = value
if prop_name in kwargs: if prop_name in kwargs:
try: try:
@ -94,11 +73,7 @@ class _STIXBase(collections.Mapping):
raise ValueError(msg.format(type=class_name, fields=field_list)) raise ValueError(msg.format(type=class_name, fields=field_list))
for prop_name, prop_metadata in cls._properties.items(): for prop_name, prop_metadata in cls._properties.items():
self._check_property(prop_name, prop_metadata, kwargs)
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._inner = kwargs self._inner = kwargs

View File

@ -8,12 +8,8 @@ from .utils import NOW
COMMON_PROPERTIES = { COMMON_PROPERTIES = {
# 'type' and 'id' should be defined on each individual type # 'type' and 'id' should be defined on each individual type
'created': { 'created': Property(default=lambda: NOW),
'default': NOW, 'modified': Property(default=lambda: NOW),
},
'modified': {
'default': NOW,
},
'external_references': Property(), 'external_references': Property(),
'revoked': BooleanProperty(), 'revoked': BooleanProperty(),
'created_by_ref': ReferenceProperty(), 'created_by_ref': ReferenceProperty(),

View File

@ -1,6 +1,8 @@
import re import re
import uuid import uuid
from .utils import NOW
class Property(object): class Property(object):
"""Represent a property of STIX data type. """Represent a property of STIX data type.

View File

@ -125,9 +125,7 @@ class Indicator(_STIXBase):
'name': Property(), 'name': Property(),
'description': Property(), 'description': Property(),
'pattern': Property(required=True), 'pattern': Property(required=True),
'valid_from': { 'valid_from': Property(default=lambda: NOW),
'default': NOW,
},
'valid_until': Property(), 'valid_until': Property(),
'kill_chain_phases': Property(), 'kill_chain_phases': Property(),
}) })