Skip to content

Commit 8e73c0d

Browse files
author
Bryan Worrell
committed
Merge branch 'master' of github.com:STIXProject/python-stix
2 parents cacd698 + e37d0c3 commit 8e73c0d

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# python-stix
22

3-
A python library for parsing, manipulating, and generating STIX content. The python-stix library
4-
is under heavy development and should be considered ALPHA.
3+
A python library for parsing, manipulating, and generating STIX content. The `python-stix` library
4+
utilized the 1.0 Draft 2 bindings, is under heavy development and should be considered PRE-ALPHA.
55

66
For more information about STIX, see http://stix.mitre.org.
77

88

99
## Installation
1010

11-
The 'python-stix' package depends on the 'lxml' XML parsing library, the
12-
'python-cybox' CybOX library, and the 'python-maec' MAEC library.
11+
The `python-stix` package depends on the `lxml` XML parsing library, the
12+
`python-cybox` CybOX library, and the `python-maec` MAEC library.
1313

1414
To build `lxml` on Ubuntu, you will need the following packages from the
1515
Ubuntu package repository:
@@ -18,14 +18,15 @@ Ubuntu package repository:
1818
* libxml2-dev
1919
* libxslt1-dev
2020

21-
Once the dependencies have been built, you can install 'lxml' via pip:
21+
Once the dependencies have been built, you can install `lxml` via pip:
22+
2223
<code>
2324
$ pip install lxml
2425
</code>
2526

26-
Note, on Windows it is recommended to download a pre-compiled distribution of lxml.
27+
Note, on Windows it is recommended to download a pre-compiled distribution of `lxml`.
2728

28-
For more information about installing lxml, see
29+
For more information about installing `lxml`, see
2930
http://lxml.de/installation.html
3031

3132
The CybOX and MAEC libraries can be found at their GitHub repositories:
@@ -36,6 +37,7 @@ To install, download or clone the repositories into their own folders. Once clon
3637
the setuptools script, setup.py can be run to install CybOX and then MAEC, in that order. MAEC
3738
depends on CybOX, so CybOX must be installed first. To install, run the following commands from
3839
within the project directories:
40+
3941
<code>
4042
$ python setup.py install
4143
</code>

stix/common/identity.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import stix
22
import stix.utils
3-
import stix.bindings.stix_indicator_1_1 as stix_indicator_binding
43
import stix.bindings.stix_common_0_2 as stix_common_binding
54
import stix.bindings.oasis.xpil as oasis_xpil_binding
65

@@ -68,7 +67,7 @@ def to_obj(self, return_obj=None):
6867

6968
if self.related_identities:
7069
related_identities_obj = stix_common_binding.RelatedIdentitiesType()
71-
for identity in related_identities:
70+
for identity in self.related_identities:
7271
related_identities_obj.add_RelatedIdentity(identity.to_obj())
7372

7473
return_obj.set_RelatedIdentities(related_identities_obj)
@@ -182,19 +181,6 @@ def party_name(self, value):
182181
raise ValueError("value must be instance of PartyName")
183182

184183
self._party_name = value
185-
186-
187-
def to_obj(self, return_obj=None):
188-
if not return_obj:
189-
return_obj = stix_common_binding.STIX_CIQ_IdentityType()
190-
191-
super(STIXCIQIdentity, self).to_obj(return_obj)
192-
193-
if self.party_name:
194-
return_obj.set_PartyName(self.party_name.to_obj())
195-
196-
return return_obj
197-
198184

199185
@classmethod
200186
def from_obj(cls, obj, return_obj=None):
@@ -222,7 +208,6 @@ def to_obj(self, return_obj=None):
222208

223209
return return_obj
224210

225-
226211
@classmethod
227212
def from_dict(cls, dict_repr, return_obj=None):
228213
if not dict_repr:
@@ -260,7 +245,7 @@ def relationship(self):
260245

