From b9f63e8de69a3282b49c07357e5a30f6883b362a Mon Sep 17 00:00:00 2001 From: Emmanuelle Vargas-Gonzalez Date: Thu, 8 Jun 2017 08:42:32 -0400 Subject: [PATCH] Minor Code Changes. --- stix2/base.py | 15 ++++++--------- stix2/observables.py | 12 ++++++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index c5b52aa..c40c9e6 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -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,12 +124,10 @@ 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)) + raise AttributeError("'%s' object has no attribute '%s'" % + (self.__class__.__name__, name)) def __setattr__(self, name, value): if name != '_inner' and not name.startswith("_STIXBase__"): @@ -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) diff --git a/stix2/observables.py b/stix2/observables.py index 89d35c6..bb4ea31 100644 --- a/stix2/observables.py +++ b/stix2/observables.py @@ -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()