From 69e9e92def36eeaf6df948203212bff547466a36 Mon Sep 17 00:00:00 2001 From: David Paleino Date: Thu, 15 Dec 2011 19:21:53 +0100 Subject: [PATCH] Fix unicode bugs with ESSIDs and keys --- wicd/configmanager.py | 7 +++++-- wicd/misc.py | 15 ++++++++++++++- wicd/monitor.py | 4 ++-- wicd/wnettools.py | 3 +-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/wicd/configmanager.py b/wicd/configmanager.py index 75843c6..9871dad 100644 --- a/wicd/configmanager.py +++ b/wicd/configmanager.py @@ -28,6 +28,7 @@ reusable for other purposes as well. import sys, os from ConfigParser import RawConfigParser, ParsingError +import codecs from wicd.misc import Noneify, to_unicode @@ -116,6 +117,7 @@ class ConfigManager(RawConfigParser): if (isinstance(ret, basestring) and ret.startswith(self.mrk_ws) and ret.endswith(self.mrk_ws)): ret = ret[3:-3] + ret = to_unicode(ret) if default: if self.debug: print ''.join(['found ', option, ' in configuration ', @@ -175,7 +177,8 @@ class ConfigManager(RawConfigParser): in the '.d' directory are read in normal sorted order and section entries in these files override entries in the main file. """ - RawConfigParser.read(self, path) + if os.path.exists(path): + RawConfigParser.readfp(self, codecs.open(path, 'r', 'utf-8')) path_d = path + ".d" files = [] @@ -186,7 +189,7 @@ class ConfigManager(RawConfigParser): for fname in files: p = RawConfigParser() - p.read(fname) + p.readfp(codecs.open(fname, 'r', 'utf-8')) for section_name in p.sections(): # New files override old, so remove first to avoid DuplicateSectionError. self.remove_section(section_name) diff --git a/wicd/misc.py b/wicd/misc.py index dc80c7b..c749eda 100644 --- a/wicd/misc.py +++ b/wicd/misc.py @@ -413,7 +413,7 @@ def noneToString(text): if text in (None, ""): return "None" else: - return str(text) + return to_unicode(text) def to_unicode(x): """ Attempts to convert a string to utf-8. """ @@ -422,6 +422,19 @@ def to_unicode(x): return x if isinstance(x, unicode): return x.encode('utf-8') + + # FIXME: this is a workaround to correctly parse utf-8 + # encoded ESSIDs returned by iwlist -- python replaces + # \xNN with \\xNN, thus losing the characters :/. + # It should really be handled in a better way. Maybe + # using index()/find()? + if '\\' in x: + begin = x.split('\\x')[:1] + chars = x.split('\\x')[1:] + end = [chars[-1][2:]] + chars[-1] = chars[-1][:2] + x = ''.join(begin + map(lambda c: chr(int(c, 16)), chars) + end) + encoding = locale.getpreferredencoding() try: ret = x.decode(encoding).encode('utf-8') diff --git a/wicd/monitor.py b/wicd/monitor.py index 5dcd157..a91d165 100755 --- a/wicd/monitor.py +++ b/wicd/monitor.py @@ -274,10 +274,10 @@ class ConnectionStatus(object): if wired.CheckIfWiredConnecting(): info = ["wired"] else: - info = ["wireless", str(wireless.GetCurrentNetwork(iwconfig))] + info = ["wireless", wireless.GetCurrentNetwork(iwconfig)] elif state == misc.WIRELESS: self.reconnect_tries = 0 - info = [str(wifi_ip), str(wireless.GetCurrentNetwork(iwconfig)), + info = [str(wifi_ip), wireless.GetCurrentNetwork(iwconfig), str(self._get_printable_sig_strength()), str(wireless.GetCurrentNetworkID(iwconfig)), wireless.GetCurrentBitrate(iwconfig)] diff --git a/wicd/wnettools.py b/wicd/wnettools.py index 1239c2f..ddb0189 100644 --- a/wicd/wnettools.py +++ b/wicd/wnettools.py @@ -1502,8 +1502,7 @@ class BaseWirelessInterface(BaseInterface): output = self.GetIwconfig() else: output = iwconfig - network = misc.RunRegex(re.compile('.*ESSID:"(.*?)"', - re.I | re.M | re.S), output) + network = misc.to_unicode(misc.RunRegex(essid_pattern, output)) if network: network = misc.to_unicode(network) return network