Switch to using six (#1189)

With the Python 2.7 deadline fast approaching, modernize the codebase
making use of the modernize module to switch to using six, as well as
other upcoming features, such as absolute imports . Stop using 2to3
for Travis, yay!
This commit is contained in:
Steve Kowalik
2019-08-05 14:06:19 +10:00
committed by GitHub
parent f1ae7200ba
commit dc2f2ad8cb
182 changed files with 850 additions and 629 deletions
+15 -13
View File
@@ -32,12 +32,14 @@
# #
################################################################################
from __future__ import absolute_import
import sys
import datetime
from operator import itemgetter
import GithubException
import Consts
from . import GithubException
from . import Consts
import six
atLeastPython3 = sys.hexversion >= 0x03000000
@@ -140,18 +142,18 @@ class GithubObject(object):
elif isinstance(value, type):
try:
return _ValuedAttribute(transform(value))
except Exception, e:
except Exception as e:
return _BadAttribute(value, type, e)
else:
return _BadAttribute(value, type)
@staticmethod
def _makeStringAttribute(value):
return GithubObject.__makeSimpleAttribute(value, (str, unicode))
return GithubObject.__makeSimpleAttribute(value, (str, six.text_type))
@staticmethod
def _makeIntAttribute(value):
return GithubObject.__makeSimpleAttribute(value, (int, long))
return GithubObject.__makeSimpleAttribute(value, six.integer_types)
@staticmethod
def _makeBoolAttribute(value):
@@ -163,7 +165,7 @@ class GithubObject(object):
@staticmethod
def _makeTimestampAttribute(value):
return GithubObject.__makeTransformedAttribute(value, (int, long), datetime.datetime.utcfromtimestamp)
return GithubObject.__makeTransformedAttribute(value, six.integer_types, datetime.datetime.utcfromtimestamp)
@staticmethod
def _makeDatetimeAttribute(value):
@@ -177,14 +179,14 @@ class GithubObject(object):
else:
return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ")
return GithubObject.__makeTransformedAttribute(value, (str, unicode), parseDatetime)
return GithubObject.__makeTransformedAttribute(value, (str, six.text_type), parseDatetime)
def _makeClassAttribute(self, klass, value):
return GithubObject.__makeTransformedAttribute(value, dict, lambda value: klass(self._requester, self._headers, value, completed=False))
@staticmethod
def _makeListOfStringsAttribute(value):
return GithubObject.__makeSimpleListAttribute(value, (str, unicode))
return GithubObject.__makeSimpleListAttribute(value, (str, six.text_type))
@staticmethod
def _makeListOfIntsAttribute(value):
@@ -205,10 +207,10 @@ class GithubObject(object):
return _BadAttribute(value, [dict])
def _makeDictOfStringsToClassesAttribute(self, klass, value):
if isinstance(value, dict) and all(isinstance(key, (str, unicode)) and isinstance(element, dict) for key, element in value.iteritems()):
return _ValuedAttribute(dict((key, klass(self._requester, self._headers, element, completed=False)) for key, element in value.iteritems()))
if isinstance(value, dict) and all(isinstance(key, (str, six.text_type)) and isinstance(element, dict) for key, element in six.iteritems(value)):
return _ValuedAttribute(dict((key, klass(self._requester, self._headers, element, completed=False)) for key, element in six.iteritems(value)))
else:
return _BadAttribute(value, {(str, unicode): dict})
return _BadAttribute(value, {(str, six.text_type): dict})
@property
def etag(self):
@@ -230,11 +232,11 @@ class GithubObject(object):
"""
def format_params(params):
if atLeastPython3:
items = params.items()
items = list(params.items())
else:
items = list(params.items())
for k, v in sorted(items, key=itemgetter(0), reverse=True):
isText = isinstance(v, (str, unicode))
isText = isinstance(v, (str, six.text_type))
if isText and not atLeastPython3:
v = v.encode('utf-8')
yield '{k}="{v}"'.format(k=k, v=v) if isText else '{k}={v}'.format(k=k, v=v)