Remove six dependency (backwards breaking)

We've already removed Python 2 from our test harness, but this removes
all python 2 compatibility code left in.
pull/1/head
Chris Lenk 2021-02-18 12:26:54 -05:00
parent 490251dd85
commit b4a0a9ea10
18 changed files with 36 additions and 84 deletions

View File

@ -51,7 +51,6 @@ setup(
'pytz', 'pytz',
'requests', 'requests',
'simplejson', 'simplejson',
'six>=1.13.0',
'stix2-patterns>=1.2.0', 'stix2-patterns>=1.2.0',
], ],
project_urls={ project_urls={

View File

@ -5,7 +5,6 @@ import re
import uuid import uuid
import simplejson as json import simplejson as json
import six
import stix2 import stix2
from stix2.canonicalization.Canonicalize import canonicalize from stix2.canonicalization.Canonicalize import canonicalize
@ -70,12 +69,9 @@ class _STIXBase(Mapping):
# InvalidValueError... so let those propagate. # InvalidValueError... so let those propagate.
raise raise
except Exception as exc: except Exception as exc:
six.raise_from( raise InvalidValueError(
InvalidValueError( self.__class__, prop_name, reason=str(exc),
self.__class__, prop_name, reason=str(exc), ) from exc
),
exc,
)
# interproperty constraint methods # interproperty constraint methods
@ -369,19 +365,8 @@ class _Observable(_STIXBase):
if json_serializable_object: if json_serializable_object:
data = canonicalize(json_serializable_object, utf8=False) data = canonicalize(json_serializable_object, utf8=False)
uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, data)
# The situation is complicated w.r.t. python 2/3 behavior, so id_ = "{}--{}".format(self._type, str(uuid_))
# I'd rather not rely on particular exceptions being raised to
# determine what to do. Better to just check the python version
# directly.
if six.PY3:
uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, data)
else:
uuid_ = uuid.uuid5(
SCO_DET_ID_NAMESPACE, data.encode("utf-8"),
)
id_ = "{}--{}".format(self._type, six.text_type(uuid_))
return id_ return id_
@ -447,7 +432,7 @@ def _make_json_serializable(value):
for v in value for v in value
] ]
elif not isinstance(value, (int, float, six.string_types, bool)): elif not isinstance(value, (int, float, str, bool)):
# If a "simple" value which is not already JSON-serializable, # If a "simple" value which is not already JSON-serializable,
# JSON-serialize to a string and use that as our JSON-serializable # JSON-serialize to a string and use that as our JSON-serializable
# value. This applies to our datetime objects currently (timestamp # value. This applies to our datetime objects currently (timestamp

View File

