Skip to content

Commit

Permalink
More impls of TypedList
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Worrell committed Mar 2, 2015
1 parent 3b86d0c commit e8da174
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 154 deletions.
76 changes: 27 additions & 49 deletions stix/common/information_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,10 @@ def contributing_sources(self):

@contributing_sources.setter
def contributing_sources(self, value):
self._contributing_sources = ContributingSources()
if not value:
return
elif isinstance(value, ContributingSources):
self._contributing_sources = value
elif utils.is_sequence(value):
for v in value:
self.add_contributing_source(v)
else:
self.add_contributing_source(value)
self._contributing_sources = ContributingSources(value)

def add_contributing_source(self, value):
if not value:
return
else:
self.contributing_sources.append(value) # input checks performed by stix.EntityList
self.contributing_sources.append(value)

@property
def references(self):
Expand Down Expand Up @@ -125,7 +113,8 @@ def tools(self):
@tools.setter
def tools(self, value):
if value and not isinstance(value, cybox.common.ToolInformationList):
raise ValueError('value must be instance of cybox.common.ToolInformationList')
error = 'value must be instance of cybox.common.ToolInformationList'
raise ValueError(error)

self._tools = value

Expand All @@ -135,36 +124,24 @@ def roles(self):

@roles.setter
def roles(self, value):
self._roles = Roles()

if not value:
return
elif utils.is_sequence(value):
for v in value:
self.add_role(v)
else:
self.add_role(value)
self._roles = Roles(value)

def add_role(self, value):
if not value:
return
elif isinstance(value, VocabString):
self.roles.append(value)
else:
role = vocabs.InformationSourceRole(value)
self.roles.append(value=role)
self.roles.append(value)

def to_obj(self, return_obj=None, ns_info=None):
super(InformationSource, self).to_obj(return_obj=return_obj, ns_info=ns_info)
super(InformationSource, self).to_obj(
return_obj=return_obj,
ns_info=ns_info
)

if not return_obj:
return_obj = self._binding_class()

if self.description is not None:
return_obj.Description = self.description.to_obj(ns_info=ns_info)
if self.references:
references_obj = stix_common_binding.ReferencesType(Reference=self.references)
return_obj.References = references_obj
return_obj.References = stix_common_binding.ReferencesType(Reference=self.references)
if self.contributing_sources:
return_obj.Contributing_Sources = self.contributing_sources.to_obj(ns_info=ns_info)
if self.identity:
Expand All @@ -174,7 +151,8 @@ def to_obj(self, return_obj=None, ns_info=None):
if self.tools:
return_obj.Tools = self.tools.to_obj(ns_info=ns_info)
if self.roles:
return_obj.Role = [x.to_obj(ns_info=ns_info) for x in self.roles]
return_obj.Role = self.roles.to_obj(ns_info=ns_info)

return return_obj

@classmethod
Expand All @@ -187,16 +165,15 @@ def from_obj(cls, obj, return_obj=None):
return_obj.description = StructuredText.from_obj(obj.Description)
return_obj.identity = Identity.from_obj(obj.Identity)
return_obj.contributing_sources = ContributingSources.from_obj(obj.Contributing_Sources)

return_obj.roles = Roles.from_obj(obj.Role)

if obj.References:
return_obj.references = obj.References.Reference
if obj.Time:
return_obj.time = cybox.common.Time.from_obj(obj.Time)
if obj.Tools:
return_obj.tools = cybox.common.ToolInformationList.from_obj(obj.Tools)
if obj.Role:
return_obj.roles = [VocabString.from_obj(x) for x in obj.Role]


return return_obj

@classmethod
Expand All @@ -209,13 +186,14 @@ def from_dict(cls, dict_repr, return_obj=None):
if not return_obj:
return_obj = cls()

return_obj.description = StructuredText.from_dict(dict_repr.get('description'))
return_obj.references = dict_repr.get('references')
return_obj.contributing_sources = ContributingSources.from_dict(dict_repr.get('contributing_sources'))
return_obj.identity = Identity.from_dict(dict_repr.get('identity'))
return_obj.time = cybox.common.Time.from_dict(dict_repr.get('time'))
return_obj.tools = cybox.common.ToolInformationList.from_list(dict_repr.get('tools'))
return_obj.roles = [VocabString.from_dict(x) for x in dict_repr.get('roles', [])]
get = dict_repr.get
return_obj.description = StructuredText.from_dict(get('description'))
return_obj.references = get('references')
return_obj.contributing_sources = ContributingSources.from_dict(get('contributing_sources'))
return_obj.identity = Identity.from_dict(get('identity'))
return_obj.time = cybox.common.Time.from_dict(get('time'))
return_obj.tools = cybox.common.ToolInformationList.from_list(get('tools'))
return_obj.roles = Roles.from_dict(get('roles'))

