2828__copyright__ = 'Copyright 2013 Gauvain Pocentek'
2929
3030
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+
3137class jsonEncoder (json .JSONEncoder ):
3238 def default (self , obj ):
3339 if isinstance (obj , GitlabObject ):
@@ -302,7 +308,7 @@ def update(self, obj):
302308 if type (v ) in (int , str , bool ):
303309 d [k ] = str (v )
304310 elif type (v ) == unicode :
305- d [k ] = str (v . encode ( sys . stdout . encoding , "replace" ))
311+ d [k ] = str (stdout_encode ( v ))
306312
307313 try :
308314 r = requests .put (url , d ,
@@ -574,7 +580,7 @@ def _obj_to_str(obj):
574580 s = ", " .join ([GitlabObject ._obj_to_str (x ) for x in obj ])
575581 return "[ %s ]" % s
576582 elif isinstance (obj , unicode ):
577- return obj . encode ( sys . stdout . encoding , "replace" )
583+ return stdout_encode ( obj )
578584 else :
579585 return str (obj )
580586
@@ -585,19 +591,18 @@ def pretty_print(self, depth=0):
585591 if k == self .idAttr :
586592 continue
587593 v = self .__dict__ [k ]
588- pretty_k = k .replace ('_' , '-' ).encode (sys .stdout .encoding ,
589- "replace" )
594+ pretty_k = stdout_encode (k .replace ('_' , '-' ))
590595 if isinstance (v , GitlabObject ):
591596 if depth == 0 :
592597 print ("%s:" % pretty_k )
593598 v .pretty_print (1 )
594599 else :
595- print ("%s: %s" % (pretty_k , v .id ))
600+ print (u"{}: {}" . format (pretty_k , v .id ))
596601 else :
597602 if isinstance (v , Gitlab ):
598603 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 ))
601606
602607 def json (self ):
603608 return json .dumps (self .__dict__ , cls = jsonEncoder )
0 commit comments