Skip to content

Commit

Permalink
Merge pull request #249 from CybOXProject/mixbox-rebased
Browse files Browse the repository at this point in the history
Python 3 + Mixbox Support
  • Loading branch information
Bryan Worrell committed May 28, 2015
2 parents 85bc959 + 603f7a9 commit 1b38e12
Show file tree
Hide file tree
Showing 174 changed files with 2,129 additions and 2,328 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
env:
- TOXENV=py26
- TOXENV=py27
- TOXENV=py32
- TOXENV=py33
- TOXENV=py34
- TOXENV=rhel6

install:
Expand Down
39 changes: 23 additions & 16 deletions cybox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import collections
import inspect
import json
from StringIO import StringIO

import cybox.bindings as bindings
from mixbox.binding_utils import save_encoding
from mixbox.vendor import six

import cybox.utils.idgen
from cybox.utils import Namespace, META, is_sequence

Expand Down Expand Up @@ -131,13 +132,13 @@ def to_obj(self, return_obj=None, ns_info=None):

entity_obj = self._binding_class()

vars = {}
members = {}
for klass in self.__class__.__mro__:
if klass is Entity:
break
vars.update(klass.__dict__.iteritems())
members.update(vars(klass))

for name, field in vars.iteritems():
for field in six.itervalues(members):
if isinstance(field, TypedField):
val = getattr(self, field.attr_name)

Expand Down Expand Up @@ -170,13 +171,13 @@ def to_dict(self):
Python dict with keys set from this Entity.
"""
entity_dict = {}
vars = {}
members = {}
for klass in self.__class__.__mro__:
if klass is Entity:
break
vars.update(klass.__dict__.iteritems())
members.update(vars(klass))

for name, field in vars.iteritems():
for field in six.itervalues(members):
if isinstance(field, TypedField):
val = getattr(self, field.attr_name)

Expand Down Expand Up @@ -290,16 +291,16 @@ def to_xml(self, include_namespaces=True, namespace_dict=None,
namespace_def = namespace_def.replace('\n\t', ' ')


with bindings.save_encoding(encoding):
sio = StringIO()
with save_encoding(encoding):
sio = six.StringIO()
self.to_obj().export(
sio.write,
0,
namespacedef_=namespace_def,
pretty_print=pretty
)

s = unicode(sio.getvalue()).strip()
s = six.text_type(sio.getvalue()).strip()

if encoding:
return s.encode(encoding)
Expand All @@ -326,7 +327,7 @@ def _get_namespace_def(self, additional_ns_dict=None):
namespaces = self._get_namespaces()

if additional_ns_dict:
for ns, prefix in additional_ns_dict.iteritems():
for ns, prefix in six.iteritems(additional_ns_dict):
namespaces.update([Namespace(ns, prefix)])

# TODO: For now, always add the ID namespace. Later we can figure out
Expand All @@ -340,7 +341,8 @@ def _get_namespace_def(self, additional_ns_dict=None):
if not namespaces:
return ""

namespaces = sorted(namespaces, key=str)
#TODO: Is there a better `key` to use here?
namespaces = sorted(namespaces, key=six.text_type)

return ('\n\t' + get_xmlns_string(namespaces) +
'\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
Expand Down Expand Up @@ -368,7 +370,12 @@ def _get_namespaces(self, recurse=True):
def _get_children(self):
#TODO: eventually everything should be in _fields, not the top level
# of vars()
for k, v in vars(self).items() + self._fields.items():

members = {}
members.update(vars(self))
members.update(self._fields)

for v in six.itervalues(members):
if isinstance(v, Entity):
yield v
elif isinstance(v, list):
Expand Down Expand Up @@ -411,7 +418,7 @@ def value(self):

@value.setter
def value(self, value):
self._value = unicode(value)
self._value = six.text_type(value)

def to_obj(self, return_obj=None, ns_info=None):
self._collect_ns_info(ns_info)
Expand Down Expand Up @@ -583,7 +590,7 @@ def from_dict(cls, ref_dict):
class ReferenceList(EntityList):

def _fix_value(self, value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return self._contained_type(value)


Expand Down
Loading

0 comments on commit 1b38e12

Please sign in to comment.