From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 9AF111381F3 for ; Thu, 15 Nov 2012 03:44:21 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0F22DE055B; Thu, 15 Nov 2012 03:44:12 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6A2CDE055B for ; Thu, 15 Nov 2012 03:44:12 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 2DDCB33DBB9 for ; Thu, 15 Nov 2012 03:44:11 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 0BA3DE5436 for ; Thu, 15 Nov 2012 03:44:08 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1352951020.37135bfdcf9cae9ed3273d3ef446dc886e809e60.dol-sen@gentoo> Subject: [gentoo-commits] proj/mirrorselect:master commit in: mirrorselect/ X-VCS-Repository: proj/mirrorselect X-VCS-Files: mirrorselect/output.py mirrorselect/selectors.py X-VCS-Directories: mirrorselect/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: 37135bfdcf9cae9ed3273d3ef446dc886e809e60 X-VCS-Branch: master Date: Thu, 15 Nov 2012 03:44:08 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 55dda8e2-3d2f-4346-91bc-83094f063e92 X-Archives-Hash: b9aa2105eada0f4225b3322e5e40103c commit: 37135bfdcf9cae9ed3273d3ef446dc886e809e60 Author: Brian Dolbec gentoo org> AuthorDate: Thu Nov 15 03:43:40 2012 +0000 Commit: Brian Dolbec gmail com> CommitDate: Thu Nov 15 03:43:40 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/mirrorselect.git;a=commit;h=37135bfd fix a null selection in the interactive dialog and hopefully a POSIX locale issue --- mirrorselect/output.py | 40 ++++++++++++++++++++++++++++++++++++++++ mirrorselect/selectors.py | 16 ++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/mirrorselect/output.py b/mirrorselect/output.py index 632791a..fab3d9a 100644 --- a/mirrorselect/output.py +++ b/mirrorselect/output.py @@ -31,10 +31,50 @@ Distributed under the terms of the GNU General Public License v2 import sys import re +import codecs from optparse import IndentedHelpFormatter +if sys.hexversion >= 0x3000000: + _unicode = str +else: + _unicode = unicode + + +def encoder(text, _encoding_): + return codecs.encode(text, _encoding_, 'replace') + + +def decode_selection(selection): + '''utility function to decode a list of strings + accoring to the filesystem encoding + ''' + # fix None passed in, return an empty list + selection = selection or [] + enc = sys.getfilesystemencoding() + if enc is not None: + return [encoder(i, enc) for i in selection] + return selection + + +def get_encoding(output): + if hasattr(output, 'encoding') \ + and output.encoding != None: + return output.encoding + else: + encoding = locale.getpreferredencoding() + # Make sure that python knows the encoding. Bug 350156 + try: + # We don't care about what is returned, we just want to + # verify that we can find a codec. + codecs.lookup(encoding) + except LookupError: + # Python does not know the encoding, so use utf-8. + encoding = 'utf_8' + return encoding + + class Output(object): """Handles text output. Only prints messages with level <= verbosity. Therefore, verbosity=2 is everything (debug), and verbosity=0 is urgent diff --git a/mirrorselect/selectors.py b/mirrorselect/selectors.py index e3ad7d0..b2a5fc7 100644 --- a/mirrorselect/selectors.py +++ b/mirrorselect/selectors.py @@ -40,16 +40,15 @@ if int(sys.version[0]) == 3: import urllib.request, urllib.parse, urllib.error url_parse = urllib.parse url_open = urllib.request.urlopen - _unicode = str else: import urllib import urlparse url_parse = urlparse.urlparse url_open = urllib.urlopen - _unicode = unicode from mirrorselect.mirrorparser3 import MirrorParser3 +from mirrorselect.output import encoder, get_encoding, decode_selection class Extractor(object): @@ -498,7 +497,7 @@ class Interactive(object): self.interactive(hosts, options) self.output.write('Interactive.interactive(): self.urls = %s\n' % self.urls, 2) - if len(self.urls[0]) == 0: + if not self.urls or len(self.urls[0]) == 0: sys.exit(1) @@ -532,7 +531,7 @@ class Interactive(object): dialog.extend(["%s" %url, "%s%s: %s" %(marker, args['country'], args['name']), "OFF"]) - dialog = [_unicode(x) for x in dialog] + dialog = [encoder(x, get_encoding(sys.stdout)) for x in dialog] proc = subprocess.Popen( dialog, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -540,8 +539,9 @@ class Interactive(object): self.urls = out.splitlines() - if hasattr(self.urls[0], 'decode'): - self.urls = [x.decode('utf-8').rstrip() for x in self.urls] - else: - self.urls = [x.rstrip() for x in self.urls] + if self.urls: + if hasattr(self.urls[0], 'decode'): + self.urls = decode_selection([x.decode('utf-8').rstrip() for x in self.urls]) + else: + self.urls = decode_selection([x.rstrip() for x in self.urls])