mirror of
https://github.com/gryf/wicd.git
synced 2026-03-01 14:15:46 +01:00
Try to better handle the double-escaped unicode ESSIDs
This commit is contained in:
24
wicd/misc.py
24
wicd/misc.py
@@ -415,6 +415,18 @@ def noneToString(text):
|
|||||||
else:
|
else:
|
||||||
return to_unicode(text)
|
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):
|
def to_unicode(x):
|
||||||
""" Attempts to convert a string to utf-8. """
|
""" Attempts to convert a string to utf-8. """
|
||||||
# If this is a unicode string, encode it and return
|
# If this is a unicode string, encode it and return
|
||||||
@@ -423,17 +435,7 @@ def to_unicode(x):
|
|||||||
if isinstance(x, unicode):
|
if isinstance(x, unicode):
|
||||||
return x.encode('utf-8')
|
return x.encode('utf-8')
|
||||||
|
|
||||||
# FIXME: this is a workaround to correctly parse utf-8
|
x = sanitize_escaped(x)
|
||||||
# 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()
|
encoding = locale.getpreferredencoding()
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user