Skip to content

Commit

Permalink
Initial Python3 support
Browse files Browse the repository at this point in the history
Create compat.py module to deal with Python2/Python3 dependencies
  • Loading branch information
gtback committed May 7, 2015
1 parent 85bc959 commit ce549b8
Show file tree
Hide file tree
Showing 114 changed files with 1,034 additions and 1,039 deletions.
26 changes: 16 additions & 10 deletions cybox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import collections
import inspect
import json
from StringIO import StringIO

from .compat import StringIO, basestring, str

import cybox.bindings as bindings
import cybox.utils.idgen
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 name, field in members.items():
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 name, field in members.items():
if isinstance(field, TypedField):
val = getattr(self, field.attr_name)

Expand Down Expand Up @@ -299,7 +300,7 @@ def to_xml(self, include_namespaces=True, namespace_dict=None,
pretty_print=pretty
)

s = unicode(sio.getvalue()).strip()
s = str(sio.getvalue()).strip()

if encoding:
return s.encode(encoding)
Expand Down Expand Up @@ -368,7 +369,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 k, v in members.items():
if isinstance(v, Entity):
yield v
elif isinstance(v, list):
Expand Down Expand Up @@ -411,7 +417,7 @@ def value(self):

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

def to_obj(self, return_obj=None, ns_info=None):
self._collect_ns_info(ns_info)
Expand Down
45 changes: 30 additions & 15 deletions cybox/bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from xml.sax import saxutils
from lxml import etree as etree_

from cybox.compat import basestring, str

CDATA_START = "<![CDATA["
CDATA_END = "]]>"

Expand Down Expand Up @@ -86,7 +88,7 @@ def gds_validate_integer_list(self, input_data, node, input_name=''):
for value in values:
try:
fvalue = float(value)
except (TypeError, ValueError), exp:
except (TypeError, ValueError) as exp:
raise_parse_error(node, 'Requires sequence of integers')
return input_data

Expand All @@ -104,7 +106,7 @@ def gds_validate_float_list(self, input_data, node, input_name=''):
for value in values:
try:
fvalue = float(value)
except (TypeError, ValueError), exp:
except (TypeError, ValueError) as exp:
raise_parse_error(node, 'Requires sequence of floats')
return input_data

Expand All @@ -122,7 +124,7 @@ def gds_validate_double_list(self, input_data, node, input_name=''):
for value in values:
try:
fvalue = float(value)
except (TypeError, ValueError), exp:
except (TypeError, ValueError) as exp:
raise_parse_error(node, 'Requires sequence of doubles')
return input_data

Expand Down Expand Up @@ -271,17 +273,33 @@ def showIndent(lwrite, level, pretty_print=True):
lwrite(' ' * level)


def quote_xml(text):
def _coerce_unicode(text):
# Convert `text` to Unicode string.

if text is None:
return u''
text = ""

if isinstance(text, str):
return text

# Convert `text` to unicode string. This is mainly a catch-all for non
# This is mainly a catch-all for non
# string/unicode types like bool and int.
try:
text = unicode(text)
text = str(text)
except UnicodeDecodeError:
text = text.decode(ExternalEncoding)

return text


def quote_xml(text):
"""Format a value for display as an XML text node.
Returns:
Unicode string (str on Python 3, unicode on Python 2)
"""
text = _coerce_unicode(text)

# If it's a CDATA block, return the text as is.
if text.startswith(CDATA_START):
return text
Expand All @@ -292,15 +310,12 @@ def quote_xml(text):


def quote_attrib(text):
if text is None:
return u'""'
"""Format a value for display as an XML attribute.
# Convert `text` to unicode string. This is mainly a catch-all for non
# string/unicode types like bool and int.
try:
text = unicode(text)
except UnicodeDecodeError:
text = text.decode(ExternalEncoding)
Returns:
Unicode string (str on Python 3, unicode on Python 2)
"""
text = _coerce_unicode(text)

# Return the escaped the value of text.
# Note: This wraps the escaped text in quotation marks.
Expand Down
6 changes: 3 additions & 3 deletions cybox/bindings/account_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from cybox.bindings import *
import cybox_common
from . import cybox_common


