Add function to urlencode unicode
This commit is contained in:
parent
2bbc41d24d
commit
2013b498d6
1 changed files with 54 additions and 20 deletions
|
@ -9,6 +9,7 @@ from sqlite3 import connect, OperationalError
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from unicodedata import normalize
|
from unicodedata import normalize
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
import urllib
|
||||||
# Originally tried faster cElementTree, but does NOT work reliably with Kodi
|
# Originally tried faster cElementTree, but does NOT work reliably with Kodi
|
||||||
import xml.etree.ElementTree as etree
|
import xml.etree.ElementTree as etree
|
||||||
import defusedxml.ElementTree as defused_etree # etree parse unsafe
|
import defusedxml.ElementTree as defused_etree # etree parse unsafe
|
||||||
|
@ -269,17 +270,22 @@ def cast(func, value):
|
||||||
func (func): Calback function to used cast to type (int, bool, float).
|
func (func): Calback function to used cast to type (int, bool, float).
|
||||||
value (any): value to be cast and returned.
|
value (any): value to be cast and returned.
|
||||||
"""
|
"""
|
||||||
if value is not None:
|
if value is None:
|
||||||
if func == bool:
|
return value
|
||||||
|
elif func == bool:
|
||||||
return bool(int(value))
|
return bool(int(value))
|
||||||
elif func == unicode:
|
elif func == unicode:
|
||||||
if isinstance(value, (int, long, float)):
|
if isinstance(value, (int, long, float)):
|
||||||
return unicode(value)
|
return unicode(value)
|
||||||
|
elif isinstance(value, unicode):
|
||||||
|
return value
|
||||||
else:
|
else:
|
||||||
return value.decode('utf-8')
|
return value.decode('utf-8')
|
||||||
elif func == str:
|
elif func == str:
|
||||||
if isinstance(value, (int, long, float)):
|
if isinstance(value, (int, long, float)):
|
||||||
return str(value)
|
return str(value)
|
||||||
|
elif isinstance(value, str):
|
||||||
|
return value
|
||||||
else:
|
else:
|
||||||
return value.encode('utf-8')
|
return value.encode('utf-8')
|
||||||
elif func in (int, float):
|
elif func in (int, float):
|
||||||
|
@ -288,7 +294,35 @@ def cast(func, value):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return float('nan')
|
return float('nan')
|
||||||
return func(value)
|
return func(value)
|
||||||
return value
|
|
||||||
|
|
||||||
|
def extend_url(url, params):
|
||||||
|
"""
|
||||||
|
Pass in an url [unicode] and params [dict]. Returns the extended url
|
||||||
|
'<url><? or &><urllib.urlencode(params)>'
|
||||||
|
in unicode
|
||||||
|
"""
|
||||||
|
params = encode_dict(params) if params else {}
|
||||||
|
params = urllib.urlencode(params).decode('utf-8')
|
||||||
|
if '?' in url:
|
||||||
|
return '%s&%s' % (url, params)
|
||||||
|
else:
|
||||||
|
return '%s?%s' % (url, params)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_dict(dictionary):
|
||||||
|
"""
|
||||||
|
Pass in a dict. Will return the same dict with all keys and values encoded
|
||||||
|
in utf-8 - as long as they are unicode. Ignores all non-unicode entries
|
||||||
|
|
||||||
|
Useful for urllib.urlencode or urllib.(un)quote
|
||||||
|
"""
|
||||||
|
for key, value in dictionary.iteritems():
|
||||||
|
if isinstance(key, unicode):
|
||||||
|
dictionary[key.encode('utf-8')] = dictionary.pop(key)
|
||||||
|
if isinstance(value, unicode):
|
||||||
|
dictionary[key] = value.encode('utf-8')
|
||||||
|
return dictionary
|
||||||
|
|
||||||
|
|
||||||
def try_encode(input_str, encoding='utf-8'):
|
def try_encode(input_str, encoding='utf-8'):
|
||||||
|
|
Loading…
Reference in a new issue