Skip to content

Commit 9dced3f

Browse files
committed
Initial Python 3 support
1 parent 657a90d commit 9dced3f

File tree

16 files changed

+83
-70
lines changed

16 files changed

+83
-70
lines changed

.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
language: python
2+
3+
python:
4+
- 3.5
5+
26
env:
37
- TOXENV=py26
48
- TOXENV=py27
9+
- TOXENV=py33
10+
- TOXENV=py34
11+
- TOXENV=py35
512
- TOXENV=rhel6
613

714
install:
8-
- pip install tox
15+
- pip install -U tox
916

1017
script:
1118
- tox

docs/api_vs_bindings/api_snippet.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
b.add_action(a)
3333
3434
# Output the Bundle to stdout
35-
print b.to_xml(include_namespaces = False)
35+
print(b.to_xml(include_namespaces = False))

docs/examples.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ different types of analysis.
4141
ms = MalwareSubject()
4242
p.add_malware_subject(ms)
4343

44-
print p.to_xml(include_namespaces=False)
44+
print(p.to_xml(include_namespaces=False, encoding=None))
4545

4646
Which outputs:
4747

@@ -77,7 +77,7 @@ that it is characterizing.
7777
ms.malware_instance_object_attributes.properties = File()
7878
ms.malware_instance_object_attributes.properties.file_name = "malware.exe"
7979
ms.malware_instance_object_attributes.properties.file_path = "C:\Windows\Temp\malware.exe"
80-
print ms.to_xml(include_namespaces=False)
80+
print(ms.to_xml(include_namespaces=False, encoding=None))
8181

8282
Which outputs:
8383

@@ -125,7 +125,7 @@ instance that it is characterizing.
125125
b.malware_instance_object_attributes.properties.file_name = "malware.exe"
126126
b.malware_instance_object_attributes.properties.file_path = "C:\Windows\Temp\malware.exe"
127127

128-
print b.to_xml(include_namespaces=False)
128+
print(b.to_xml(include_namespaces=False, encoding=None))
129129

130130
Which outputs:
131131

@@ -168,7 +168,7 @@ be defined in their parent Malware Subject.
168168
b = Bundle()
169169
ms.add_findings_bundle(b)
170170

171-
print ms.to_xml(include_namespaces=False)
171+
print(ms.to_xml(include_namespaces=False, encoding=None))
172172

173173
Which outputs:
174174

@@ -227,7 +227,7 @@ needed.
227227

228228
b.add_action(a)
229229

230-
print b.to_xml(include_namespaces = False)
230+
print(b.to_xml(include_namespaces = False, encoding=None))
231231

232232
.. testoutput::
233233

maec/__init__.py

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

4+
from __future__ import absolute_import
45
from mixbox.entities import Entity as cyboxEntity
56
from mixbox.entities import EntityList
67
from mixbox.namespaces import (Namespace, get_xmlns_string,
78
get_schemaloc_string, lookup_name, lookup_prefix)
9+
from mixbox.vendor.six import iteritems, string_types
810

9-
import bindings.maec_bundle as bundle_binding
10-
import bindings.maec_package as package_binding
11+
from .bindings import maec_bundle as bundle_binding
12+
from .bindings import maec_package as package_binding
1113
import maec
1214
from maec.utils import flip_dict, EntityParser
1315

@@ -42,14 +44,14 @@ def to_xml_file(self, file, namespace_dict=None, custom_header=None):
4244
namespace_dict = {}
4345
else:
4446
# Make a copy so we don't pollute the source
45-
namespace_dict = dict(namespace_dict.iteritems())
47+
namespace_dict = dict(iteritems(namespace_dict))
4648

4749
# Update the namespace dictionary with namespaces found upon import
4850
input_namespaces = self._ns_to_prefix_input_namespaces()
4951
namespace_dict.update(input_namespaces)
5052

