Skip to content

Fix encoding errors on display and update with redirected output #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def __init__(self, url, private_token=None,
self.email = email
self.password = password
self.ssl_verify = ssl_verify
# Gitlab should handle UTF-8
self.gitlab_encoding = 'UTF-8'

def auth(self):
"""Performs an authentication using either the private token, or the
Expand Down Expand Up @@ -302,7 +304,7 @@ def update(self, obj):
if type(v) in (int, str, bool):
d[k] = str(v)
elif type(v) == unicode:
d[k] = str(v.encode(sys.stdout.encoding, "replace"))
d[k] = str(v.encode(self.gitlab_encoding, "replace"))

try:
r = requests.put(url, d,
Expand Down Expand Up @@ -445,6 +447,8 @@ def Team(self, id=None, **kwargs):
"""
return self._getListOrObject(Team, id, **kwargs)

def _get_display_encoding():
return sys.stdout.encoding or sys.getdefaultencoding()

class GitlabObject(object):
_url = None
Expand Down Expand Up @@ -574,7 +578,7 @@ def _obj_to_str(obj):
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
return "[ %s ]" % s
elif isinstance(obj, unicode):
return obj.encode(sys.stdout.encoding, "replace")
return obj.encode(_get_display_encoding(), "replace")
else:
return str(obj)

Expand All @@ -585,8 +589,8 @@ def pretty_print(self, depth=0):
if k == self.idAttr:
continue
v = self.__dict__[k]
pretty_k = k.replace('_', '-').encode(sys.stdout.encoding,
"replace")
pretty_k = k.replace('_', '-')
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
if isinstance(v, GitlabObject):
if depth == 0:
print("%s:" % pretty_k)
Expand Down