Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/requests/packages/urllib3/filepost.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 codecs | |
2 | |
3 from uuid import uuid4 | |
4 from io import BytesIO | |
5 | |
6 from .packages import six | |
7 from .packages.six import b | |
8 from .fields import RequestField | |
9 | |
10 writer = codecs.lookup('utf-8')[3] | |
11 | |
12 | |
13 def choose_boundary(): | |
14 """ | |
15 Our embarassingly-simple replacement for mimetools.choose_boundary. | |
16 """ | |
17 return uuid4().hex | |
18 | |
19 | |
20 def iter_field_objects(fields): | |
21 """ | |
22 Iterate over fields. | |
23 | |
24 Supports list of (k, v) tuples and dicts, and lists of | |
25 :class:`~urllib3.fields.RequestField`. | |
26 | |
27 """ | |
28 if isinstance(fields, dict): | |
29 i = six.iteritems(fields) | |
30 else: | |
31 i = iter(fields) | |
32 | |
33 for field in i: | |
34 if isinstance(field, RequestField): | |
35 yield field | |
36 else: | |
37 yield RequestField.from_tuples(*field) | |
38 | |
39 | |
40 def iter_fields(fields): | |
41 """ | |
42 .. deprecated:: 1.6 | |
43 | |
44 Iterate over fields. | |
45 | |
46 The addition of :class:`~urllib3.fields.RequestField` makes this function | |
47 obsolete. Instead, use :func:`iter_field_objects`, which returns | |
48 :class:`~urllib3.fields.RequestField` objects. | |
49 | |
50 Supports list of (k, v) tuples and dicts. | |
51 """ | |
52 if isinstance(fields, dict): | |
53 return ((k, v) for k, v in six.iteritems(fields)) | |
54 | |
55 return ((k, v) for k, v in fields) | |
56 | |
57 | |
58 def encode_multipart_formdata(fields, boundary=None): | |
59 """ | |
60 Encode a dictionary of ``fields`` using the multipart/form-data MIME format. | |
61 | |
62 :param fields: | |
63 Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestField`). | |
64 | |
65 :param boundary: | |
66 If not specified, then a random boundary will be generated using | |
67 :func:`mimetools.choose_boundary`. | |
68 """ | |
69 body = BytesIO() | |
70 if boundary is None: | |
71 boundary = choose_boundary() | |
72 | |
73 for field in iter_field_objects(fields): | |
74 body.write(b('--%s\r\n' % (boundary))) | |
75 | |
76 writer(body).write(field.render_headers()) | |
77 data = field.data | |
78 | |
79 if isinstance(data, int): | |
80 data = str(data) # Backwards compatibility | |
81 | |
82 if isinstance(data, six.text_type): | |
83 writer(body).write(data) | |
84 else: | |
85 body.write(data) | |
86 | |
87 body.write(b'\r\n') | |
88 | |
89 body.write(b('--%s--\r\n' % (boundary))) | |
90 | |
91 content_type = str('multipart/form-data; boundary=%s' % boundary) | |
92 | |
93 return body.getvalue(), content_type |