diff options
author | Brian Dolbec <dolsen@gentoo.org> | 2012-11-14 19:43:40 -0800 |
---|---|---|
committer | Brian Dolbec <dolsen@gentoo.org> | 2012-11-14 19:43:40 -0800 |
commit | 37135bfdcf9cae9ed3273d3ef446dc886e809e60 (patch) | |
tree | afebcd2f7c14612d3dd57d2e2c1bc640e52412c1 /mirrorselect | |
parent | Call the base class constructor for ColoredFormatter so the class gets (diff) | |
download | mirrorselect-37135bfdcf9cae9ed3273d3ef446dc886e809e60.tar.gz mirrorselect-37135bfdcf9cae9ed3273d3ef446dc886e809e60.tar.bz2 mirrorselect-37135bfdcf9cae9ed3273d3ef446dc886e809e60.zip |
fix a null selection in the interactive dialog and hopefully a POSIX locale issue
Diffstat (limited to 'mirrorselect')
-rw-r--r-- | mirrorselect/output.py | 40 | ||||
-rw-r--r-- | 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]) |