1
0
mirror of https://github.com/gryf/wicd.git synced 2025-12-24 15:12:31 +01:00

Try to better handle the double-escaped unicode ESSIDs

This commit is contained in:
David Paleino
2012-02-01 17:09:53 +01:00
parent 63a2e8667d
commit 585b95190e

View File

@@ -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: