comparison venv/lib/python2.7/site-packages/requests_toolbelt/utils/deprecated.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 # -*- coding: utf-8 -*-
2 """A collection of functions deprecated in requests.utils."""
3 import re
4
5 from requests import utils
6
7
8 def get_encodings_from_content(content):
9 """Return encodings from given content string.
10
11 .. code-block:: python
12
13 import requests
14 from requests_toolbelt.utils import deprecated
15
16 r = requests.get(url)
17 encodings = deprecated.get_encodings_from_content(r)
18
19 :param content: bytestring to extract encodings from.
20 :type content: bytes
21 """
22 find_charset = re.compile(
23 r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I
24 ).findall
25
26 find_pragma = re.compile(
27 r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I
28 ).findall
29
30 find_xml = re.compile(
31 r'^<\?xml.*?encoding=["\']*(.+?)["\'>]'
32 ).findall
33
34 return find_charset(content) + find_pragma(content) + find_xml(content)
35
36
37 def get_unicode_from_response(response):
38 """Return the requested content back in unicode.
39
40 This will first attempt to retrieve the encoding from the response
41 headers. If that fails, it will use
42 :func:`requests_toolbelt.utils.deprecated.get_encodings_from_content`
43 to determine encodings from HTML elements.
44
45 .. code-block:: python
46
47 import requests
48 from requests_toolbelt.utils import deprecated
49
50 r = requests.get(url)
51 text = deprecated.get_unicode_from_response(r)
52
53 :param response: Response object to get unicode content from.
54 :type response: requests.models.Response
55 """
56 tried_encodings = set()
57
58 # Try charset from content-type
59 encoding = utils.get_encoding_from_headers(response.headers)
60
61 if encoding:
62 try:
63 return str(response.content, encoding)
64 except UnicodeError:
65 tried_encodings.add(encoding.lower())
66
67 encodings = get_encodings_from_content(response.content)
68
69 for _encoding in encodings:
70 _encoding = _encoding.lower()
71 if _encoding in tried_encodings:
72 continue
73 try:
74 return str(response.content, _encoding)
75 except UnicodeError:
76 tried_encodings.add(_encoding)
77
78 # Fall back:
79 if encoding:
80 try:
81 return str(response.content, encoding, errors='replace')
82 except TypeError:
83 pass
84 return response.text