261246
@relationship.setter
262247
def relationship(self, value):
263-
if value and not isinstasnce(value, basestring):
248+
if value and not isinstance(value, basestring):
264249
raise ValueError('value must be instance of basestring')
265250

266251
self._relationship = value
@@ -675,7 +660,7 @@ def to_obj(self, return_obj):
675660

676661
@classmethod
677662
def from_dict(cls, dict_repr, return_obj):
678-
self.value = dict_repr.get('value', None)
663+
return_obj.value = dict_repr.get('value', None)
679664

680665
def to_dict(self, return_dict):
681666
return_dict['value'] = self.value
@@ -709,8 +694,8 @@ def element_type(self):
709694

710695
@element_type.setter
711696
def element_type(self, value):
712-
if value and value not in TYPES:
713-
raise ValueError('value must be one of %s: ' % ' '.join(TYPES) )
697+
if value and value not in self.TYPES:
698+
raise ValueError('value must be one of %s: ' % ' '.join(self.TYPES) )
714699

715700
self._element_type = value
716701

@@ -773,7 +758,7 @@ class OrganisationNameElement(NameElement):
773758
TYPES = (TYPE_NAME_ONLY, TYPE_TYPE_ONLY, TYPE_FULL_NAME)
774759

775760
def __init__(self, value=None, element_type=None):
776-
super(SubDivisionNameElement, self).__init__(value)
761+
super(OrganisationNameElement, self).__init__(value)
777762
self.element_type = element_type
778763

779764
@property
@@ -783,8 +768,8 @@ def element_type(self):
783768

784769
@element_type.setter
785770
def element_type(self, value):
786-
if value and value not in TYPES:
787-
raise ValueError('value must be one of: %s ' % ' '.join(TYPES) )
771+
if value and value not in self.TYPES:
772+
raise ValueError('value must be one of: %s ' % ' '.join(self.TYPES) )
788773

789774
self._element_type = value
790775

@@ -856,8 +841,8 @@ def type(self):
856841

857842
@type.setter
858843
def type(self, value):
859-
if value and value not in TYPES:
860-
raise ValueError('value must be one of: %s' % ' '.join(TYPES))
844+
if value and value not in self.TYPES:
845+
raise ValueError('value must be one of: %s' % ' '.join(self.TYPES))
861846

862847
self._type = value
863848

@@ -900,7 +885,7 @@ def to_dict(self, return_dict=None):
900885

901886
@classmethod
902887
def from_dict(cls, dict_repr, return_obj=None):
903-
if not obj:
888+
if not dict_repr:
904889
return None
905890

906891
if not return_obj:

stix/core/stix_package.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def to_dict(self, return_dict=None):
7373
if not return_dict:
7474
return_dict = {}
7575

76-
return_dict['id'] = self.id_
76+
if self.id_:
77+
return_dict['id'] = self.id_
78+
7779
return_dict['version'] = self.version
7880

7981
if self.idref_:

stix/indicator/indicator.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
from cybox.core import Observable, ObservableComposition
66
from cybox.common import Time
77

8-
98
class Indicator(stix.Entity):
109
TYPE_SOURCE_ORG = 0
1110
TYPE_SOURCE_PERSON = 1
1211
TYPES_SOURCE = (TYPE_SOURCE_ORG, TYPE_SOURCE_PERSON)
1312

14-
def __init__(self, id=None, producer=None, observables=None):
15-
self._id = id if id else stix.utils.create_id()
13+
def __init__(self, id_=None, producer=None, observables=None):
14+
self.id_ = id_ if id_ is not None else stix.utils.create_id()
1615
self.producer = producer if producer else stix.common.InformationSource()
1716
self.observables = observables
1817
self.name = None
@@ -39,27 +38,27 @@ def observables(self, valuelist):
3938

4039
if valuelist:
4140
for value in valuelist:
42-
self.add_observable(observable)
41+
self.add_observable(value)
4342

