Minor Code Changes.

stix2.1
Emmanuelle Vargas-Gonzalez 2017-06-08 08:42:32 -04:00
parent 860efcc230
commit b9f63e8de6
2 changed files with 14 additions and 13 deletions

View File

@ -73,8 +73,7 @@ class _STIXBase(collections.Mapping):
failed_dependency_pairs = [] failed_dependency_pairs = []
for p in list_of_properties: for p in list_of_properties:
for dp in list_of_dependent_properties: for dp in list_of_dependent_properties:
if ((not hasattr(self, p) or (hasattr(self, p) and not self.__getattr__(p))) and if not self.get(p) and self.get(dp):
hasattr(self, dp) and self.__getattr__(dp)):
failed_dependency_pairs.append((p, dp)) failed_dependency_pairs.append((p, dp))
if failed_dependency_pairs: if failed_dependency_pairs:
raise DependentPropertiesError(self.__class__, failed_dependency_pairs) raise DependentPropertiesError(self.__class__, failed_dependency_pairs)
@ -125,10 +124,8 @@ class _STIXBase(collections.Mapping):
# Handle attribute access just like key access # Handle attribute access just like key access
def __getattr__(self, name): def __getattr__(self, name):
try: if name in self:
# Return attribute value.
return self.__getitem__(name) return self.__getitem__(name)
except KeyError:
raise AttributeError("'%s' object has no attribute '%s'" % raise AttributeError("'%s' object has no attribute '%s'" %
(self.__class__.__name__, name)) (self.__class__.__name__, name))
@ -160,7 +157,7 @@ class _STIXBase(collections.Mapping):
def new_version(self, **kwargs): def new_version(self, **kwargs):
unchangable_properties = [] unchangable_properties = []
if hasattr(self, 'revoked') and self.revoked: if self.get("revoked"):
raise RevokeError("new_version") raise RevokeError("new_version")
new_obj_inner = copy.deepcopy(self._inner) new_obj_inner = copy.deepcopy(self._inner)
properties_to_change = kwargs.keys() properties_to_change = kwargs.keys()
@ -180,7 +177,7 @@ class _STIXBase(collections.Mapping):
return cls(**new_obj_inner) return cls(**new_obj_inner)
def revoke(self): def revoke(self):
if hasattr(self, 'revoked') and self.revoked: if self.get("revoked"):
raise RevokeError("revoke") raise RevokeError("revoke")
return self.new_version(revoked=True) return self.new_version(revoked=True)

View File

@ -6,7 +6,7 @@ and do not have a '_type' attribute.
""" """
from .base import _Extension, _Observable, _STIXBase from .base import _Extension, _Observable, _STIXBase
from .exceptions import AtLeastOnePropertyError from .exceptions import AtLeastOnePropertyError, DependentPropertiesError
from .properties import (BinaryProperty, BooleanProperty, DictionaryProperty, from .properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty, EmbeddedObjectProperty, EnumProperty,
ExtensionsProperty, FloatProperty, HashesProperty, ExtensionsProperty, FloatProperty, HashesProperty,
@ -110,7 +110,11 @@ class EmailMessage(_Observable):
def _check_object_constraints(self): def _check_object_constraints(self):
super(EmailMessage, self)._check_object_constraints() super(EmailMessage, self)._check_object_constraints()
self._check_properties_dependency(["is_multipart"], ["body_multipart"]) self._check_properties_dependency(["is_multipart"], ["body_multipart"])
# self._dependency(["is_multipart"], ["body"], [False]) if self.get("is_multipart") is False and self.get("body"):
raise DependentPropertiesError(
self.__class__,
(self.get("is_multipart"), self.get("body"))
)
class ArchiveExt(_Extension): class ArchiveExt(_Extension):
@ -443,13 +447,13 @@ class Process(_Observable):
super(Process, self)._check_object_constraints() super(Process, self)._check_object_constraints()
try: try:
self._check_at_least_one_property() self._check_at_least_one_property()
if hasattr(self, 'extensions') and "windows-process-ext" in self.extensions: if "windows-process-ext" in self.get("extensions", []):
self.extensions["windows-process-ext"]._check_at_least_one_property() self.extensions["windows-process-ext"]._check_at_least_one_property()
except AtLeastOnePropertyError as enclosing_exc: except AtLeastOnePropertyError as enclosing_exc:
if not hasattr(self, 'extensions'): if not hasattr(self, 'extensions'):
raise enclosing_exc raise enclosing_exc
else: else:
if "windows-process-ext" in self.extensions: if "windows-process-ext" in self.get("extensions", []):
self.extensions["windows-process-ext"]._check_at_least_one_property() self.extensions["windows-process-ext"]._check_at_least_one_property()