Skip to content

Commit c3b3380

Browse files
author
Bryan Worrell
committed
Refactored report Header class to use TypedFields.
1 parent 1e41008 commit c3b3380

2 files changed

Lines changed: 15 additions & 164 deletions

File tree

stix/report/header.py

Lines changed: 14 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
22
# See LICENSE.txt for complete terms.
33

4+
from mixbox import fields
5+
46
import stix
57
import stix.bindings.report as report_binding
6-
from stix.common import InformationSource, StructuredTextList, VocabString
7-
from stix.common.vocabs import ReportIntent
8+
from stix.common import InformationSource, StructuredTextList
9+
from stix.common.vocabs import VocabField, ReportIntent
810
from stix.data_marking import Marking
911

1012

@@ -28,11 +30,21 @@ class Header(stix.Entity):
2830
2931
"""
3032
_binding = report_binding
33+
_binding_class = _binding.HeaderType
3134
_namespace = 'http://stix.mitre.org/Report-1'
3235

36+
title = fields.TypedField("Title")
37+
descriptions = fields.TypedField("Description", type_=StructuredTextList)
38+
short_descriptions = fields.TypedField("Short_Description", type_=StructuredTextList)
39+
intents = VocabField("Intent", ReportIntent, multiple=True, key_name="intents")
40+
handling = fields.TypedField("Handling", Marking)
41+
information_source = fields.TypedField("Information_Source", InformationSource)
42+
3343
def __init__(self, title=None, description=None, short_description=None,
3444
handling=None, intents=None, information_source=None):
3545

46+
super(Header, self).__init__()
47+
3648
self.intents = intents
3749
self.title = title
3850
self.description = description
@@ -61,36 +73,6 @@ def description(self):
6173
def description(self, value):
6274
self.descriptions = value
6375

64-
@property
65-
def descriptions(self):
66-
"""A :class:`.StructuredTextList` object, containing descriptions about
67-
the purpose or intent of this object.
68-
69-
This is typically used for the purpose of providing multiple
70-
descriptions with different classificaton markings.
71-
72-
Iterating over this object will yield its contents sorted by their
73-
``ordinality`` value.
74-
75-
Default Value: Empty :class:`.StructuredTextList` object.
76-
77-
Note:
78-
IF this is set to a value that is not an instance of
79-
:class:`.StructuredText`, an effort will ne made to convert it.
80-
If this is set to an iterable, any values contained that are not
81-
an instance of :class:`.StructuredText` will be be converted.
82-
83-
Returns:
84-
An instance of
85-
:class:`.StructuredTextList`
86-
87-
"""
88-
return self._description
89-
90-
@descriptions.setter
91-
def descriptions(self, value):
92-
self._description = StructuredTextList(value)
93-
9476
def add_description(self, description):
9577
"""Adds a description to the ``descriptions`` collection.
9678
@@ -120,35 +102,6 @@ def short_description(self):
120102
def short_description(self, value):
121103
self.short_descriptions = value
122104

123-
@property
124-
def short_descriptions(self):
125-
"""A :class:`.StructuredTextList` object, containing short descriptions
126-
about the purpose or intent of this object.
127-
128-
This is typically used for the purpose of providing multiple
129-
short descriptions with different classificaton markings.
130-
131-
Iterating over this object will yield its contents sorted by their
132-
``ordinality`` value.
133-
134-
Default Value: Empty :class:`.StructuredTextList` object.
135-
136-
Note:
137-
IF this is set to a value that is not an instance of
138-
:class:`.StructuredText`, an effort will ne made to convert it.
139-
If this is set to an iterable, any values contained that are not
140-
an instance of :class:`.StructuredText` will be be converted.
141-
142-
Returns:
143-
An instance of :class:`.StructuredTextList`
144-
145-
"""
146-
return self._short_description
147-
148-
@short_descriptions.setter
149-
def short_descriptions(self, value):
150-
self._short_description = StructuredTextList(value)
151-
152105
def add_short_description(self, description):
153106
"""Adds a description to the ``short_descriptions`` collection.
154107
@@ -157,30 +110,6 @@ def add_short_description(self, description):
157110
"""
158111
self.short_descriptions.add(description)
159112

