def unquote_plus(string, errors='ignore'): """A wrapper for urllib.unquote_plus() that UTF-8 decodes strs. Should always return a unicode. errors - What to do on a decoding error? Default behavior is to ignore bytes that we can't decode.""" return from_utf8(urllib.unquote_plus(to_utf8(string)), errors=errors)
def parse_qs(query_string, errors='ignore'): """Wrap urlparse.parse_qs() to handle URL-encoded strings stored in class unicode and similar travesties.""" # Actually from urlparse, not urllib; might get its own urlparse_utf8.py some day. kwargs_bytes = urlparse.parse_qs(to_utf8(query_string)) kwargs = dict((( key, [from_utf8(value, errors=errors) for value in value_list], ) for key, value_list in kwargs_bytes.iteritems())) return kwargs
def parse_qs(query_string, errors='ignore'): """Wrap urlparse.parse_qs() to handle URL-encoded strings stored in class unicode and similar travesties.""" # Actually from urlparse, not urllib; might get its own urlparse_utf8.py some day. kwargs_bytes = urlparse.parse_qs(to_utf8(query_string)) kwargs = dict(( ( key, [from_utf8(value, errors=errors) for value in value_list], ) for key, value_list in kwargs_bytes.items() )) return kwargs
def splitvalue(string): return urllib.splitvalue(to_utf8(string))
def quote_plus(string, *args, **kwargs): """A wrapper for urllib.quote_plus() that UTF-8 encodes string if query is unicode. Always returns a str.""" assert isinstance(string, basestring), type(string) return urllib.quote_plus(to_utf8(string), *args, **kwargs)
def quote(string, *args, **kwargs): """A wrapper for urllib.quote() that UTF-8 encodes string if query is unicode. Always returns a str.""" return urllib.quote(to_utf8(string), *args, **kwargs)
def urlencode(query, *args, **kwargs): """A wrapper for urllib.urlencode() that UTF-8 encodes query if query is unicode. Always returns a str.""" return urllib.urlencode([(to_utf8(k), to_utf8(v)) for (k, v) in _pairs(query)], *args, **kwargs)
def encode_pair(key, val): if doseq and isinstance(val, (list, tuple)): return to_utf8(key), [to_utf8(item) for item in val] else: return to_utf8(key), to_utf8(val)
def splitvalue(string): return urlparse.splitvalue(to_utf8(string))