class AuthenticationType(GeneratedsSuper):
Expand Down Expand Up @@ -426,7 +426,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
"""

def usage():
print USAGE_TEXT
print(USAGE_TEXT)
sys.exit(1)

def get_root_tag(node):
Expand Down Expand Up @@ -472,7 +472,7 @@ def parseEtree(inFileName):
return rootObj, rootElement

def parseString(inString):
from StringIO import StringIO
from cybox.compat import StringIO
doc = parsexml_(StringIO(inString))
rootNode = doc.getroot()
rootTag, rootClass = get_root_tag(rootNode)
Expand Down
8 changes: 4 additions & 4 deletions cybox/bindings/address_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from cybox.bindings import *
import cybox_common
from . import cybox_common


class AddressObjectType(cybox_common.ObjectPropertiesType):
Expand All @@ -13,7 +13,7 @@ class AddressObjectType(cybox_common.ObjectPropertiesType):
is being defined. The is_source field specifies if this is a
"Source" addressThe is_destination field specifies if this is a
"Destination" address"""

subclass = None
superclass = cybox_common.ObjectPropertiesType
def __init__(self, object_reference=None, Custom_Properties=None, xsi_type=None, category='ipv4-addr', is_source=None, is_destination=None, is_spoofed=None, Address_Value=None, VLAN_Name=None, VLAN_Num=None):
Expand Down Expand Up @@ -259,7 +259,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
"""

def usage():
print USAGE_TEXT
print(USAGE_TEXT)
sys.exit(1)

def get_root_tag(node):
Expand Down Expand Up @@ -305,7 +305,7 @@ def parseEtree(inFileName):
return rootObj, rootElement

def parseString(inString):
from StringIO import StringIO
from cybox.compat import StringIO
doc = parsexml_(StringIO(inString))
rootNode = doc.getroot()
rootTag, rootClass = get_root_tag(rootNode)
Expand Down
8 changes: 4 additions & 4 deletions cybox/bindings/api_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import sys

from cybox.bindings import *
import cybox_common
from . import cybox_common


class APIObjectType(cybox_common.ObjectPropertiesType):
"""The APIObjectType type is intended to characterize a specific
Application Programming Interface."""

subclass = None
superclass = cybox_common.ObjectPropertiesType
def __init__(self, object_reference=None, Custom_Properties=None, xsi_type=None, Description=None, Function_Name=None, Normalized_Function_Name=None, Platform=None, Address=None):
Expand Down Expand Up @@ -220,7 +220,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
"""

def usage():
print USAGE_TEXT
print(USAGE_TEXT)
sys.exit(1)

def get_root_tag(node):
Expand Down Expand Up @@ -266,7 +266,7 @@ def parseEtree(inFileName):
return rootObj, rootElement

def parseString(inString):
from StringIO import StringIO
from cybox.compat import StringIO
doc = parsexml_(StringIO(inString))
rootNode = doc.getroot()
rootTag, rootClass = get_root_tag(rootNode)
Expand Down
9 changes: 4 additions & 5 deletions cybox/bindings/archive_file_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import sys

from cybox.bindings import *
import cybox_common

import file_object
from . import cybox_common
from . import file_object


class ArchiveFileFormatType(cybox_common.BaseObjectPropertyType):
Expand Down Expand Up @@ -363,7 +362,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
"""

def usage():
print USAGE_TEXT
print(USAGE_TEXT)
sys.exit(1)

def get_root_tag(node):
Expand Down Expand Up @@ -409,7 +408,7 @@ def parseEtree(inFileName):
return rootObj, rootElement

def parseString(inString):
from StringIO import StringIO
from cybox.compat import StringIO
doc = parsexml_(StringIO(inString))
rootNode = doc.getroot()
rootTag, rootClass = get_root_tag(rootNode)
Expand Down
11 changes: 5 additions & 6 deletions cybox/bindings/arp_cache_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import sys

from cybox.bindings import *
import cybox_common

import address_object
import system_object
from . import cybox_common
from . import address_object
from . import system_object


class ARPCacheEntryType(GeneratedsSuper):
Expand Down Expand Up @@ -404,7 +403,7 @@ def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
"""

def usage():
print USAGE_TEXT
print(USAGE_TEXT)
sys.exit(1)

def get_root_tag(node):
Expand Down Expand Up @@ -450,7 +449,7 @@ def parseEtree(inFileName):
return rootObj, rootElement

def parseString(inString):
from StringIO import StringIO
from cybox.compat import StringIO
doc = parsexml_(StringIO(inString))
rootNode = doc.getroot()
rootTag, rootClass = get_root_tag(rootNode)
Expand Down
Loading

0 comments on commit ce549b8

Please sign in to comment.