5153
# Check whether we're dealing with a filename or file-like Object
52-
if isinstance(file, basestring):
54+
if isinstance(file, string_types):
5355
out_file = open(file, 'w')
5456
else:
5557
out_file = file
@@ -62,12 +64,12 @@ def to_xml_file(self, file, namespace_dict=None, custom_header=None):
6264
out_file.write("-->\n")
6365
elif isinstance(custom_header, dict):
6466
out_file.write("<!--\n")
65-
for key, value in custom_header.iteritems():
67+
for key, value in iteritems(custom_header):
6668
sanitized_key = str(key).replace("-->", "\\-\\->")
6769
sanitized_value = str(value).replace("-->", "\\-\\->")
6870
out_file.write(sanitized_key + ": " + sanitized_value + "\n")
6971
out_file.write("-->\n")
70-
elif isinstance(custom_header, basestring):
72+
elif isinstance(custom_header, string_types):
7173
out_file.write("<!--\n")
7274
out_file.write(custom_header.replace("-->", "\\-\\->") + "\n")
7375
out_file.write("-->\n")
@@ -87,7 +89,7 @@ def _get_namespace_def(self, additional_ns_dict=None):
8789

8890
if namespaces and additional_ns_dict:
8991
namespace_list = [x.name for x in namespaces if x]
90-
for ns, prefix in additional_ns_dict.iteritems():
92+
for ns, prefix in iteritems(additional_ns_dict):
9193
if ns not in namespace_list:
9294
namespaces.update([Namespace(ns, prefix, '')])
9395

@@ -119,7 +121,7 @@ def _get_namespaces(self, recurse=True):
119121

120122
# Add any additional namespaces that may be included in the entity
121123
input_ns = self._ns_to_prefix_input_namespaces()
122-
for namespace, alias in input_ns.iteritems():
124+
for namespace, alias in iteritems(input_ns):
123125
if not lookup_name(namespace):
124126
nsset.add(Namespace(namespace, alias, ''))
125127

