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 = []
for p in list_of_properties:
for dp in list_of_dependent_properties:
if ((not hasattr(self, p) or (hasattr(self, p) and not self.__getattr__(p))) and
hasattr(self, dp) and self.__getattr__(dp)):
if not self.get(p) and self.get(dp):
failed_dependency_pairs.append((p, dp))
if 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
def __getattr__(self, name):
try:
# Return attribute value.
if name in self:
return self.__getitem__(name)
except KeyError:
raise AttributeError("'%s' object has no attribute '%s'" %
(self.__class__.__name__, name))
@ -160,7 +157,7 @@ class _STIXBase(collections.Mapping):
def new_version(self, **kwargs):
unchangable_properties = []
if hasattr(self, 'revoked') and self.revoked:
if self.get("revoked"):
raise RevokeError("new_version")
new_obj_inner = copy.deepcopy(self._inner)
properties_to_change = kwargs.keys()
@ -180,7 +177,7 @@ class _STIXBase(collections.Mapping):
return cls(**new_obj_inner)
def revoke(self):
if hasattr(self, 'revoked') and self.revoked:
if self.get("revoked"):
raise RevokeError("revoke")
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 .exceptions import AtLeastOnePropertyError
from .exceptions import AtLeastOnePropertyError, DependentPropertiesError
from .properties import (BinaryProperty, BooleanProperty, DictionaryProperty,
EmbeddedObjectProperty, EnumProperty,
ExtensionsProperty, FloatProperty, HashesProperty,
@ -110,7 +110,11 @@ class EmailMessage(_Observable):
def _check_object_constraints(self):
super(EmailMessage, self)._check_object_constraints()
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):
@ -443,13 +447,13 @@ class Process(_Observable):
super(Process, self)._check_object_constraints()
try:
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()
except AtLeastOnePropertyError as enclosing_exc:
if not hasattr(self, 'extensions'):
raise enclosing_exc
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()