@ -1,7 +1,5 @@
from collections import OrderedDict from collections import OrderedDict
import six
from .base import _cls_init from .base import _cls_init
from .registration import ( from .registration import (
_register_marking, _register_object, _register_observable, _register_marking, _register_object, _register_observable,
@ -13,14 +11,11 @@ def _get_properties_dict(properties):
try: try:
return OrderedDict(properties) return OrderedDict(properties)
except TypeError as e: except TypeError as e:
six.raise_from( raise ValueError(
ValueError( "properties must be dict-like, e.g. a list "
"properties must be dict-like, e.g. a list " "containing tuples. For example, "
"containing tuples. For example, " "[('property1', IntegerProperty())]",
"[('property1', IntegerProperty())]", ) from e
),
e,
)
def _custom_object_builder(cls, type, properties, version, base_class): def _custom_object_builder(cls, type, properties, version, base_class):

View File

@ -15,8 +15,6 @@ Python STIX2 DataStore API.
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
import uuid import uuid
from six import with_metaclass
from stix2.datastore.filters import Filter, FilterSet from stix2.datastore.filters import Filter, FilterSet
from stix2.utils import deduplicate from stix2.utils import deduplicate
@ -219,7 +217,7 @@ class DataStoreMixin(object):
raise AttributeError(msg % self.__class__.__name__) raise AttributeError(msg % self.__class__.__name__)
class DataSink(with_metaclass(ABCMeta)): class DataSink(metaclass=ABCMeta):
"""An implementer will create a concrete subclass from """An implementer will create a concrete subclass from
this class for the specific DataSink. this class for the specific DataSink.
@ -245,7 +243,7 @@ class DataSink(with_metaclass(ABCMeta)):
""" """
class DataSource(with_metaclass(ABCMeta)): class DataSource(metaclass=ABCMeta):
"""An implementer will create a concrete subclass from """An implementer will create a concrete subclass from
this class for the specific DataSource. this class for the specific DataSource.

View File

@ -6,8 +6,6 @@ import os
import re import re
import stat import stat
import six
from stix2 import v20, v21 from stix2 import v20, v21
from stix2.base import _STIXBase from stix2.base import _STIXBase
from stix2.datastore import ( from stix2.datastore import (
@ -116,7 +114,7 @@ def _update_allow(allow_set, value):
""" """
adding_seq = hasattr(value, "__iter__") and \ adding_seq = hasattr(value, "__iter__") and \
not isinstance(value, six.string_types) not isinstance(value, str)
if allow_set is None: if allow_set is None:
allow_set = set() allow_set = set()

View File

@ -3,8 +3,6 @@
import collections import collections
from datetime import datetime from datetime import datetime
import six
import stix2.utils import stix2.utils
"""Supported filter operations""" """Supported filter operations"""
@ -12,8 +10,7 @@ FILTER_OPS = ['=', '!=', 'in', '>', '<', '>=', '<=', 'contains']
"""Supported filter value types""" """Supported filter value types"""
FILTER_VALUE_TYPES = ( FILTER_VALUE_TYPES = (
bool, dict, float, int, list, tuple, six.string_types, bool, dict, float, int, list, tuple, str, datetime,
datetime,
) )
@ -84,7 +81,7 @@ class Filter(collections.namedtuple('Filter', ['property', 'op', 'value'])):
# If filtering on a timestamp property and the filter value is a string, # If filtering on a timestamp property and the filter value is a string,
# try to convert the filter value to a datetime instance. # try to convert the filter value to a datetime instance.
if isinstance(stix_obj_property, datetime) and \ if isinstance(stix_obj_property, datetime) and \
isinstance(self.value, six.string_types): isinstance(self.value, str):
filter_value = stix2.utils.parse_into_datetime(self.value) filter_value = stix2.utils.parse_into_datetime(self.value)
else: else:
filter_value = self.value filter_value = self.value

View File

@ -2,8 +2,6 @@
import collections import collections
import six
from stix2 import exceptions, utils from stix2 import exceptions, utils
@ -129,7 +127,7 @@ def compress_markings(granular_markings):
{'marking_ref': item, 'selectors': sorted(selectors)} {'marking_ref': item, 'selectors': sorted(selectors)}
if utils.is_marking(item) else if utils.is_marking(item) else
{'lang': item, 'selectors': sorted(selectors)} {'lang': item, 'selectors': sorted(selectors)}
for item, selectors in six.iteritems(map_) for item, selectors in map_.items()
] ]
return compressed return compressed
@ -230,7 +228,7 @@ def iterpath(obj, path=None):
if path is None: if path is None:
path = [] path = []
for varname, varobj in iter(sorted(six.iteritems(obj))): for varname, varobj in iter(sorted(obj.items())):
path.append(varname) path.append(varname)
yield (path, varobj) yield (path, varobj)

View File

@ -3,7 +3,6 @@
import importlib import importlib
import inspect import inspect
from six import text_type
from stix2patterns.exceptions import ParseException from stix2patterns.exceptions import ParseException
from stix2patterns.grammars.STIXPatternParser import TerminalNode from stix2patterns.grammars.STIXPatternParser import TerminalNode
from stix2patterns.v20.grammars.STIXPatternParser import \ from stix2patterns.v20.grammars.STIXPatternParser import \
@ -263,7 +262,7 @@ class STIXPatternVisitorForSTIX2():
property_path.append( property_path.append(
self.instantiate( self.instantiate(
"ListObjectPathComponent", "ListObjectPathComponent",
current.property_name if isinstance(current, BasicObjectPathComponent) else text_type(current), current.property_name if isinstance(current, BasicObjectPathComponent) else str(current),
next.value, next.value,
), ),
) )
@ -286,7 +285,7 @@ class STIXPatternVisitorForSTIX2():
if isinstance(first_component, TerminalNode): if isinstance(first_component, TerminalNode):
step = first_component.getText() step = first_component.getText()
else: else:
step = text_type(first_component) step = str(first_component)
# if step.endswith("_ref"): # if step.endswith("_ref"):
# return stix2.ReferenceObjectPathComponent(step) # return stix2.ReferenceObjectPathComponent(step)
# else: # else:

View File

@ -5,8 +5,6 @@ import binascii
import datetime import datetime
import re import re
import six
from .utils import parse_into_datetime from .utils import parse_into_datetime
@ -15,7 +13,7 @@ def escape_quotes_and_backslashes(s):
def quote_if_needed(x): def quote_if_needed(x):
if isinstance(x, six.string_types): if isinstance(x, str):
if x.find("-") != -1: if x.find("-") != -1:
if not x.startswith("'"): if not x.startswith("'"):
return "'" + x + "'" return "'" + x + "'"

View File

@ -7,8 +7,6 @@ import inspect
import re import re
import uuid import uuid
from six import string_types, text_type
from .base import _STIXBase from .base import _STIXBase
from .exceptions import ( from .exceptions import (
CustomContentError, DictionaryKeyError, MissingPropertiesError, CustomContentError, DictionaryKeyError, MissingPropertiesError,
@ -227,7 +225,7 @@ class ListProperty(Property):
except TypeError: except TypeError:
raise ValueError("must be an iterable.") raise ValueError("must be an iterable.")
if isinstance(value, (_STIXBase, string_types)): if isinstance(value, (_STIXBase, str)):
value = [value] value = [value]
if isinstance(self.contained, Property): if isinstance(self.contained, Property):
@ -268,8 +266,8 @@ class StringProperty(Property):
super(StringProperty, self).__init__(**kwargs) super(StringProperty, self).__init__(**kwargs)
def clean(self, value): def clean(self, value):
if not isinstance(value, string_types): if not isinstance(value, str):
return text_type(value) return str(value)
return value return value

View File

@ -128,18 +128,17 @@ def test_filter_value_type_check():
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter('created', '=', object()) Filter('created', '=', object())
# On Python 2, the type of object() is `<type 'object'>` On Python 3, it's `<class 'object'>`. assert "'<class 'object'>'" in str(excinfo.value)
assert any([s in str(excinfo.value) for s in ["<type 'object'>", "'<class 'object'>'"]])
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter("type", "=", complex(2, -1)) Filter("type", "=", complex(2, -1))
assert any([s in str(excinfo.value) for s in ["<type 'complex'>", "'<class 'complex'>'"]]) assert "'<class 'complex'>'" in str(excinfo.value)
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter("type", "=", set([16, 23])) Filter("type", "=", set([16, 23]))
assert any([s in str(excinfo.value) for s in ["<type 'set'>", "'<class 'set'>'"]]) assert "'<class 'set'>'" in str(excinfo.value)
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)

View File

@ -3,7 +3,6 @@ import json
from medallion.filters.basic_filter import BasicFilter from medallion.filters.basic_filter import BasicFilter
import pytest import pytest
from requests.models import Response from requests.models import Response
import six
from taxii2client.common import _filter_kwargs_to_query_params from taxii2client.common import _filter_kwargs_to_query_params
from taxii2client.v20 import Collection from taxii2client.v20 import Collection
@ -27,7 +26,7 @@ class MockTAXIICollectionEndpoint(Collection):
def add_objects(self, bundle): def add_objects(self, bundle):
self._verify_can_write() self._verify_can_write()
if isinstance(bundle, six.string_types): if isinstance(bundle, str):
bundle = json.loads(bundle) bundle = json.loads(bundle)
for object in bundle.get("objects", []): for object in bundle.get("objects", []):
self.objects.append(object) self.objects.append(object)

View File

@ -146,18 +146,17 @@ def test_filter_value_type_check():
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter('created', '=', object()) Filter('created', '=', object())
# On Python 2, the type of object() is `<type 'object'>` On Python 3, it's `<class 'object'>`. assert "'<class 'object'>'" in str(excinfo.value)
assert any([s in str(excinfo.value) for s in ["<type 'object'>", "'<class 'object'>'"]])
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter("type", "=", complex(2, -1)) Filter("type", "=", complex(2, -1))
assert any([s in str(excinfo.value) for s in ["<type 'complex'>", "'<class 'complex'>'"]]) assert "'<class 'complex'>'" in str(excinfo.value)
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
Filter("type", "=", set([16, 23])) Filter("type", "=", set([16, 23]))
assert any([s in str(excinfo.value) for s in ["<type 'set'>", "'<class 'set'>'"]]) assert "'<class 'set'>'" in str(excinfo.value)
assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value) assert "is not supported. The type must be a Python immutable type or dictionary" in str(excinfo.value)

View File

@ -3,7 +3,6 @@ import json
from medallion.filters.basic_filter import BasicFilter from medallion.filters.basic_filter import BasicFilter
import pytest import pytest
from requests.models import Response from requests.models import Response
import six
from taxii2client.common import _filter_kwargs_to_query_params from taxii2client.common import _filter_kwargs_to_query_params
from taxii2client.v21 import Collection from taxii2client.v21 import Collection
@ -27,7 +26,7 @@ class MockTAXIICollectionEndpoint(Collection):
def add_objects(self, bundle): def add_objects(self, bundle):
self._verify_can_write() self._verify_can_write()
if isinstance(bundle, six.string_types): if isinstance(bundle, str):
bundle = json.loads(bundle) bundle = json.loads(bundle)
for object in bundle.get("objects", []): for object in bundle.get("objects", []):
self.objects.append(object) self.objects.append(object)

View File

@ -3,7 +3,6 @@ import datetime
import uuid import uuid
import pytest import pytest
import six
import stix2.base import stix2.base
import stix2.canonicalization.Canonicalize import stix2.canonicalization.Canonicalize
@ -31,12 +30,7 @@ def _make_uuid5(name):
""" """
Make a STIX 2.1+ compliant UUIDv5 from a "name". Make a STIX 2.1+ compliant UUIDv5 from a "name".
""" """
if six.PY3: uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, name)
uuid_ = uuid.uuid5(SCO_DET_ID_NAMESPACE, name)
else:
uuid_ = uuid.uuid5(
SCO_DET_ID_NAMESPACE, name.encode("utf-8"),
)
return uuid_ return uuid_

View File

@ -7,7 +7,6 @@ import json
import re import re
import pytz import pytz
import six
import stix2.registry as mappings import stix2.registry as mappings
import stix2.version import stix2.version
@ -70,7 +69,7 @@ def _to_enum(value, enum_type, enum_default=None):
if not isinstance(value, enum_type): if not isinstance(value, enum_type):
if value is None and enum_default is not None: if value is None and enum_default is not None:
value = enum_default value = enum_default
elif isinstance(value, six.string_types): elif isinstance(value, str):
value = enum_type[value.upper()] value = enum_type[value.upper()]
else: else:
raise TypeError( raise TypeError(

View File

@ -3,8 +3,6 @@
from collections import OrderedDict from collections import OrderedDict
import copy import copy
import six
from ..custom import _custom_marking_builder from ..custom import _custom_marking_builder
from ..markings import _MarkingsMixin from ..markings import _MarkingsMixin
from ..markings.utils import check_tlp_marking from ..markings.utils import check_tlp_marking
@ -21,7 +19,7 @@ def _should_set_millisecond(cr, marking_type):
if marking_type == TLPMarking: if marking_type == TLPMarking:
return True return True
# otherwise, precision is kept from how it was given # otherwise, precision is kept from how it was given
if isinstance(cr, six.string_types): if isinstance(cr, str):
if '.' in cr: if '.' in cr:
return True return True
else: else:

View File

@ -2,9 +2,9 @@
from collections import OrderedDict from collections import OrderedDict
import itertools import itertools
from urllib.parse import quote_plus
import warnings import warnings
from six.moves.urllib.parse import quote_plus
from stix2patterns.validator import run_validator from stix2patterns.validator import run_validator
from ..custom import _custom_object_builder from ..custom import _custom_object_builder