Skip to content

Commit

Permalink
Merge pull request #9 from STIXProject/python3
Browse files Browse the repository at this point in the history
Python3
  • Loading branch information
gtback committed Jul 26, 2016
2 parents f370d53 + 395901e commit 6b4dcd1
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "3.5"

install:
- pip install .
Expand Down
15 changes: 8 additions & 7 deletions ramrod/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# external
from lxml import etree
from six import iteritems

# relative
from . import errors, utils, xmlconst, results
Expand Down Expand Up @@ -171,7 +172,7 @@ def _translate_attributes(cls, old, new):
if cls.COPY_ATTRIBUTES:
new.attrib.update(source.attrib)

for name, val in cls.OVERRIDE_ATTRIBUTES.iteritems():
for name, val in iteritems(cls.OVERRIDE_ATTRIBUTES):
if name not in source.attrib:
continue
new.attrib[name] = val
Expand Down Expand Up @@ -521,7 +522,7 @@ def _get_duplicates(self, root):
id_nodes[id_].append(desc)

filtered = {}
for id_, nodes in id_nodes.iteritems():
for id_, nodes in iteritems(id_nodes):
if len(nodes) > 1:
filtered[id_] = nodes

Expand Down Expand Up @@ -700,7 +701,7 @@ def _remap_namespaces(self, node):
"""
remapped = {}
for alias, ns in node.nsmap.iteritems():
for alias, ns in iteritems(node.nsmap):
if ns in self.DISALLOWED_NAMESPACES:
continue

Expand Down Expand Up @@ -844,14 +845,14 @@ def clean(self, root, options=None):
return value:
>>> results = updater.clean(root)
>>> print results.removed
>>> print(results.removed)
(<Element at 0xffdcf234>, <Element at 0xffdcf284>)
Items which have been reassigned IDs can be retrieved via the
``remapped_ids`` attribute on the return value:
>>> results = updater.clean(root)
>>> print results.remapped_ids
>>> print(results.remapped_ids)
{'example:Observable-duplicate': [<Element {http://cybox.mitre.org...
Note:
Expand Down Expand Up @@ -935,14 +936,14 @@ def update(self, root, options=None, force=False):
return value:
>>> results = updater.update(root, force=True)
>>> print results.removed
>>> print(results.removed)
(<Element at 0xffdcf234>, <Element at 0xffdcf284>)
Items which have been reassigned IDs can be retrieved via the
``remappped_ids`` attribute on the return value:
>>> results = updater.update(root, force=True)
>>> print results.remapped_ids
>>> print(results.remapped_ids)
{'example:Observable-duplicate-id-1': [<Element {http://cybox.mitre...
Args:
Expand Down
6 changes: 4 additions & 2 deletions ramrod/cybox/cybox_2_0_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from ramrod import base, errors, utils
from ramrod.options import DEFAULT_UPDATE_OPTIONS

# external
from six import iteritems

# relative imports
from . import common
from . import register_updater
from .base import BaseCyboxUpdater, CyboxVocab


class ObjectRelationshipVocab(CyboxVocab):
OLD_TYPES = ('ObjectRelationshipVocab-1.0',)
NEW_TYPE = 'ObjectRelationshipVocab-1.1'
Expand Down Expand Up @@ -606,7 +608,7 @@ def _clean_duplicates(self, duplicates, options):
"""
new_id = options.new_id_func
for _, nodes in duplicates.iteritems():
for _, nodes in iteritems(duplicates):
for node in nodes:
new_id(node)

Expand Down
25 changes: 7 additions & 18 deletions ramrod/results.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

# stdlib
import StringIO

# external
from lxml import etree
from six import StringIO, text_type, python_2_unicode_compatible


@python_2_unicode_compatible
class UpdateResults(object):
"""Returned from :meth:`ramrod.update`, :meth:`ramrod.cybox.update`, and
:meth:`ramrod.stix.update` methods.
Expand Down Expand Up @@ -42,20 +41,14 @@ def document(self, value):
self._document = ResultDocument(value)


def __unicode__(self):
if not self.document:
return u''

return unicode(self.document)


def __str__(self):
if not self.document:
return ''

return str(self.document)
return text_type(self.document)


@python_2_unicode_compatible
class ResultDocument(object):
"""Used to encapsulate an updated XML document. This is the type of the
``document`` attribute on :class:`ramrod.UpdateResults`
Expand All @@ -80,12 +73,8 @@ def __init__(self, document):
self._document = document


def __unicode__(self):
return unicode(self.as_stringio().getvalue())


def __str__(self):
return unicode(self).encode('utf-8')
return text_type(self.as_stringio().getvalue())


def as_element(self):
Expand All @@ -103,12 +92,12 @@ def as_element_tree(self):
return self._document

def as_stringio(self):
"""Returns a ``StringIO.StringIO`` representation of the
"""Returns a ``StringIO`` representation of the
``ResultDocument`` instance.
"""
buf = etree.tounicode(self._document, pretty_print=True)
return StringIO.StringIO(buf)
return StringIO(buf)


__all__ = [
Expand Down
24 changes: 16 additions & 8 deletions ramrod/scripts/ramrod_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import ramrod
import ramrod.errors as errors

# external
from six import iteritems, PY2


EXIT_SUCCESS = 0
EXIT_FAILURE = 1
Expand Down Expand Up @@ -41,7 +44,12 @@ def _write_xml(document, outfn=None):
tree: A :class:`ramrod.ResultDocument` instance.
"""
out = outfn or sys.stdout
if PY2:
bin_stdout = sys.stdout
else:
bin_stdout = sys.stdout.buffer

