28
28
__copyright__ = 'Copyright 2013 Gauvain Pocentek'
29
29
30
30
31
+ def stdout_encode (text ):
32
+ if None != sys .stdout .encoding :
33
+ return text .encode (sys .stdout .encoding , "replace" )
34
+ return text .encode ("iso8859-1" , 'replace' )
35
+
36
+
31
37
class jsonEncoder (json .JSONEncoder ):
32
38
def default (self , obj ):
33
39
if isinstance (obj , GitlabObject ):
@@ -302,7 +308,7 @@ def update(self, obj):
302
308
if type (v ) in (int , str , bool ):
303
309
d [k ] = str (v )
304
310
elif type (v ) == unicode :
305
- d [k ] = str (v . encode ( sys . stdout . encoding , "replace" ))
311
+ d [k ] = str (stdout_encode ( v ))
306
312
307
313
try :
308
314
r = requests .put (url , d ,
@@ -574,7 +580,7 @@ def _obj_to_str(obj):
574
580
s = ", " .join ([GitlabObject ._obj_to_str (x ) for x in obj ])
575
581
return "[ %s ]" % s
576
582
elif isinstance (obj , unicode ):
577
- return obj . encode ( sys . stdout . encoding , "replace" )
583
+ return stdout_encode ( obj )
578
584
else :
579
585
return str (obj )
580
586
@@ -585,19 +591,18 @@ def pretty_print(self, depth=0):
585
591
if k == self .idAttr :
586
592
continue
587
593
v = self .__dict__ [k ]
588
- pretty_k = k .replace ('_' , '-' ).encode (sys .stdout .encoding ,
589
- "replace" )
594
+ pretty_k = stdout_encode (k .replace ('_' , '-' ))
590
595
if isinstance (v , GitlabObject ):
591
596
if depth == 0 :
592
597
print ("%s:" % pretty_k )
593
598
v .pretty_print (1 )
594
599
else :
595
- print ("%s: %s" % (pretty_k , v .id ))
600
+ print (u"{}: {}" . format (pretty_k , v .id ))
596
601
else :
597
602
if isinstance (v , Gitlab ):
598
603
continue
599
- v = GitlabObject ._obj_to_str (v )
600
- print ("%s%s: %s" % ( " " * depth * 2 , pretty_k , v ))
604
+ v = stdout_encode ( GitlabObject ._obj_to_str (v ) )
605
+ print (u"{0}{1}: {2}" . format ( u " " * depth * 2 , pretty_k , v ))
601
606
602
607
def json (self ):
603
608
return json .dumps (self .__dict__ , cls = jsonEncoder )
0 commit comments