160-
@property
161-
def handling(self):
162-
"""The :class:`.Marking` section of this Header. This section contains
163-
data marking information.
164-
165-
"""
166-
return self._handling
167-
168-
@handling.setter
169-
def handling(self, value):
170-
self._set_var(Marking, try_cast=False, handling=value)
171-
172-
@property
173-
def intents(self):
174-
"""A collection of :class:`.VocabString` controlled vocabulary
175-
objects.
176-
177-
"""
178-
return self._intents
179-
180-
@intents.setter
181-
def intents(self, value):
182-
self._intents = _ReportIntents(value)
183-
184113
def add_intent(self, intent):
185114
"""Adds :class:`.VocabString` object to the :attr:`intents`
186115
collection.
@@ -190,82 +119,3 @@ def add_intent(self, intent):
190119
191120
"""
192121
self.intents.append(intent)
193-
194-
@property
195-
def information_source(self):
196-
"""The :class:`.InformationSource` section of the Header.
197-
198-
"""
199-
return self._information_source
200-
201-
@information_source.setter
202-
def information_source(self, value):
203-
self._set_var(InformationSource, try_cast=False, information_source=value)
204-
205-
@classmethod
206-
def from_obj(cls, obj, return_obj=None):
207-
if not obj:
208-
return None
209-
210-
if not return_obj:
211-
return_obj = cls()
212-
213-
return_obj.title = obj.Title
214-
return_obj.descriptions = StructuredTextList.from_obj(obj.Description)
215-
return_obj.short_descriptions = StructuredTextList.from_obj(obj.Short_Description)
216-
return_obj.handling = Marking.from_obj(obj.Handling)
217-
return_obj.information_source = InformationSource.from_obj(obj.Information_Source)
218-
return_obj.intents = _ReportIntents.from_obj(obj.Intent)
219-
220-
return return_obj
221-
222-
def to_obj(self, return_obj=None, ns_info=None):
223-
super(Header, self).to_obj(return_obj=return_obj, ns_info=ns_info)
224-
225-
if not return_obj:
226-
return_obj = self._binding.HeaderType()
227-
228-
if self.title:
229-
return_obj.Title = self.title
230-
if self.intents:
231-
return_obj.Intent = self.intents.to_obj(ns_info=ns_info)
232-
if self.descriptions:
233-
return_obj.Description = self.descriptions.to_obj(ns_info=ns_info)
234-
if self.short_descriptions:
235-
return_obj.Short_Description = self.short_descriptions.to_obj(ns_info=ns_info)
236-
if self.handling:
237-
return_obj.Handling = self.handling.to_obj(ns_info=ns_info)
238-
if self.information_source:
239-
return_obj.Information_Source = self.information_source.to_obj(ns_info=ns_info)
240-
241-
return return_obj
242-
243-
@classmethod
244-
def from_dict(cls, dict_repr, return_obj=None):
245-
if not dict_repr:
246-
return None
247-
248-
if not return_obj:
249-
return_obj = cls()
250-
251-
get = dict_repr.get
252-
253-
return_obj.title = get('title')
254-
return_obj.intents = _ReportIntents.from_list(get('intents'))
255-
return_obj.descriptions = StructuredTextList.from_dict(get('description'))
256-
return_obj.short_descriptions = StructuredTextList.from_dict(get('short_description'))
257-
return_obj.handling = Marking.from_dict(get('handling'))
258-
return_obj.information_source = InformationSource.from_dict(get('information_source'))
259-
260-
return return_obj
261-
262-
def to_dict(self):
263-
return super(Header, self).to_dict()
264-
265-
266-
# NOT AN ACTUAL STIX TYPE!
267-
class _ReportIntents(stix.TypedList):
268-
_contained_type = VocabString
269-
270-
def _fix_value(self, value):
271-
return ReportIntent(value)

stix/test/report_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class HeaderTests(EntityTestCase, unittest.TestCase):
1818
'short_description': 'A really, really short description',
1919
'handling': data_marking_test.MarkingTests._full_dict,
2020
'information_source': information_source_test.InformationSourceTests._full_dict,
21+
'intents': ["foo", "bar"]
2122
}
2223

2324
def test_duplicate_package_intent(self):

0 commit comments

Comments
 (0)