out = outfn or bin_stdout
tree = document.as_element_tree()
tree.write(out, pretty_print=True)

Expand All @@ -64,8 +72,8 @@ def _print_update_error(err):
_print_error(" Line %s: %s", node.sourceline, node.tag)

if duplicates:
print "[!] Found items with duplicate ids:"
for id_, nodes in duplicates.iteritems():
print("[!] Found items with duplicate ids:")
for id_, nodes in iteritems(duplicates):
_print_error(" '%s' on lines %s", id_, [x.sourceline for x in nodes])


Expand Down Expand Up @@ -111,11 +119,11 @@ def _write_removed(removed):
if not removed:
return

print ("\n[!] The following nodes were removed from the source document "
print("\n[!] The following nodes were removed from the source document "
"during the update process:")

for node in removed:
print " Line %s: %s" % (node.sourceline, node.tag)
print(" Line %s: %s" % (node.sourceline, node.tag))


def _write_remapped_ids(remapped):
Expand All @@ -130,11 +138,11 @@ def _write_remapped_ids(remapped):
if not remapped:
return

print ("\n[!] The following ids were duplicated in the source document and "
print("\n[!] The following ids were duplicated in the source document and "
"remapped during the update process:")

for orig_id, nodes in remapped.iteritems():
print "'%s': %s" % (orig_id, [x.attrib['id'] for x in nodes])
for orig_id, nodes in iteritems(remapped):
print("'%s': %s" % (orig_id, [x.attrib['id'] for x in nodes]))


def _get_options(args):
Expand Down
10 changes: 5 additions & 5 deletions ramrod/scripts/update_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from __future__ import print_function
import argparse
import lxml.etree as ET
import six
import sys
from six import iteritems, itervalues, PY2

STIX_NS_1_2 = [
# "Core" stuff
Expand Down Expand Up @@ -127,15 +127,15 @@ def update_namespaces(elt, ns_mapping):
# Wholesale replacement is only necessary when we need to modify
# nsmap.
need_replace_elt = False
for ns in six.itervalues(elt.nsmap):
for ns in itervalues(elt.nsmap):
if ns in ns_mapping:
need_replace_elt = True
break

if need_replace_elt:
# Update nsmap
new_ns_map = {}
for pfx, old_ns in six.iteritems(elt.nsmap):
for pfx, old_ns in iteritems(elt.nsmap):
new_ns_map[pfx] = ns_mapping.get(old_ns, old_ns)

# Must update the element and attribute names when we update nsmap,
Expand Down Expand Up @@ -292,7 +292,7 @@ def parse_args():
Pretty-print output.
""")

if six.PY2:
if PY2:
bin_stdin = sys.stdin
else:
bin_stdin = sys.stdin.buffer
Expand Down Expand Up @@ -348,7 +348,7 @@ def main(tree, from_id, to_id):

output_encoding = "utf-8"

if six.PY2:
if PY2:
bin_stdout = sys.stdout
else:
bin_stdout = sys.stdout.buffer
Expand Down
7 changes: 5 additions & 2 deletions ramrod/stix/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# internal
from ramrod import base, errors, utils

# external
from six import iteritems


class BaseSTIXUpdater(base.BaseUpdater):
"""Base class for STIX updating code. Sets default values for
Expand Down Expand Up @@ -63,8 +66,8 @@ def _init_cybox_updater(self):

updater.NSMAP = dict(
itertools.chain(
self.NSMAP.iteritems(),
self.CYBOX_UPDATER.NSMAP.iteritems()
iteritems(self.NSMAP),
iteritems(self.CYBOX_UPDATER.NSMAP)
)
)

Expand Down
5 changes: 3 additions & 2 deletions ramrod/stix/stix_1_0_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# external
from lxml import etree
from six import itervalues

# internal
from ramrod import base, errors, utils
Expand Down Expand Up @@ -444,7 +445,7 @@ def _get_duplicates(self, root):
"""
duplicates = super(STIX_1_0_1_Updater, self)._get_duplicates(root)
cybox = self._cybox_updater._get_duplicates(root) # noqa
return dict(duplicates.items() + cybox.items())
return dict(list(duplicates.items()) + list(cybox.items()))

def _update_versions(self, root):
"""Updates the versions of versioned nodes under `root` to align with
Expand Down Expand Up @@ -502,7 +503,7 @@ def _clean_duplicates(self, duplicates, options):
"""
new_id = options.new_id_func

for nodes in duplicates.itervalues():
for nodes in itervalues(duplicates):
for node in nodes:
new_id(node)

Expand Down
2 changes: 1 addition & 1 deletion ramrod/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE.txt for complete terms.

import unittest
from StringIO import StringIO
from six import StringIO
import ramrod.utils as utils

class _BaseOptional(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion ramrod/test/cybox/cybox_2_0_1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE.txt for complete terms.

import unittest
from StringIO import StringIO
from six import StringIO

import ramrod
import ramrod.cybox
Expand Down
2 changes: 1 addition & 1 deletion ramrod/test/cybox/cybox_2_0_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE.txt for complete terms.

import unittest
from StringIO import StringIO
from six import StringIO

import ramrod
import ramrod.cybox
Expand Down

0 comments on commit 6b4dcd1

Please sign in to comment.