maec/bindings/maec_bundle.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def exportAttributes(self, write, level, already_processed, namespace_='maecBund
103103
write(' status=%s' % (quote_attrib(self.status), ))
104104
if self.duration is not None and 'duration' not in already_processed:
105105
already_processed.add('duration')
106-
write(' duration=%s' % (quote_attrib(self.duration).encode(ExternalEncoding)))
106+
write(' duration=%s' % (quote_attrib(self.duration)))
107107
if self.ordinal_position is not None and 'ordinal_position' not in already_processed:
108108
already_processed.add('ordinal_position')
109109
write(' ordinal_position="%s"' % self.gds_format_integer(self.ordinal_position, input_name='ordinal_position'))
@@ -148,7 +148,7 @@ def buildAttributes(self, node, attrs, already_processed):
148148
already_processed.add('ordinal_position')
149149
try:
150150
self.ordinal_position = int(value)
151-
except ValueError, exp:
151+
except ValueError as exp:
152152
raise_parse_error(node, 'Bad integer attribute: %s' % exp)
153153
if self.ordinal_position <= 0:
154154
raise_parse_error(node, 'Invalid PositiveInteger')
@@ -355,7 +355,7 @@ def buildAttributes(self, node, attrs, already_processed):
355355
already_processed.add('timestamp')
356356
try:
357357
self.timestamp = self.gds_parse_datetime(value, node, 'timestamp')
358-
except ValueError, exp:
358+
except ValueError as exp:
359359
raise ValueError('Bad date-time attribute (timestamp): %s' % exp)
360360
def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
361361
if nodeName_ == 'Malware_Instance_Object_Attributes':
@@ -1007,7 +1007,7 @@ def buildAttributes(self, node, attrs, already_processed):
10071007
already_processed.add('ordinal_position')
10081008
try:
10091009
self.ordinal_position = int(value)
1010-
except ValueError, exp:
1010+
except ValueError as exp:
10111011
raise_parse_error(node, 'Bad integer attribute: %s' % exp)
10121012
if self.ordinal_position <= 0:
10131013
raise_parse_error(node, 'Invalid PositiveInteger')
@@ -1980,7 +1980,7 @@ def buildAttributes(self, node, attrs, already_processed):
19801980
already_processed.add('behavioral_ordering')
19811981
try:
19821982
self.behavioral_ordering = int(value)
1983-
except ValueError, exp:
1983+
except ValueError as exp:
19841984
raise_parse_error(node, 'Bad integer attribute: %s' % exp)
19851985
if self.behavioral_ordering <= 0:
19861986
raise_parse_error(node, 'Invalid PositiveInteger')
@@ -2316,7 +2316,7 @@ def buildAttributes(self, node, attrs, already_processed):
23162316
already_processed.add('creation_datetime')
23172317
try:
23182318
self.creation_datetime = value
2319-
except ValueError, exp:
2319+
except ValueError as exp:
23202320
raise ValueError('Bad date-time attribute (creation_datetime): %s' % exp)
23212321
value = find_attr_value_('id', node)
23222322
if value is not None and 'id' not in already_processed:
@@ -2327,7 +2327,7 @@ def buildAttributes(self, node, attrs, already_processed):
23272327
already_processed.add('lastupdate_datetime')
23282328
try:
23292329
self.lastupdate_datetime = value
2330-
except ValueError, exp:
2330+
except ValueError as exp:
23312331
raise ValueError('Bad date-time attribute (lastupdate_datetime): %s' % exp)
23322332
def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
23332333
if nodeName_ == 'Importance':
@@ -2338,7 +2338,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
23382338
sval_ = child_.text
23392339
try:
23402340
ival_ = int(sval_)
2341-
except (TypeError, ValueError), exp:
2341+
except (TypeError, ValueError) as exp:
23422342
raise_parse_error(child_, 'requires integer: %s' % exp)
23432343
if ival_ <= 0:
23442344
raise_parse_error(child_, 'requires positiveInteger')
@@ -3482,7 +3482,7 @@ def buildAttributes(self, node, attrs, already_processed):
34823482
already_processed.add('behavioral_ordering')
34833483
try:
34843484
self.behavioral_ordering = int(value)
3485-
except ValueError, exp:
3485+
except ValueError as exp:
34863486
raise_parse_error(node, 'Bad integer attribute: %s' % exp)
34873487
if self.behavioral_ordering <= 0:
34883488
raise_parse_error(node, 'Invalid PositiveInteger')
@@ -3890,7 +3890,7 @@ def buildAttributes(self, node, attrs, already_processed):
38903890
already_processed.add('behavioral_ordering')
38913891
try:
38923892
self.behavioral_ordering = int(value)
3893-
except ValueError, exp:
3893+
except ValueError as exp:
38943894
raise_parse_error(node, 'Bad integer attribute: %s' % exp)
38953895
if self.behavioral_ordering <= 0:
38963896
raise_parse_error(node, 'Invalid PositiveInteger')
@@ -4628,7 +4628,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
46284628
"""
46294629

46304630
def usage():
4631-
print USAGE_TEXT
4631+
print(USAGE_TEXT)
46324632
sys.exit(1)
46334633

46344634
def get_root_tag(node):
@@ -4668,7 +4668,7 @@ def parseEtree(inFileName):
46684668
return rootObj, rootElement
46694669

46704670
def parseString(inString):
4671-
from StringIO import StringIO
4671+
from mixbox.vendor.six import StringIO
46724672
doc = parsexml_(StringIO(inString))
46734673
rootNode = doc.getroot()
46744674
rootTag, rootClass = get_root_tag(rootNode)

maec/bindings/maec_container.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def buildAttributes(self, node, attrs, already_processed):
8989
already_processed.add('timestamp')
9090
try:
9191
self.timestamp = value
92-
except ValueError, exp:
92+
except ValueError as exp:
9393
raise ValueError('Bad date-time attribute (timestamp): %s' % exp)
9494
value = find_attr_value_('id', node)
9595
if value is not None and 'id' not in already_processed:
@@ -177,7 +177,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
177177
"""
178178

179179
def usage():
180-
print USAGE_TEXT
180+
print(USAGE_TEXT)
181181
sys.exit(1)
182182

183183
def get_root_tag(node):
@@ -217,7 +217,7 @@ def parseEtree(inFileName):
217217
return rootObj, rootElement
218218

219219
def parseString(inString):
220-
from StringIO import StringIO
220+
from mixbox.vendor.six import StringIO
221221
doc = parsexml_(StringIO(inString))
222222
rootNode = doc.getroot()
223223
rootTag, rootClass = get_root_tag(rootNode)

0 commit comments

Comments
 (0)