Skip to content

Commit

Permalink
Code cleanup. Mostly fixing up imports. Use utils methods where appli…
Browse files Browse the repository at this point in the history
…cable.
  • Loading branch information
Bryan Worrell committed Mar 1, 2015
1 parent 70b27a8 commit 0e7c66d
Show file tree
Hide file tree
Showing 37 changed files with 206 additions and 260 deletions.
41 changes: 5 additions & 36 deletions stix/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,10 @@
import json

# external
import lxml
import lxml.etree

# internal
from . import bindings

# lazy imports
cybox = None
cybox_common = None
utils = None
nsparser = None

__LAZY_MODS_LOADED = False

def _load_lazy_mods():
global cybox, cybox_common, utils, nsparser
global __LAZY_MODS_LOADED

if __LAZY_MODS_LOADED:
return

if not cybox:
import cybox
if not cybox_common:
import cybox.common as cybox_common
if not utils:
import stix.utils as utils
if not nsparser:
import stix.utils.nsparser as nsparser

__LAZY_MODS_LOADED = True
from . import bindings, utils


def _override(*args, **kwargs):
Expand Down Expand Up @@ -108,7 +82,8 @@ def to_xml(self, include_namespaces=True, include_schemalocs=True,
:class:`Entity` instance. Default character encoding is ``utf-8``.
"""
_load_lazy_mods()

nsparser = utils.nsparser
parser = nsparser.NamespaceParser()

if auto_namespace:
Expand Down Expand Up @@ -197,8 +172,6 @@ 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]
Expand Down Expand Up @@ -244,7 +217,6 @@ def dict_from_object(cls, entity_obj):
return cls.from_obj(entity_obj).to_dict()

def walk(self):
_load_lazy_mods()
return utils.walk.iterwalk(self)

def find(self, id_):
Expand Down Expand Up @@ -371,7 +343,6 @@ 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 utils.is_sequence(list_repr):
return None
Expand Down Expand Up @@ -416,7 +387,6 @@ class TypedList(collections.MutableSequence):
_contained_type = _override

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

for arg in args:
Expand Down Expand Up @@ -479,7 +449,6 @@ 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 @@ -497,7 +466,7 @@ def from_obj(cls, obj_list, contained_type=None):

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


if not utils.is_sequence(list_repr):
return None
Expand Down
2 changes: 1 addition & 1 deletion stix/bindings/ttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
obj_ = stix_common_binding.IdentityType.factory() # IdentityType is not abstract

obj_.build(child_)
self.set_Identity(obj_)
self.set_Persona(obj_)
# end class PersonasType

class BehaviorType(GeneratedsSuper):
Expand Down
40 changes: 23 additions & 17 deletions stix/coa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

from datetime import datetime
from stix.utils import dates
from dateutil.tz import tzutc
# external
from cybox.core import Observables

# internal
import stix
import stix.utils
import stix.bindings.course_of_action as coa_binding
from stix.common import StructuredText, VocabString, InformationSource, Statement
from stix.common.related import (GenericRelationshipList, RelatedCOA,
RelatedPackageRefs)
import stix.utils as utils
from stix.data_marking import Marking
from stix.common import (
vocabs, related, StructuredText, VocabString, InformationSource, Statement
)
import stix.bindings.course_of_action as coa_binding

# relative
from .objective import Objective

from stix.common.vocabs import COAStage as Stage
from stix.common.vocabs import CourseOfActionType as COAType

class RelatedCOAs(GenericRelationshipList):
# Redefines
Stage = vocabs.COAStage
COAType = vocabs.CourseOfActionType


class RelatedCOAs(related.GenericRelationshipList):
_namespace = "http://stix.mitre.org/CourseOfAction-1"
_binding = coa_binding
_binding_class = coa_binding.RelatedCOAsType
_binding_var = "Related_COA"
_contained_type = RelatedCOA
_contained_type = related.RelatedCOA
_inner_name = "coas"


class CourseOfAction(stix.Entity):
_binding = coa_binding
_binding_class = coa_binding.CourseOfActionType
Expand All @@ -50,12 +56,12 @@ def __init__(self, id_=None, idref=None, timestamp=None, title=None, description
self.information_source = None
self.handling = None
self.related_coas = RelatedCOAs()
self.related_packages = RelatedPackageRefs()
self.related_packages = related.RelatedPackageRefs()

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 @@ -102,7 +108,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 @@ -312,7 +318,7 @@ def from_obj(cls, obj, return_obj=None):
return_obj.related_coas = \
RelatedCOAs.from_obj(obj.Related_COAs)
return_obj.related_packages = \
RelatedPackageRefs.from_obj(obj.Related_Packages)
related.RelatedPackageRefs.from_obj(obj.Related_Packages)

return return_obj

Expand Down Expand Up @@ -386,7 +392,7 @@ def from_dict(cls, dict_repr, return_obj=None):
return_obj.related_coas = \
RelatedCOAs.from_dict(dict_repr.get('related_coas'))
return_obj.related_packages = \
RelatedPackageRefs.from_dict(dict_repr.get('related_packages'))
related.RelatedPackageRefs.from_dict(dict_repr.get('related_packages'))

return return_obj

Expand Down
57 changes: 27 additions & 30 deletions stix/common/confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

from __future__ import absolute_import

from datetime import datetime
import dateutil
from dateutil.tz import tzutc

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

from .structured_text import StructuredText
Expand All @@ -19,7 +17,7 @@ class Confidence(stix.Entity):
_binding_class = common_binding.ConfidenceType

def __init__(self, value=None, timestamp=None, description=None, source=None):
self.timestamp = timestamp or datetime.now(tzutc())
self.timestamp = timestamp or utils.dates.now()
self.timestamp_precision = "second"
self.value = value
self.description = description
Expand All @@ -33,12 +31,7 @@ def timestamp(self):

@timestamp.setter
def timestamp(self, value):
if not value:
self._timestamp = None
elif isinstance(value, datetime):
self._timestamp = value
else:
self._timestamp = dateutil.parser.parse(value)
self._timestamp = utils.dates.parse_value(value)

@property
def value(self):
Expand Down Expand Up @@ -110,7 +103,7 @@ def to_obj(self, return_obj=None, ns_info=None):
def to_dict(self):
d = {}
if self.timestamp:
d['timestamp'] = self.timestamp.isoformat()
d['timestamp'] = utils.dates.serialize_value(self.timestamp)
if self.timestamp_precision != 'second':
d['timestamp_precision'] = self.timestamp_precision
if self.value:
Expand All @@ -122,37 +115,41 @@ def to_dict(self):

return d

@staticmethod
def from_obj(obj):
@classmethod
def from_obj(cls, obj, return_obj=None):
from .information_source import InformationSource

if not obj:
return None
c = Confidence()

c.timestamp = obj.timestamp
c.timestamp_precision = obj.timestamp_precision
c.value = VocabString.from_obj(obj.Value)
c.description = StructuredText.from_obj(obj.Description)
c.source = InformationSource.from_obj(obj.Source)
if not return_obj:
return_obj = cls()

return c
return_obj.timestamp = obj.timestamp
return_obj.timestamp_precision = obj.timestamp_precision
return_obj.value = VocabString.from_obj(obj.Value)
return_obj.description = StructuredText.from_obj(obj.Description)
return_obj.source = InformationSource.from_obj(obj.Source)

@staticmethod
def from_dict(dict_):
return return_obj

@classmethod
def from_dict(cls, d, return_obj=None):
from .information_source import InformationSource

if dict_ is None:
if not d:
return None
c = Confidence()

if not return_obj:
return_obj = cls()

c.timestamp = dict_.get('timestamp')
c.timestamp_precision = dict_.get('timestamp_precision', 'second')
c.value = VocabString.from_dict(dict_.get('value'))
c.description = StructuredText.from_dict(dict_.get('description'))
c.source = InformationSource.from_dict(dict_.get('source'))
return_obj.timestamp = d.get('timestamp')
return_obj.timestamp_precision = d.get('timestamp_precision', 'second')
return_obj.value = VocabString.from_dict(d.get('value'))
return_obj.description = StructuredText.from_dict(d.get('description'))
return_obj.source = InformationSource.from_dict(d.get('source'))

return c
return return_obj

class ConfidenceAssertionChain(stix.Entity):
_namespace = 'http://stix.mitre.org/common-2'
Expand Down
11 changes: 5 additions & 6 deletions stix/common/datetimewithprecision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# See LICENSE.txt for complete terms.

import stix
import stix.utils as utils
import stix.bindings.stix_common as common_binding
import dateutil
from stix.utils.dates import parse_value, serialize_value

DATE_PRECISION_VALUES = ("year", "month", "day")
TIME_PRECISION_VALUES = ("hour", "minute", "second")
Expand All @@ -26,7 +25,7 @@ def value(self):

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

@property
def precision(self):
Expand All @@ -43,7 +42,7 @@ def to_obj(self, return_obj=None, ns_info=None):
super(DateTimeWithPrecision, self).to_obj(return_obj=return_obj, ns_info=ns_info)

obj = self._binding_class()
obj.valueOf_ = serialize_value(self.value)
obj.valueOf_ = utils.dates.serialize_value(self.value)
obj.precision = self.precision
return obj

Expand All @@ -61,10 +60,10 @@ def from_obj(cls, obj, return_obj=None):

def to_dict(self):
if self.precision == 'second':
return serialize_value(self.value)
return utils.dates.serialize_value(self.value)

d = {}
d['value'] = serialize_value(self.value)
d['value'] = utils.dates.serialize_value(self.value)
d['precision'] = self.precision
return d

Expand Down
3 changes: 1 addition & 2 deletions stix/common/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from __future__ import absolute_import

import stix
import stix.bindings.stix_common as common_binding
import stix.bindings.extensions.identity.ciq_identity_3_0 as ciq_identity_binding
import stix.utils
import stix.bindings.stix_common as common_binding

# import of RelatedIdentity is below

Expand Down
4 changes: 2 additions & 2 deletions stix/common/information_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def description(self):

@description.setter
def description(self, value):
'''Sets the value of the description property.
"""Sets the value of the description property.
If the value is an instance of basestring, it will be coerced into an
instance of StructuredText, with its 'text' property set to the input
value.
'''
"""
if value and isinstance(value, basestring):
st = StructuredText()
st.value = value
Expand Down

0 comments on commit 0e7c66d

Please sign in to comment.