comparison venv/lib/python2.7/site-packages/requests_toolbelt/downloadutils/stream.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 """Utilities for dealing with streamed requests."""
3 import collections
4 import re
5
6 from .. import exceptions as exc
7
8 # Regular expressions stolen from werkzeug/http.py
9 # cd2c97bb0a076da2322f11adce0b2731f9193396 L62-L64
10 _QUOTED_STRING_RE = r'"[^"\\]*(?:\\.[^"\\]*)*"'
11 _OPTION_HEADER_PIECE_RE = re.compile(
12 r';\s*(%s|[^\s;=]+)\s*(?:=\s*(%s|[^;]+))?\s*' % (_QUOTED_STRING_RE,
13 _QUOTED_STRING_RE)
14 )
15
16
17 def _get_filename(content_disposition):
18 for match in _OPTION_HEADER_PIECE_RE.finditer(content_disposition):
19 k, v = match.groups()
20 if k == 'filename':
21 return v
22 return None
23
24
25 def stream_response_to_file(response, path=None):
26 """Stream a response body to the specified file.
27
28 Either use the ``path`` provided or use the name provided in the
29 ``Content-Disposition`` header.
30
31 .. warning::
32
33 If you pass this function an open file-like object as the ``path``
34 parameter, the function will not close that file for you.
35
36 .. warning::
37
38 This function will not automatically close the response object
39 passed in as the ``response`` parameter.
40
41 If no ``path`` parameter is supplied, this function will parse the
42 ``Content-Disposition`` header on the response to determine the name of
43 the file as reported by the server.
44
45 .. code-block:: python
46
47 import requests
48 from requests_toolbelt import exceptions
49 from requests_toolbelt.downloadutils import stream
50
51 r = requests.get(url, stream=True)
52 try:
53 filename = stream.stream_response_to_file(r)
54 except exceptions.StreamingError as e:
55 # The toolbelt could not find the filename in the
56 # Content-Disposition
57 print(e.message)
58
59 You can also specify the filename as a string. This will be passed to
60 the built-in :func:`open` and we will read the content into the file.
61
62 .. code-block:: python
63
64 import requests
65 from requests_toolbelt.downloadutils import stream
66
67 r = requests.get(url, stream=True)
68 filename = stream.stream_response_to_file(r, path='myfile')
69
70 Instead, if you want to manage the file object yourself, you need to
71 provide either a :class:`io.BytesIO` object or a file opened with the
72 `'b'` flag. See the two examples below for more details.
73
74 .. code-block:: python
75
76 import requests
77 from requests_toolbelt.downloadutils import stream
78
79 with open('myfile', 'wb') as fd:
80 r = requests.get(url, stream=True)
81 filename = stream.stream_response_to_file(r, path=fd)
82
83 print('{0} saved to {1}'.format(url, filename))
84
85 .. code-block:: python
86
87 import io
88 import requests
89 from requests_toolbelt.downloadutils import stream
90
91 b = io.BytesIO()
92 r = requests.get(url, stream=True)
93 filename = stream.stream_response_to_file(r, path=b)
94 assert filename is None
95
96
97 :param response: A Response object from requests
98 :type response: requests.models.Response
99 :param path: *(optional)*, Either a string with the path to the location
100 to save the response content, or a file-like object expecting bytes.
101 :type path: :class:`str`, or object with a :meth:`write`
102 :returns: The name of the file, if one can be determined, else None
103 :rtype: str
104 :raises: :class:`requests_toolbelt.exceptions.StreamingError`
105 """
106 pre_opened = False
107 fd = None
108 filename = None
109 if path:
110 if isinstance(getattr(path, 'write', None), collections.Callable):
111 pre_opened = True
112 fd = path
113 filename = getattr(fd, 'name', None)
114 else:
115 fd = open(path, 'wb')
116 filename = path
117 else:
118 filename = _get_filename(response.headers['content-disposition'])
119 if filename is None:
120 raise exc.StreamingError(
121 'No filename given to stream response to.'
122 )
123 fd = open(filename, 'wb')
124
125 for chunk in response.iter_content(chunk_size=512):
126 fd.write(chunk)
127
128 if not pre_opened:
129 fd.close()
130
131 return filename