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: Py3 compatibility with six --- gitlab.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'gitlab.py') diff --git a/gitlab.py b/gitlab.py index 50eae96..6074002 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 @@ class Gitlab(object): 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 @@ class Gitlab(object): # 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 @@ class GitlabObject(object): 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 @@ class GitlabObject(object): 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 @@ class GitlabObject(object): 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: -- cgit v1.2.1