a *Na@sdZddlZddlZddlmZddlmZddZGdd d e Z Gd d d e Z d d Z Gddde ZGddde ZdS)z requests_toolbelt.multipart.decoder =================================== This holds all the implementation details of the MultipartDecoder N encode_with)CaseInsensitiveDictcCs*||}|d|||t|dfSN)findlen)contentboundZpointr ~/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/requests_toolbelt/multipart/decoder.py_split_on_finds r c@s eZdZdS) ImproperBodyPartContentExceptionN__name__ __module__ __qualname__r r r r rsrc@s eZdZdS) NonMultipartContentTypeExceptionNrr r r r rsrcsBtjd}|dkr|}tj|}fdd|DS)Nrc3s&|]\}}t|t|fVqdSrr).0kvencodingr r #sz!_header_parser..)sys version_infodecodeemailparser HeaderParserparsestritems)stringrmajorheadersr rr _header_parsers   r&c@s$eZdZdZddZeddZdS)BodyParta The ``BodyPart`` object is a ``Response``-like interface to an individual subpart of a multipart response. It is expected that these will generally be created by objects of the ``MultipartDecoder`` class. Like ``Response``, there is a ``CaseInsensitiveDict`` object named headers, ``content`` to access bytes, ``text`` to access unicode, and ``encoding`` to access the unicode codec. cCsP||_i}d|vr:t|d\}|_|dkrBt||}ntdt||_dS)Ns z$content does not contain CR-LF-CR-LF)rr r r&lstriprrr%)selfr rr%firstr r r __init__6szBodyPart.__init__cCs|j|jS)z'Content of the ``BodyPart`` in unicode.)r rr)r*r r r textDsz BodyPart.textN)rrr__doc__r,propertyr-r r r r r')s r'c@sDeZdZdZdddZddZeddZd d Ze dd d Z d S)MultipartDecoderai The ``MultipartDecoder`` object parses the multipart payload of a bytestring into a tuple of ``Response``-like ``BodyPart`` objects. The basic usage is:: import requests from requests_toolbelt import MultipartDecoder response = request.get(url) decoder = MultipartDecoder.from_response(response) for part in decoder.parts: print(part.headers['content-type']) If the multipart content is not from a response, basic usage is:: from requests_toolbelt import MultipartDecoder decoder = MultipartDecoder(content, content_type) for part in decoder.parts: print(part.headers['content-type']) For both these usages, there is an optional ``encoding`` parameter. This is a string, which is the name of the unicode codec to use (default is ``'utf-8'``). utf-8cCs*||_||_t|_|||dSr) content_typertupleparts_find_boundary _parse_body)r*r r2rr r r r,gs zMultipartDecoder.__init__cCstdd|jdD}|d}|dddkrFtd||ddD]2}t|d \}}|d krRt|d |j |_ qRdS) Ncss|]}|VqdSr)striprxr r r rrr(z2MultipartDecoder._find_boundary..;r/ multipartz*Unexpected mimetype in content-type: '{0}'r=boundary") r3r2splitlowerrformatr rr7rr>)r*Zct_infomimetypeitemattrvaluer r r r5qs zMultipartDecoder._find_boundarycCs,t|}||d|kr$||dS|SdSr)r)partZboundary_markerZbm_lenr r r _fix_first_parts z MultipartDecoder._fix_first_partcsXddjffdddd|ddf}tfdd |D_dS) Nr(--cst|}t|jSr)r0rHr'r)rGfixed)r>r*r r body_parts z/MultipartDecoder._parse_body..body_partcSs(|dko&|dko&|dddko&|dkS)Nr( s-- rIr )rGr r r test_partsz/MultipartDecoder._parse_body..test_partrLc3s|]}|r|VqdSrr r8)rKrNr r rr(z/MultipartDecoder._parse_body..)joinr>r@r3r4)r*r r4r )rKr>r*rNr r6s zMultipartDecoder._parse_bodycCs |j}|jdd}||||S)Nz content-type)r r%get)clsresponserr r2r r r from_responseszMultipartDecoder.from_responseN)r1)r1) rrrr.r,r5 staticmethodrHr6 classmethodrSr r r r r0Js  r0)r.r email.parserrencoderrZrequests.structuresrr Exceptionrrr&objectr'r0r r r r s    !