Skip to content

Commit

Permalink
Added utils.is_sequence() method for checking sequence types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Worrell committed Mar 1, 2015
1 parent 8fd207f commit 70b27a8
Show file tree
Hide file tree
Showing 24 changed files with 229 additions and 199 deletions.
20 changes: 13 additions & 7 deletions stix/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

__LAZY_MODS_LOADED = False


def _load_lazy_mods():
global cybox, cybox_common, utils, nsparser
global __LAZY_MODS_LOADED
Expand Down Expand Up @@ -198,14 +197,16 @@ def to_dict(self):
dictionary. This may be overridden by derived classes.
"""
_load_lazy_mods()

d = {}
for raw_key in self.__dict__.keys():
raw_value = self.__dict__[raw_key]

if raw_value:
if isinstance(raw_value, Entity):
value = raw_value.to_dict()
elif isinstance(raw_value, collections.MutableSequence):
elif utils.is_sequence(raw_value):
value = []
for x in raw_value:
if isinstance(x, Entity):
Expand Down Expand Up @@ -271,7 +272,7 @@ def __init__(self, *args):
self._inner = []

for arg in args:
if isinstance(arg, list):
if utils.is_sequence(arg):
self.extend(arg)
else:
self.append(arg)
Expand Down Expand Up @@ -370,8 +371,9 @@ def from_obj(cls, obj, return_obj=None, contained_type=None,

@classmethod
def from_list(cls, list_repr, return_obj=None, contained_type=None):
_load_lazy_mods()

if not isinstance(list_repr, list):
if not utils.is_sequence(list_repr):
return None

if return_obj is None:
Expand Down Expand Up @@ -414,10 +416,11 @@ class TypedList(collections.MutableSequence):
_contained_type = _override

def __init__(self, *args):
import stix.utils as utils
self._inner = []

for arg in args:
if isinstance(arg, list):
if utils.is_sequence(arg):
self.extend(arg)
else:
self.append(arg)
Expand Down Expand Up @@ -476,6 +479,8 @@ def to_list(self):

@classmethod
def from_obj(cls, obj_list, contained_type=None):
import stix.utils as utils

if not obj_list:
return None

Expand All @@ -484,16 +489,17 @@ def from_obj(cls, obj_list, contained_type=None):
if not contained_type:
contained_type = cls._contained_type

if not isinstance(obj_list, (list, tuple)):
if not utils.is_sequence(obj_list):
obj_list = [obj_list]

return_obj.extend(contained_type.from_obj(x) for x in obj_list)
return return_obj

@classmethod
def from_list(cls, list_repr, contained_type=None):
import stix.utils as utils

if not isinstance(list_repr, list):
if not utils.is_sequence(list_repr):
return None

return_obj = cls()
Expand Down
38 changes: 18 additions & 20 deletions stix/campaign/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.


from dateutil.tz import tzutc
from datetime import datetime

import stix
import stix.bindings.campaign as campaign_binding
from stix.common import (Activity, Confidence, InformationSource, Statement,
StructuredText, VocabString)
from stix.common.related import (GenericRelationshipList, RelatedCampaign,
RelatedIncident, RelatedIndicator, RelatedPackageRefs,
RelatedThreatActor, RelatedTTP)
import stix.utils as utils
from stix.common import vocabs
from stix.common import (
Activity, Confidence, InformationSource, Statement, StructuredText,
VocabString
)
from stix.common.related import (
GenericRelationshipList, RelatedCampaign,RelatedIncident, RelatedIndicator,
RelatedPackageRefs, RelatedThreatActor, RelatedTTP
)
from stix.data_marking import Marking
import stix.utils
from stix.utils import dates
from stix.common.vocabs import CampaignStatus, IntendedEffect
import stix.bindings.campaign as campaign_binding


class AssociatedCampaigns(GenericRelationshipList):
Expand Down Expand Up @@ -106,7 +104,7 @@ def __init__(self, id_=None, idref=None, timestamp=None, title=None, description
if timestamp:
self.timestamp = timestamp
else:
self.timestamp = datetime.now(tzutc()) if not idref else None
self.timestamp = utils.dates.now() if not idref else None

@property
def id_(self):
Expand Down Expand Up @@ -153,7 +151,7 @@ def timestamp(self):

@timestamp.setter
def timestamp(self, value):
self._timestamp = dates.parse_value(value)
self._timestamp = utils.dates.parse_value(value)

@property
def title(self):
Expand Down Expand Up @@ -200,7 +198,7 @@ def intended_effects(self, value):
self._intended_effects = []
if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_intended_effect(v)
else:
Expand All @@ -212,7 +210,7 @@ def add_intended_effect(self, value):
elif isinstance(value, Statement):
self.intended_effects.append(value)
else:
intended_effect = IntendedEffect(value)
intended_effect = vocabs.IntendedEffect(value)
self.intended_effects.append(Statement(value=intended_effect))

@property
Expand All @@ -226,7 +224,7 @@ def status(self, value):
elif isinstance(value, VocabString):
self._status = value
else:
self._status = CampaignStatus(value)
self._status = vocabs.CampaignStatus(value)

@property
def attribution(self):
Expand All @@ -242,7 +240,7 @@ def attribution(self, value):

if not value:
return
elif hasattr(value, '__getitem__'):
elif utils.is_sequence(value):
self._attribution = AttributionList(*value)
else:
self._attribution.append(value) # may raise a ValueError
Expand Down Expand Up @@ -340,7 +338,7 @@ def to_dict(self):
if self.idref:
d['idref'] = self.idref
if self.timestamp:
d['timestamp'] = self.timestamp.isoformat()
d['timestamp'] = utils.dates.serialize_value(self.timestamp)
if self.version:
d['version'] = self.version
if self.title:
Expand Down
7 changes: 4 additions & 3 deletions stix/common/information_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cybox.common

import stix
import stix.utils as utils
import stix.bindings.stix_common as stix_common_binding
from stix.common import VocabString
from stix.common.vocabs import InformationSourceRole
Expand Down Expand Up @@ -39,7 +40,7 @@ def contributing_sources(self, value):
return
elif isinstance(value, ContributingSources):
self._contributing_sources = value
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_contributing_source(v)
else:
Expand All @@ -60,7 +61,7 @@ def references(self, value):
self._references = []
if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_reference(v)
else:
Expand Down Expand Up @@ -139,7 +140,7 @@ def roles(self, value):

if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_role(v)
else:
Expand Down
4 changes: 2 additions & 2 deletions stix/common/kill_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE.txt for complete terms.

import stix
import stix.utils
import stix.utils as utils
import stix.bindings.stix_common as common_binding

class KillChain(stix.Entity):
Expand All @@ -27,7 +27,7 @@ def kill_chain_phases(self, value):
self._kill_chain_phases = []
if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_kill_chain_phase(v)
else:
Expand Down
3 changes: 2 additions & 1 deletion stix/core/stix_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# See LICENSE.txt for complete terms.

import stix
import stix.utils as utils
import stix.bindings.stix_common as stix_common_binding
import stix.bindings.stix_core as stix_core_binding
from stix.common import InformationSource, StructuredText, VocabString
Expand Down Expand Up @@ -98,7 +99,7 @@ def package_intents(self, value):
self._package_intents = PackageIntents()
if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_package_intent(v)
else:
Expand Down
14 changes: 7 additions & 7 deletions stix/core/stix_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def indicators(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_indicator(v)
else:
Expand Down Expand Up @@ -137,7 +137,7 @@ def campaigns(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_campaign(v)
else:
Expand Down Expand Up @@ -182,7 +182,7 @@ def incidents(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_incident(v)
else:
Expand Down Expand Up @@ -210,7 +210,7 @@ def threat_actors(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_threat_actor(v)
else:
Expand Down Expand Up @@ -238,7 +238,7 @@ def courses_of_action(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_course_of_action(v)
else:
Expand Down Expand Up @@ -266,7 +266,7 @@ def exploit_targets(self, value):

if not value:
return
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_exploit_target(v)
else:
Expand All @@ -292,7 +292,7 @@ def ttps(self, value):

if not value:
self._ttps = TTPs()
elif isinstance(value, (list, tuple)):
elif utils.is_sequence(value):
for v in value:
self.add_ttp(v)
else:
Expand Down
3 changes: 2 additions & 1 deletion stix/core/ttps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# See LICENSE.txt for complete terms.

import stix
import stix.utils as utils
from stix.ttp import TTP

import stix.bindings.stix_core as core_binding
Expand Down Expand Up @@ -31,7 +32,7 @@ def ttps(self, value):
self._inner = []
if not value:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for v in value:
self.add_ttp(v)
else:
Expand Down
8 changes: 3 additions & 5 deletions stix/data_marking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# See LICENSE.txt for complete terms.

import stix

from stix.bindings.data_marking import MarkingSpecificationType, MarkingStructureType, MarkingType
from stix.common import StructuredText
import stix.utils as utils
import stix.bindings.data_marking as stix_data_marking_binding

class Marking(stix.Entity):
Expand All @@ -25,7 +23,7 @@ def markings(self, value):

if value is None:
return
elif isinstance(value, list):
elif utils.is_sequence(value):
for m in value:
self.add_marking(m)
else:
Expand Down Expand Up @@ -80,7 +78,7 @@ def __init__(self, controlled_structure=None, marking_structures=None):
self.version = None
self.controlled_structure = controlled_structure

if isinstance(marking_structures,list):
if utils.is_sequence(marking_structures):
self.marking_structures = marking_structures
elif marking_structures is None:
self.marking_structures = []
Expand Down

0 comments on commit 70b27a8

Please sign in to comment.