44-
def add_source(self, type, name):
43+
def add_source(self, type_, name):
4544
'''
4645
Adds a source to this indicator.
4746
4847
Keyword arguments:
49-
type -- the type of source (Indicator.TYPE_SOURCE_ORG, Indicator.TYPE_SOURCE_PERSON)
48+
type_ -- the type_ of source (Indicator.TYPE_SOURCE_ORG, Indicator.TYPE_SOURCE_PERSON)
5049
name -- the name of the source
5150
'''
52-
if type not in self.TYPES_SOURCE:
53-
raise ValueError('type not known')
51+
if type_ not in self.TYPES_SOURCE:
52+
raise ValueError('type_ not known')
5453

55-
if type == TYPE_SOURCE_ORG:
56-
org_name_element = stix.common.OrganisationNameElement(org_name)
54+
if type_ == self.TYPE_SOURCE_ORG:
55+
org_name_element = stix.common.OrganisationNameElement(value=name)
5756
org_name = stix.common.OrganisationName()
5857
org_name.add_organisation_name_element(org_name_element)
5958
self.producer.identity.party_name.add_organisation_name(org_name)
6059

61-
if type == TYPE_SOURCE_PERSON:
62-
person_name_element = stix.common.PersonNameElement(person_name)
60+
if type_ == self.TYPE_SOURCE_PERSON:
61+
person_name_element = stix.common.PersonNameElement(value=name)
6362
person_name = stix.common.PersonName()
6463
person_name.add_name_element(person_name_element)
6564
self.producer.identity.party_name.add_person_name(person_name)
@@ -94,7 +93,7 @@ def get_sources(self):
9493

9594
# get the namelines
9695
try:
97-
part_name = self.producer.identity.party_name
96+
party_name = self.producer.identity.party_name
9897
list_names.extend(party_name.name_lines)
9998
except:
10099
pass
@@ -152,14 +151,20 @@ def _merge_observables(self, observables, operator='AND'):
152151

153152
return root_observable
154153

155-
def add_object(self, object):
154+
def add_object(self, object_):
156155
''' The object paramter is wrapped in an observable and attached to the indicator. The object must be a
157156
cybox.core.DefinedObject instance'''
158157

159-
observable = Observable(stateful_measure=object)
158+
observable = Observable(stateful_measure=object_)
160159
self.add_observable(observable)
161160

162-
def to_obj(self, return_obj=stix_indicator_binding.IndicatorType()):
161+
def to_obj(self, return_obj=None):
162+
if not return_obj:
163+
return_obj = stix_indicator_binding.IndicatorType()
164+
165+
if self.id_:
166+
return_obj.set_id(self.id_)
167+
163168
'''most of this does not work because of the state of the cybox api development'''
164169
if self.observables:
165170
observables_obj = stix_indicator_binding.ObservablesType()
@@ -184,6 +189,8 @@ def from_obj(cls, obj, return_obj=None):
184189
if not return_obj:
185190
return_obj = cls()
186191

192+
return_obj.id_ = obj.get_id()
193+
187194
if obj.get_Producer():
188195
return_obj.producer = stix.common.InformationSource.from_obj(obj.get_Producer())
189196

@@ -198,6 +205,9 @@ def to_dict(self, return_dict=None):
198205
if not return_dict:
199206
return_dict = {}
200207

208+
if self.id_:
209+
return_dict['id'] = self.id_
210+
201211
if self.observables:
202212
if len(self.observables) == 1:
203213
return_dict['observable'] = self.observables[0].to_dict()
@@ -219,6 +229,8 @@ def from_dict(cls, dict_repr, return_obj=None):
219229
if not return_obj:
220230
return_obj = cls()
221231

232+
return_obj.id_ = dict_repr.get('id', None)
233+
222234
observable_dict = dict_repr.get('observable', )
223235
producer_dict = dict_repr.get('producer', None)
224236

0 commit comments

Comments
 (0)