Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 + Mixbox Support #249

Merged
merged 17 commits into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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