Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/requests/packages/urllib3/fields.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 import email.utils | |
2 import mimetypes | |
3 | |
4 from .packages import six | |
5 | |
6 | |
7 def guess_content_type(filename, default='application/octet-stream'): | |
8 """ | |
9 Guess the "Content-Type" of a file. | |
10 | |
11 :param filename: | |
12 The filename to guess the "Content-Type" of using :mod:`mimetypes`. | |
13 :param default: | |
14 If no "Content-Type" can be guessed, default to `default`. | |
15 """ | |
16 if filename: | |
17 return mimetypes.guess_type(filename)[0] or default | |
18 return default | |
19 | |
20 | |
21 def format_header_param(name, value): | |
22 """ | |
23 Helper function to format and quote a single header parameter. | |
24 | |
25 Particularly useful for header parameters which might contain | |
26 non-ASCII values, like file names. This follows RFC 2231, as | |
27 suggested by RFC 2388 Section 4.4. | |
28 | |
29 :param name: | |
30 The name of the parameter, a string expected to be ASCII only. | |
31 :param value: | |
32 The value of the parameter, provided as a unicode string. | |
33 """ | |
34 if not any(ch in value for ch in '"\\\r\n'): | |
35 result = '%s="%s"' % (name, value) | |
36 try: | |
37 result.encode('ascii') | |
38 except UnicodeEncodeError: | |
39 pass | |
40 else: | |
41 return result | |
42 if not six.PY3: # Python 2: | |
43 value = value.encode('utf-8') | |
44 value = email.utils.encode_rfc2231(value, 'utf-8') | |
45 value = '%s*=%s' % (name, value) | |
46 return value | |
47 | |
48 | |
49 class RequestField(object): | |
50 """ | |
51 A data container for request body parameters. | |
52 | |
53 :param name: | |
54 The name of this request field. | |
55 :param data: | |
56 The data/value body. | |
57 :param filename: | |
58 An optional filename of the request field. | |
59 :param headers: | |
60 An optional dict-like object of headers to initially use for the field. | |
61 """ | |
62 def __init__(self, name, data, filename=None, headers=None): | |
63 self._name = name | |
64 self._filename = filename | |
65 self.data = data | |
66 self.headers = {} | |
67 if headers: | |
68 self.headers = dict(headers) | |
69 | |
70 @classmethod | |
71 def from_tuples(cls, fieldname, value): | |
72 """ | |
73 A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters. | |
74 | |
75 Supports constructing :class:`~urllib3.fields.RequestField` from | |
76 parameter of key/value strings AND key/filetuple. A filetuple is a | |
77 (filename, data, MIME type) tuple where the MIME type is optional. | |
78 For example:: | |
79 | |
80 'foo': 'bar', | |
81 'fakefile': ('foofile.txt', 'contents of foofile'), | |
82 'realfile': ('barfile.txt', open('realfile').read()), | |
83 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'), | |
84 'nonamefile': 'contents of nonamefile field', | |
85 | |
86 Field names and filenames must be unicode. | |
87 """ | |
88 if isinstance(value, tuple): | |
89 if len(value) == 3: | |
90 filename, data, content_type = value | |
91 else: | |
92 filename, data = value | |
93 content_type = guess_content_type(filename) | |
94 else: | |
95 filename = None | |
96 content_type = None | |
97 data = value | |
98 | |
99 request_param = cls(fieldname, data, filename=filename) | |
100 request_param.make_multipart(content_type=content_type) | |
101 | |
102 return request_param | |
103 | |
104 def _render_part(self, name, value): | |
105 """ | |
106 Overridable helper function to format a single header parameter. | |
107 | |
108 :param name: | |
109 The name of the parameter, a string expected to be ASCII only. | |
110 :param value: | |
111 The value of the parameter, provided as a unicode string. | |
112 """ | |
113 return format_header_param(name, value) | |
114 | |
115 def _render_parts(self, header_parts): | |
116 """ | |
117 Helper function to format and quote a single header. | |
118 | |
119 Useful for single headers that are composed of multiple items. E.g., | |
120 'Content-Disposition' fields. | |
121 | |
122 :param header_parts: | |
123 A sequence of (k, v) typles or a :class:`dict` of (k, v) to format | |
124 as `k1="v1"; k2="v2"; ...`. | |
125 """ | |
126 parts = [] | |
127 iterable = header_parts | |
128 if isinstance(header_parts, dict): | |
129 iterable = header_parts.items() | |
130 | |
131 for name, value in iterable: | |
132 if value: | |
133 parts.append(self._render_part(name, value)) | |
134 | |
135 return '; '.join(parts) | |
136 | |
137 def render_headers(self): | |
138 """ | |
139 Renders the headers for this request field. | |
140 """ | |
141 lines = [] | |
142 | |
143 sort_keys = ['Content-Disposition', 'Content-Type', 'Content-Location'] | |
144 for sort_key in sort_keys: | |
145 if self.headers.get(sort_key, False): | |
146 lines.append('%s: %s' % (sort_key, self.headers[sort_key])) | |
147 | |
148 for header_name, header_value in self.headers.items(): | |
149 if header_name not in sort_keys: | |
150 if header_value: | |
151 lines.append('%s: %s' % (header_name, header_value)) | |
152 | |
153 lines.append('\r\n') | |
154 return '\r\n'.join(lines) | |
155 | |
156 def make_multipart(self, content_disposition=None, content_type=None, | |
157 content_location=None): | |
158 """ | |
159 Makes this request field into a multipart request field. | |
160 | |
161 This method overrides "Content-Disposition", "Content-Type" and | |
162 "Content-Location" headers to the request parameter. | |
163 | |
164 :param content_type: | |
165 The 'Content-Type' of the request body. | |
166 :param content_location: | |
167 The 'Content-Location' of the request body. | |
168 | |
169 """ | |
170 self.headers['Content-Disposition'] = content_disposition or 'form-data' | |
171 self.headers['Content-Disposition'] += '; '.join([ | |
172 '', self._render_parts( | |
173 (('name', self._name), ('filename', self._filename)) | |
174 ) | |
175 ]) | |
176 self.headers['Content-Type'] = content_type | |
177 self.headers['Content-Location'] = content_location |