return return_obj

Expand All @@ -234,7 +212,8 @@ def to_dict(self):
if self.contributing_sources:
d['contributing_sources'] = self.contributing_sources.to_dict()
if self.roles:
d['roles'] = [x.to_dict() for x in self.roles]
d['roles'] = self.roles.to_list()

return d

class ContributingSources(stix.EntityList):
Expand All @@ -246,8 +225,7 @@ class ContributingSources(stix.EntityList):
_inner_name = "sources"


class Roles(stix.EntityList):
_namespace = "http://stix.mitre.org/common-1"
class Roles(stix.TypedList):
_contained_type = VocabString

def _fix_value(self, value):
Expand Down
49 changes: 18 additions & 31 deletions stix/core/stix_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from stix.common import vocabs, InformationSource, StructuredText, VocabString
from stix.data_marking import Marking


class STIXHeader(stix.Entity):
_binding = stix_core_binding
_namespace = 'http://stix.mitre.org/stix-1'
Expand Down Expand Up @@ -94,23 +95,11 @@ def package_intents(self):

@package_intents.setter
def package_intents(self, value):
self._package_intents = PackageIntents()
if not value:
return
elif utils.is_sequence(value):
for v in value:
self.add_package_intent(v)
else:
self.add_package_intent(value)
self._package_intents = PackageIntents(value)

def add_package_intent(self, package_intent):
if not package_intent:
return
elif isinstance(package_intent, VocabString):
self.package_intents.append(package_intent)
else:
tmp_package_intent = vocabs.PackageIntent(value=package_intent)
self.package_intents.append(tmp_package_intent)
self.package_intents.append(package_intent)


@property
def information_source(self):
Expand Down Expand Up @@ -140,11 +129,8 @@ def from_obj(cls, obj, return_obj=None):
return_obj.short_description = StructuredText.from_obj(obj.Short_Description)
return_obj.handling = Marking.from_obj(obj.Handling)
return_obj.information_source = InformationSource.from_obj(obj.Information_Source)

if obj.Package_Intent:
return_obj.package_intents = [VocabString.from_obj(x) for x in obj.Package_Intent]
if obj.Profiles:
return_obj.profiles = obj.Profiles.Profile
return_obj.package_intents = PackageIntents.from_obj(obj.Package_Intent)
return_obj.profiles = obj.Profiles.Profile if obj.Profiles else []

return return_obj

Expand All @@ -157,7 +143,7 @@ def to_obj(self, return_obj=None, ns_info=None):
if self.title:
return_obj.Title = self.title
if self.package_intents:
return_obj.Package_Intent = [x.to_obj(ns_info=ns_info) for x in self.package_intents]
return_obj.Package_Intent = PackageIntents.to_obj(ns_info=ns_info)
if self.description:
return_obj.Description = self.description.to_obj(ns_info=ns_info)
if self.short_description:
Expand All @@ -180,13 +166,15 @@ def from_dict(cls, dict_repr, return_obj=None):
if not return_obj:
return_obj = cls()

return_obj.title = dict_repr.get('title')
return_obj.package_intents = [VocabString.from_dict(x) for x in dict_repr.get('package_intents', [])]
return_obj.description = StructuredText.from_dict(dict_repr.get('description'))
return_obj.short_description = StructuredText.from_dict(dict_repr.get('short_description'))
return_obj.handling = Marking.from_dict(dict_repr.get('handling'))
return_obj.information_source = InformationSource.from_dict(dict_repr.get('information_source'))
return_obj.profiles = dict_repr.get('profiles')
get = dict_repr.get

return_obj.title = get('title')
return_obj.package_intents = PackageIntents.from_list(get('package_intents'))
return_obj.description = StructuredText.from_dict(get('description'))
return_obj.short_description = StructuredText.from_dict(get('short_description'))
return_obj.handling = Marking.from_dict(get('handling'))
return_obj.information_source = InformationSource.from_dict(get('information_source'))
return_obj.profiles = get('profiles')

return return_obj

Expand All @@ -195,7 +183,7 @@ def to_dict(self):
if self.title:
d['title'] = self.title
if self.package_intents:
d['package_intents'] = [x.to_dict() for x in self.package_intents]
d['package_intents'] = self.package_intents.to_list()
if self.description:
d['description'] = self.description.to_dict()
if self.short_description:
Expand All @@ -210,8 +198,7 @@ def to_dict(self):
return d


class PackageIntents(stix.EntityList):
_namespace = "http://stix.mitre.org/stix-1"
class PackageIntents(stix.TypedList):
_contained_type = VocabString

def _fix_value(self, value):
Expand Down

0 comments on commit e8da174

Please sign in to comment.