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):
"""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

View File

@ -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(),

View File

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

View File

@ -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(),
})