Spade
Mini Shell
| Directory:~$ /lib/python2.7/site-packages/requests_toolbelt/multipart/ |
| [Home] [System Details] [Kill Me] |
�
ӵ
Yc@sdZddlZddlZddlZddlmZddlmZdefd��YZ d�Z
d efd
��YZd�Zd�Z
d
�Zejd��Zd�Zd�Zdefd��YZdejfd��YZdefd��YZdS(s�
requests_toolbelt.multipart.encoder
===================================
This holds all of the implementation details of the MultipartEncoder
i����N(tuuid4i(tfieldstMultipartEncodercBs�eZdZddd�Zed��Zd�Zd�Zd�Z d�Z
d�Zd �Zd
�Z
d�Zd�Zd
�Zd�Zed��Zd�Zdd�ZRS(s
The ``MultipartEncoder`` oject is a generic interface to the engine
that
will create a ``multipart/form-data`` body for you.
The basic usage is:
.. code-block:: python
import requests
from requests_toolbelt import MultipartEncoder
encoder = MultipartEncoder({'field': 'value',
'other_field',
'other_value'})
r = requests.post('https://httpbin.org/post',
data=encoder,
headers={'Content-Type':
encoder.content_type})
If you do not need to take advantage of streaming the post body, you
can
also do:
.. code-block:: python
r = requests.post('https://httpbin.org/post',
data=encoder.to_string(),
headers={'Content-Type':
encoder.content_type})
If you want the encoder to use a specific order, you can use an
OrderedDict or more simply, a list of tuples:
.. code-block:: python
encoder = MultipartEncoder([('field', 'value'),
('other_field',
'other_value')])
.. versionchanged:: 0.4.0
You can also provide tuples as part values as you would provide them to
requests' ``files`` parameter.
.. code-block:: python
encoder = MultipartEncoder({
'field': ('file_name',
b'{"a": "b"}', 'application/json',
{'X-My-Header': 'my-value'})
])
.. warning::
This object will end up directly in :mod:`httplib`. Currently,
:mod:`httplib` has a hard-coded read size of **8192 bytes**. This
means that it will loop until the file has been read and your
upload
could take a while. This is **not** a bug in requests. A feature is
being considered for this object to allow you, the user, to specify
what size should be returned on a read. If you have opinions on
this,
please weigh in on `this issue`_.
.. _this issue:
https://github.com/sigmavirus24/requests-toolbelt/issues/75
sutf-8cCs�|pt�j|_dj|j�|_||_djt|j|j�td|j�g�|_||_ t
|_g|_t
g�|_d|_d|_td|�|_|j�|j�dS(Ns--{0}ts
tencoding(Rthextboundary_valuetformattboundaryRtjointencode_witht_encoded_boundaryRtFalsetfinishedtpartstitert_iter_partstNonet
_current_partt_lent
CustomBytesIOt_buffert_prepare_partst_write_boundary(tselfRRR((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt__init__Qs
cCs|jp|j�S(svLength of the multipart/form-data body.
requests will first attempt to get the length of the body by
calling
``len(body)`` and then by checking for the ``len`` attribute.
On 32-bit systems, the ``__len__`` method cannot return anything
larger than an integer (in C) can hold. If the total size of the
body
is even slightly larger than 4GB users will see an OverflowError.
This
manifested itself in `bug #80`_.
As such, we now calculate the length lazily as a property.
.. _bug #80:
https://github.com/sigmavirus24/requests-toolbelt/issues/80
(Rt_calculate_length(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pytlen|scCsdj|j�S(Ns<MultipartEncoder:
{0!r}>(RR(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt__repr__�scs@t|j��t�fd�|jD���d|_|jS(s�
This uses the parts to calculate the length of the body.
This returns the calculated length so __len__ can be lazy.
c3s#|]}�t|�dVqdS(iN(t total_len(t.0tp(tboundary_len(sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pys <genexpr>�si(RRtsumRR(R((R
sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR�s*cCs'|t|j�}|dkr#|SdS(s�This
calculates how many bytes need to be added to the buffer.
When a consumer read's ``x`` from the buffer, there are two
cases to
satisfy:
1. Enough data in the buffer to return the requested amount
2. Not enough data
This function uses the amount of unread bytes in the buffer and
determines how much the Encoder has to load before it can return
the
requested amount of bytes.
:param int read_size: the number of bytes the consumer requests
:returns: int -- the number of bytes that must be loaded into the
buffer before the read can be satisfied. This will be strictly
non-negative
i(RR(Rt read_sizetamount((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt_calculate_load_amount�scCs�|jj�|jp|j�}x�|dks=|dkr�d}|j�s�||jd�7}||j�7}|j�}n|s�||j�7}t|_ Pn||j
|j|�7}|dkr%||8}q%q%WdS(s0Load ``amount`` number of bytes into
the buffer.i����is
N(Rtsmart_truncateRt
_next_parttbytes_left_to_writet_writeRt_write_closing_boundarytTrueR
twrite_to(RR#tparttwritten((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt_load�s
cCs8yt|j�}|_Wntk
r3d}nX|S(N(tnextRRt
StopIterationR(RR((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR&�s
c
cs|j}t|jd�r3t|jj��}nx�|D]�\}}d}d}d}t|ttf�r�t|�dkr�|\}}q�t|�dkr�|\}}}q�|\}}}}n|}tjd|d|d|d|�}|j d|�|Vq:WdS( Ntitemsiitnametdatatfilenametheaderstcontent_type(
RthasattrtlistR1Rt
isinstancettupleRtRequestFieldtmake_multipart( Rt_fieldstktvt file_namet file_typetfile_headerstfile_pointertfield((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt_iter_fields�s& cCsM|j}g|j�D]}tj||�^q|_t|j�|_dS(s�This
uses the fields provided by the user and creates Part objects.
It populates the `parts` attribute and uses that to create a
generator for iteration.
N(RREtPartt
from_fieldRRR(Rtenctf((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR�s .cCs|jj|�S(s�Write
the bytes to the end of the buffer.
:param bytes bytes_to_write: byte-string (or bytearray) to append
to
the buffer
:returns: int -- the number of bytes written
(Rtappend(Rtbytes_to_write((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR(�scCs|j|j�S(s,Write
the boundary to the end of the
buffer.(R(R(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR�scCs=t|j��(|jjdd�|jjd�WdQXdS(s?Write
the bytes necessary to finish a multipart/form-data
body.i����is--
N(tresetRtseektwrite(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR)scCs|jt||j��S(s/Write
the current part's headers to the buffer.(R(R
R(RR5((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt_write_headersscCstdj|j��S(Ns!multipart/form-data;
boundary={0}(tstrRR(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR6scCs
|j�S(s�Return the entirety of the data in the encoder.
.. note::
This simply reads all of the data it can. If you have started
streaming or reading data from the encoder, this method will
only
return whatever data is left in the encoder.
.. note::
This method affects the internal state of the encoder. Calling
this method will exhaust the encoder.
:returns: the multipart message
:rtype: bytes
(tread(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt to_stringsi����cCsl|jr|jj|�S|}|dkrO|dk rO|jt|��}n|j|�|jj|�S(s�Read
data from the streaming encoder.
:param int size: (optional), If provided, ``read`` will return
exactly
that many bytes. If it is not provided, it will return the
remaining bytes.
:returns: bytes
i����N(R
RRQRR$tintR.(Rtsizet
bytes_to_load((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRQ%s
N(t__name__t
__module__t__doc__RRtpropertyRRRR$R.R&RERR(RR)ROR6RRRQ(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRs"=+
cCs|S(N((tmonitor((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pytIDENTITY8stMultipartEncoderMonitorcBsVeZdZdd�Zedddd��Zed��Zd�Z dd�Z
RS( s�
An object used to monitor the progress of a :class:`MultipartEncoder`.
The :class:`MultipartEncoder` should only be responsible for preparing
and
streaming the data. For anyone who wishes to monitor it, they
shouldn't be
using that instance to manage that as well. Using this class, they can
monitor an encoder and register a callback. The callback receives the
instance of the monitor.
To use this monitor, you construct your :class:`MultipartEncoder` as
you
normally would.
.. code-block:: python
from requests_toolbelt import (MultipartEncoder,
MultipartEncoderMonitor)
import requests
def callback(encoder, bytes_read):
# Do something with this information
pass
m = MultipartEncoder(fields={'field0':
'value0'})
monitor = MultipartEncoderMonitor(m, callback)
headers = {'Content-Type': montior.content_type}
r = requests.post('https://httpbin.org/post',
data=monitor,
headers=headers)
Alternatively, if your use case is very simple, you can use the
following
pattern.
.. code-block:: python
from requests_toolbelt import MultipartEncoderMonitor
import requests
def callback(encoder, bytes_read):
# Do something with this information
pass
monitor = MultipartEncoderMonitor.from_fields(
fields={'field0': 'value0'}, callback
)
headers = {'Content-Type': montior.content_type}
r = requests.post('https://httpbin.org/post',
data=monitor,
headers=headers)
cCs4||_|pt|_d|_|jj|_dS(Ni(tencoderR[tcallbackt
bytes_readR(RR]R^((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRos sutf-8cCst|||�}|||�S(N(R(tclsRRRR^R]((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pytfrom_fields}scCs
|jjS(N(R]R6(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR6�scCs
|j�S(N(RQ(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRR�si����cCs8|jj|�}|jt|�7_|j|�|S(N(R]RQR_RR^(RRTtstring((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRQ�s
N(RVRWRXRRtclassmethodRaRYR6RRRQ(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR\<s1 cCs,|dkpt|t�s(|j|�S|S(s6Encoding
``string`` with ``encoding`` if necessary.
:param str string: If string is a bytes object, it will not encode it.
Otherwise, this function will encode it with the provided encoding.
:param str encoding: The encoding with which to encode string.
:returns: encoded bytes object
N(RR9tbytestencode(RbR((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR
�s
cCs t|d�r|St||�S(s4Coerce the data to an object
with a ``read``
method.RQ(R7R(R3R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt
readable_data�scCs�t|d�rt|�St|d�r/|jSt|d�rxy|j�}Wntjk
rdqxXtj|�jSnt|d�r�t|j��SdS(Nt__len__Rtfilenotgetvalue( R7RRhtiotUnsupportedOperationtostfstattst_sizeRi(toRh((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR�s
ccs5|j�}|jdd�dV|j|d�dS(sKeep
track of the buffer's current position and write to the end.
This is a context manager meant to be used when adding data to the
buffer.
It eliminates the need for every function to be concerned with the
position of the cursor in the buffer.
iiN(ttellRM(tbuffertoriginal_position((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRL�scCsmt|t�sit|d�r1t|j�|�St|d�rJt|�St|d�sit||�Sn|S(s5Ensure
that every object's __len__ behaves
uniformly.RiRhRQ(R9RR7RitFileWrapper(R3R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pytcoerce_data�s
cCs)t|d�rt|j��St|�S(NR1(R7R8R1(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pytto_list�sRFcBs2eZd�Zed��Zd�Zd�ZRS(cCs>||_||_t|_t|j�t|j�|_dS(N(R5tbodyR*theaders_unreadRR(RR5Rv((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR�s cCs4t|j�|�}t|j|�}|||�S(s8Create
a part from a Request Field generated by urllib3.(R
trender_headersRtR3(R`RDRR5Rv((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRG�scCs<d}|jr%|t|j�7}n|t|j�dkS(s�Determine
if there are bytes left to write.
:returns: bool -- ``True`` if there are bytes left to write,
otherwise
``False``
i(RwRR5RRv(Rtto_read((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR'�s cCs�d}|jr1||j|j�7}t|_nxot|j�dkr�|dksa||kr�|}|dkr�||}n||j|jj|��7}q4W|S(s�Write
the requested amount of bytes to the buffer provided.
The number of bytes written may exceed size on the first read since
we
load the headers ambitiously.
:param CustomBytesIO buffer: buffer we want to write bytes to
:param int size: number of bytes requested to be written to the
buffer
:returns: int -- number of bytes actually written
ii����(RwRJR5RRRvRQ(RRqRTR-tamount_to_read((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR+�s
0
#(RVRWRRcRGR'R+(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRF�s RcBsAeZddd�Zd�Zed��Zd�Zd�ZRS(sutf-8cCs)t||�}tt|�j|�dS(N(R
tsuperRR(RRqR((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRscCs<|j�}|jdd�|j�}|j|d�|S(Nii(RpRM(Rtcurrent_postlength((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt_get_ends
cCs|j�}||j�S(N(R~Rp(RR}((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRscCs&t|��|j|�}WdQX|S(N(RLRN(RRdR-((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRJ
s
cCsrt|�}|j�|}||krn|j�}|jdd�|j�|j|�|jdd�ndS(Ni(RR~RQRMttruncateRN(Rt
to_be_readtalready_readt old_bytes((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR%%s
N( RVRWRRR~RYRRJR%(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRs
RscBs,eZd�Zed��Zdd�ZRS(cCs
||_dS(N(tfd(Rtfile_object((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR2scCst|j�|jj�S(N(RR�Rp(R((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR5si����cCs|jj|�S(N(R�RQ(RR}((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRQ9s(RVRWRRYRRQ(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyRs1s (RXt
contextlibRjRltuuidRt_compatRtobjectRR[R\R
RfRtcontextmanagerRLRtRuRFtBytesIORRs(((sG/usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt<module> s$�' U
2"