Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
gpo: Align safe_print() with Python 3's print() (bug 1697)
On Python 3, we don't need to use safe_print() and all our
weird encoding tricks are obsolete then. As we still have to
support Python 2, the safe_print version won't go away yet.
  • Loading branch information
thp committed Oct 23, 2012
1 parent 2cb67e4 commit 2381f17
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions bin/gpo
Expand Up @@ -64,6 +64,8 @@
"""

from __future__ import print_function

import sys
import collections
import os
Expand Down Expand Up @@ -151,7 +153,7 @@ def safe_print(*args, **kwargs):
try:
ofile.write(output)
except Exception, e:
print """
print("""
*** ENCODING FAIL ***
Please report this to http://bugs.gpodder.org/:
Expand All @@ -160,12 +162,16 @@ def safe_print(*args, **kwargs):
map(convert, args) = %s
Exception = %s
""" % (repr(args), repr(map(convert, args)), e)
""" % (repr(args), repr(map(convert, args)), e))

if kwargs.get('newline', True):
ofile.write(os.linesep)
ofile.write(kwargs.get('end', os.linesep))
ofile.flush()

# On Python 3 the encoding insanity is gone, so our safe_print()
# function simply becomes the normal print() function. Good stuff!
if sys.version_info >= (3,):
safe_print = print

# ANSI Colors: red = 1, green = 2, yellow = 3, blue = 4
inred, ingreen, inyellow, inblue = (functools.partial(incolor, x)
for x in range(1, 5))
Expand Down Expand Up @@ -247,13 +253,13 @@ class gPodderCli(object):
else:
line = line + (' '*(self.COLUMNS-7-len(line)))
self._current_action = line
safe_print(self._current_action, newline=False)
safe_print(self._current_action, end='')

def _update_action(self, progress):
if have_ansi:
progress = '%3.0f%%' % (progress*100.,)
result = '['+inblue(progress)+']'
safe_print('\r' + self._current_action + result, newline=False)
safe_print('\r' + self._current_action + result, end='')

def _finish_action(self, success=True, skip=False):
if skip:
Expand Down Expand Up @@ -615,7 +621,7 @@ class gPodderCli(object):
return True

def help(self):
safe_print(stylize(__doc__), file=sys.stderr, newline=False)
safe_print(stylize(__doc__), file=sys.stderr, end='')
return True

# -------------------------------------------------------------------
Expand Down Expand Up @@ -780,5 +786,5 @@ if __name__ == '__main__':
elif interactive_console:
cli._shell()
else:
safe_print(__doc__, newline=False)
safe_print(__doc__, end='')

0 comments on commit 2381f17

Please sign in to comment.