diff --git a/wicd/misc.py b/wicd/misc.py index c749eda..fd83791 100644 --- a/wicd/misc.py +++ b/wicd/misc.py @@ -415,6 +415,18 @@ def noneToString(text): else: return to_unicode(text) +def sanitize_escaped(s): + """ Sanitize double-escaped unicode strings. """ + lastpos = 0 + while True: + lastpos = s.find('\\x', lastpos + 1) + #print lastpos + if lastpos == -1: + break + c = s[lastpos+2:lastpos+4] # i.e. get the next two characters + s = s.replace('\\x'+c, chr(int(c, 16))) + return s + def to_unicode(x): """ Attempts to convert a string to utf-8. """ # If this is a unicode string, encode it and return @@ -423,17 +435,7 @@ def to_unicode(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) + x = sanitize_escaped(x) encoding = locale.getpreferredencoding() try: