Skip to content

Commit 431e4bd

Browse files
committed
Py3 compatibility with six
1 parent d714c4d commit 431e4bd

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

gitlab.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@
1717

1818
from __future__ import print_function, division, absolute_import
1919

20-
from itertools import chain
20+
import six
2121

2222
import json
2323
import requests
2424
import sys
2525

26-
if sys.version_info[0] < 3:
27-
PY2=True
28-
str_types = (str, unicode,)
29-
else:
30-
PY2=False
31-
str_types = (str,)
32-
3326
__title__ = 'python-gitlab'
3427
__version__ = '0.7'
3528
__author__ = 'Gauvain Pocentek'
@@ -292,7 +285,7 @@ def create(self, obj):
292285
url = obj._url % args
293286
url = '%s%s' % (self._url, url)
294287

295-
for k, v in obj.__dict__.items():
288+
for k, v in list(obj.__dict__.items()):
296289
if type(v) == bool:
297290
obj.__dict__[k] = 1 if v else 0
298291

@@ -318,12 +311,12 @@ def update(self, obj):
318311

319312
# build a dict of data that can really be sent to server
320313
d = {}
321-
for k, v in obj.__dict__.items():
314+
for k, v in list(obj.__dict__.items()):
322315
if type(v) in (int, str):
323316
d[k] = str(v)
324317
elif type(v) == bool:
325318
d[k] = 1 if v else 0
326-
elif PY2 and type(v) == unicode:
319+
elif six.PY2 and type(v) == six.text_type:
327320
d[k] = str(v.encode(self.gitlab_encoding, "replace"))
328321

329322
try:
@@ -473,7 +466,7 @@ def _get_display_encoding():
473466

474467

475468
def _sanitize(value):
476-
if type(value) in str_types:
469+
if isinstance(value, six.string_types):
477470
return value.replace('/', '%2F')
478471
return value
479472

@@ -573,7 +566,8 @@ def delete(self):
573566
def __init__(self, gl, data=None, **kwargs):
574567
self.gitlab = gl
575568

576-
if data is None or type(data) in chain((int,), str_types):
569+
if data is None or isinstance(data, six.integer_types) or\
570+
isinstance(data, six.string_types):
577571
data = self.gitlab.get(self.__class__, data, **kwargs)
578572

579573
self._setFromDict(data)
@@ -609,7 +603,7 @@ def _obj_to_str(obj):
609603
elif isinstance(obj, list):
610604
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
611605
return "[ %s ]" % s
612-
elif PY2 and isinstance(obj, unicode):
606+
elif six.PY2 and isinstance(obj, six.text_type):
613607
return obj.encode(_get_display_encoding(), "replace")
614608
else:
615609
return str(obj)
@@ -622,7 +616,7 @@ def pretty_print(self, depth=0):
622616
continue
623617
v = self.__dict__[k]
624618
pretty_k = k.replace('_', '-')
625-
if PY2:
619+
if six.PY2:
626620
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
627621
if isinstance(v, GitlabObject):
628622
if depth == 0:

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests>1.0
2+
six

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_version():
2222
url='https://github.com/gpocentek/python-gitlab',
2323
py_modules=['gitlab'],
2424
scripts=['gitlab'],
25-
install_requires=['requests'],
25+
install_requires=['requests', 'six'],
2626
classifiers=[
2727
'Development Status :: 5 - Production/Stable',
2828
'Environment :: Console',

0 commit comments

Comments
 (0)