325 def url_quote(self, value, safe=''): |
325 def url_quote(self, value, safe=''): |
326 """urllib.quote is not unicode safe, use this method to do the |
326 """urllib.quote is not unicode safe, use this method to do the |
327 necessary encoding / decoding. Also it's designed to quote each |
327 necessary encoding / decoding. Also it's designed to quote each |
328 part of a url path and so the '/' character will be encoded as well. |
328 part of a url path and so the '/' character will be encoded as well. |
329 """ |
329 """ |
330 if PY2 and isinstance(value, unicode): |
330 if PY2 and isinstance(value, text_type): |
331 quoted = urlquote(value.encode(self.encoding), safe=safe) |
331 quoted = urlquote(value.encode(self.encoding), safe=safe) |
332 return unicode(quoted, self.encoding) |
332 return text_type(quoted, self.encoding) |
333 return urlquote(str(value), safe=safe) |
333 return urlquote(str(value), safe=safe) |
334 |
334 |
335 def url_unquote(self, quoted): |
335 def url_unquote(self, quoted): |
336 """returns a unicode unquoted string |
336 """returns a unicode unquoted string |
337 |
337 |
338 decoding is based on `self.encoding` which is the encoding |
338 decoding is based on `self.encoding` which is the encoding |
339 used in `url_quote` |
339 used in `url_quote` |
340 """ |
340 """ |
341 if PY3: |
341 if PY3: |
342 return urlunquote(quoted) |
342 return urlunquote(quoted) |
343 if isinstance(quoted, unicode): |
343 if isinstance(quoted, text_type): |
344 quoted = quoted.encode(self.encoding) |
344 quoted = quoted.encode(self.encoding) |
345 try: |
345 try: |
346 return unicode(urlunquote(quoted), self.encoding) |
346 return text_type(urlunquote(quoted), self.encoding) |
347 except UnicodeDecodeError: # might occurs on manually typed URLs |
347 except UnicodeDecodeError: # might occurs on manually typed URLs |
348 return unicode(urlunquote(quoted), 'iso-8859-1') |
348 return text_type(urlunquote(quoted), 'iso-8859-1') |
349 |
349 |
350 def url_parse_qsl(self, querystring): |
350 def url_parse_qsl(self, querystring): |
351 """return a list of (key, val) found in the url quoted query string""" |
351 """return a list of (key, val) found in the url quoted query string""" |
352 if PY3: |
352 if PY3: |
353 for key, val in parse_qsl(querystring): |
353 for key, val in parse_qsl(querystring): |
354 yield key, val |
354 yield key, val |
355 return |
355 return |
356 if isinstance(querystring, unicode): |
356 if isinstance(querystring, text_type): |
357 querystring = querystring.encode(self.encoding) |
357 querystring = querystring.encode(self.encoding) |
358 for key, val in parse_qsl(querystring): |
358 for key, val in parse_qsl(querystring): |
359 try: |
359 try: |
360 yield unicode(key, self.encoding), unicode(val, self.encoding) |
360 yield text_type(key, self.encoding), text_type(val, self.encoding) |
361 except UnicodeDecodeError: # might occurs on manually typed URLs |
361 except UnicodeDecodeError: # might occurs on manually typed URLs |
362 yield unicode(key, 'iso-8859-1'), unicode(val, 'iso-8859-1') |
362 yield text_type(key, 'iso-8859-1'), text_type(val, 'iso-8859-1') |
363 |
363 |
364 def rebuild_url(self, url, **newparams): |
364 def rebuild_url(self, url, **newparams): |
365 """return the given url with newparams inserted. If any new params |
365 """return the given url with newparams inserted. If any new params |
366 is already specified in the url, it's overriden by the new value |
366 is already specified in the url, it's overriden by the new value |
367 |
367 |
368 newparams may only be mono-valued. |
368 newparams may only be mono-valued. |
369 """ |
369 """ |
370 if PY2 and isinstance(url, unicode): |
370 if PY2 and isinstance(url, text_type): |
371 url = url.encode(self.encoding) |
371 url = url.encode(self.encoding) |
372 schema, netloc, path, query, fragment = urlsplit(url) |
372 schema, netloc, path, query, fragment = urlsplit(url) |
373 query = parse_qs(query) |
373 query = parse_qs(query) |
374 # sort for testing predictability |
374 # sort for testing predictability |
375 for key, val in sorted(newparams.items()): |
375 for key, val in sorted(newparams.items()): |