From 15c0da5552aa57340d25946bb41d0cd079ec495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4enp=C3=A4=C3=A4?= Date: Thu, 9 Oct 2014 11:47:39 +0300 Subject: [PATCH 1/3] Python3 compatibility --- gitlab.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/gitlab.py b/gitlab.py index 268fe5ce1..50eae9639 100644 --- a/gitlab.py +++ b/gitlab.py @@ -15,10 +15,21 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +from __future__ import print_function, division, absolute_import + +from itertools import chain + import json import requests import sys +if sys.version_info[0] < 3: + PY2=True + str_types = (str, unicode,) +else: + PY2=False + str_types = (str,) + __title__ = 'python-gitlab' __version__ = '0.7' __author__ = 'Gauvain Pocentek' @@ -312,7 +323,7 @@ def update(self, obj): d[k] = str(v) elif type(v) == bool: d[k] = 1 if v else 0 - elif type(v) == unicode: + elif PY2 and type(v) == unicode: d[k] = str(v.encode(self.gitlab_encoding, "replace")) try: @@ -462,7 +473,7 @@ def _get_display_encoding(): def _sanitize(value): - if type(value) in (str, unicode): + if type(value) in str_types: return value.replace('/', '%2F') return value @@ -562,7 +573,7 @@ def delete(self): def __init__(self, gl, data=None, **kwargs): self.gitlab = gl - if data is None or type(data) in [int, str, unicode]: + if data is None or type(data) in chain((int,), str_types): data = self.gitlab.get(self.__class__, data, **kwargs) self._setFromDict(data) @@ -598,7 +609,7 @@ def _obj_to_str(obj): elif isinstance(obj, list): s = ", ".join([GitlabObject._obj_to_str(x) for x in obj]) return "[ %s ]" % s - elif isinstance(obj, unicode): + elif PY2 and isinstance(obj, unicode): return obj.encode(_get_display_encoding(), "replace") else: return str(obj) @@ -611,7 +622,8 @@ def pretty_print(self, depth=0): continue v = self.__dict__[k] pretty_k = k.replace('_', '-') - pretty_k = pretty_k.encode(_get_display_encoding(), "replace") + if PY2: + pretty_k = pretty_k.encode(_get_display_encoding(), "replace") if isinstance(v, GitlabObject): if depth == 0: print("%s:" % pretty_k) From d714c4d35bc627d9113a4925f843c54d6123e621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4enp=C3=A4=C3=A4?= Date: Mon, 13 Oct 2014 09:59:33 +0300 Subject: [PATCH 2/3] Python 3 compatibility for cli-program --- gitlab | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gitlab b/gitlab index 7ddd48c54..f0aa46069 100755 --- a/gitlab +++ b/gitlab @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +from __future__ import print_function, division, absolute_import + import os import sys import re From 431e4bdf089354534f6877d39631ff23038e8866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4enp=C3=A4=C3=A4?= Date: Mon, 13 Oct 2014 16:40:09 +0300 Subject: [PATCH 3/3] Py3 compatibility with six --- gitlab.py | 24 +++++++++--------------- requirements.txt | 1 + setup.py | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/gitlab.py b/gitlab.py index 50eae9639..60740020c 100644 --- a/gitlab.py +++ b/gitlab.py @@ -17,19 +17,12 @@ from __future__ import print_function, division, absolute_import -from itertools import chain +import six import json import requests import sys -if sys.version_info[0] < 3: - PY2=True - str_types = (str, unicode,) -else: - PY2=False - str_types = (str,) - __title__ = 'python-gitlab' __version__ = '0.7' __author__ = 'Gauvain Pocentek' @@ -292,7 +285,7 @@ def create(self, obj): url = obj._url % args url = '%s%s' % (self._url, url) - for k, v in obj.__dict__.items(): + for k, v in list(obj.__dict__.items()): if type(v) == bool: obj.__dict__[k] = 1 if v else 0 @@ -318,12 +311,12 @@ def update(self, obj): # build a dict of data that can really be sent to server d = {} - for k, v in obj.__dict__.items(): + for k, v in list(obj.__dict__.items()): if type(v) in (int, str): d[k] = str(v) elif type(v) == bool: d[k] = 1 if v else 0 - elif PY2 and type(v) == unicode: + elif six.PY2 and type(v) == six.text_type: d[k] = str(v.encode(self.gitlab_encoding, "replace")) try: @@ -473,7 +466,7 @@ def _get_display_encoding(): def _sanitize(value): - if type(value) in str_types: + if isinstance(value, six.string_types): return value.replace('/', '%2F') return value @@ -573,7 +566,8 @@ def delete(self): def __init__(self, gl, data=None, **kwargs): self.gitlab = gl - if data is None or type(data) in chain((int,), str_types): + if data is None or isinstance(data, six.integer_types) or\ + isinstance(data, six.string_types): data = self.gitlab.get(self.__class__, data, **kwargs) self._setFromDict(data) @@ -609,7 +603,7 @@ def _obj_to_str(obj): elif isinstance(obj, list): s = ", ".join([GitlabObject._obj_to_str(x) for x in obj]) return "[ %s ]" % s - elif PY2 and isinstance(obj, unicode): + elif six.PY2 and isinstance(obj, six.text_type): return obj.encode(_get_display_encoding(), "replace") else: return str(obj) @@ -622,7 +616,7 @@ def pretty_print(self, depth=0): continue v = self.__dict__[k] pretty_k = k.replace('_', '-') - if PY2: + if six.PY2: pretty_k = pretty_k.encode(_get_display_encoding(), "replace") if isinstance(v, GitlabObject): if depth == 0: diff --git a/requirements.txt b/requirements.txt index 65d5e04b2..af8843719 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests>1.0 +six diff --git a/setup.py b/setup.py index eeeeaf0d1..4330e6c2a 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def get_version(): url='https://github.com/gpocentek/python-gitlab', py_modules=['gitlab'], scripts=['gitlab'], - install_requires=['requests'], + install_requires=['requests', 'six'], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Console',