U
    Â½„bV+  ã                   @   sú   d dl mZ d dlmZmZmZmZ d dlmZ d dl	Z	d dl
Z
d dlZdd„ Zdd„ Zed	d
ƒZdej_dd„ Zdd„ Zdd„ Zer˜G dd„ deƒZnejZdd„ Zd*dd„Zd+dd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd,d#d$„Zd-d&d'„Zd(d)„ ZdS ).é    )Ú
exceptions)Ústring_typesÚ	text_typeÚurlparseÚ_TemporaryFileWrapper)Ú
namedtupleNc                 C   sF   |dks|dkrdS t   | j¡j}| d¡r>| |dd… ¡S ||kS )zx
    Domain string matching against an outgoing request.
    Patterns starting with '*' indicate a wildcard domain.
    NÚ*Té   )r   ÚurlÚhostnameÚ
startswithÚendswith)ÚrequestÚdomainÚhost© r   ú1/tmp/pip-unpacked-wheel-07qh9vnj/coreapi/utils.pyÚdomain_matches	   s    
r   c                  C   s$   dd„ t jdd�D ƒ} dd„ | D ƒS )Nc                 S   s   g | ]}||  ¡ f‘qS r   )Úload)Ú.0Úpackager   r   r   Ú
<listcomp>   s    z(get_installed_codecs.<locals>.<listcomp>zcoreapi.codecs)Úgroupc                 S   s   i | ]\}}|j |ƒ “qS r   )Úname)r   r   Úclsr   r   r   Ú
<dictcomp>   s     z(get_installed_codecs.<locals>.<dictcomp>)Úpkg_resourcesÚiter_entry_points)Úpackagesr   r   r   Úget_installed_codecs   s    
þÿr   ÚFilezname content content_type©Nc                 C   s2   t | tƒrdS t| dƒr.t | ttttfƒs.dS dS )NTÚ__iter__F)Ú
isinstancer    Úhasattrr   ÚlistÚtupleÚdict)Úobjr   r   r   Úis_file'   s
    
r)   c                 C   sB   t | dd ƒ}|r>t|tƒr>|d dkr>|d dkr>tj |¡S d S )Nr   r   ú<éÿÿÿÿú>)Úgetattrr#   r   ÚosÚpathÚbasename)r(   r   r   r   r   Úguess_filename2   s    &r1   c              P   C   s¬   ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHd&dIdJdKdLdMdNdOœO  | dP¡S )Qzà
    Python's `mimetypes.guess_extension` is no use because it simply returns
    the first of an unordered set. We use the same set of media types here,
    but take a reasonable preference on what extension to map to.
    z.jsz.docz.binz.odaz.pdfz.p7cz.psz.m3uz.xlsz.pptz.bcpioz.cpioz.cshz.dviz.gtarz.hdfz.latexz.mifz.ncz.p12z.ramz.pycz.shz.sharz.swfz.sv4cpioz.sv4crcz.tarz.tclz.texz.texinfoz.trz.manz.mez.msz.ustarz.srcz.xmlz.zipz.auz.mp3z.aifz.raz.wavz.gifz.iefz.jpez.pngz.svgz.tiffz.icoz.rasz.bmpz.pnmz.pbmz.pgmz.ppmz.rgbz.xbmz.xpmz.xwdz.emlz.cssz.csvz.htmlz.txtz.rtxz.tsvz.pyz.etxz.sgmlz.vcfz.mp4z.mpegz.movz.webmz.aviz.movie)Ozapplication/javascriptzapplication/mswordúapplication/octet-streamzapplication/odazapplication/pdfzapplication/pkcs7-mimezapplication/postscriptzapplication/vnd.apple.mpegurlzapplication/vnd.ms-excelzapplication/vnd.ms-powerpointzapplication/x-bcpiozapplication/x-cpiozapplication/x-cshzapplication/x-dvizapplication/x-gtarzapplication/x-hdfzapplication/x-latexzapplication/x-mifzapplication/x-netcdfzapplication/x-pkcs12zapplication/x-pn-realaudiozapplication/x-python-codezapplication/x-shzapplication/x-sharzapplication/x-shockwave-flashzapplication/x-sv4cpiozapplication/x-sv4crczapplication/x-tarzapplication/x-tclzapplication/x-texzapplication/x-texinfozapplication/x-troffzapplication/x-troff-manzapplication/x-troff-mezapplication/x-troff-mszapplication/x-ustarzapplication/x-wais-sourcezapplication/xmlzapplication/zipzaudio/basicz
audio/mpegzaudio/x-aiffzaudio/x-pn-realaudiozaudio/x-wavz	image/gifz	image/iefz
image/jpegz	image/pngzimage/svg+xmlz
image/tiffzimage/vnd.microsoft.iconzimage/x-cmu-rasterzimage/x-ms-bmpzimage/x-portable-anymapzimage/x-portable-bitmapzimage/x-portable-graymapzimage/x-portable-pixmapzimage/x-rgbzimage/x-xbitmapzimage/x-xpixmapzimage/x-xwindowdumpzmessage/rfc822ztext/cssztext/csvz	text/htmlz
text/plainztext/richtextztext/tab-separated-valuesztext/x-pythonztext/x-setextztext/x-sgmlztext/x-vcardztext/xmlz	video/mp4z
video/mpegzvideo/quicktimez
video/webmzvideo/x-msvideozvideo/x-sgi-movieÚ )Úget)Úcontent_typer   r   r   Úguess_extension9   s¦    ±P °r6   c                   @   s   e Zd ZdZdd„ ZdS )ÚDownloadedFileNc                 C   s4   | j r
dnd}| j rdn
d| jj }d| j||f S )NÚclosedÚopenr3   z '%s'z<DownloadedFile '%s', %s%s>c                 S   s   |   ¡ S r!   )Ú__repr__)Úselfr   r   r   Ú__str__œ   s    z(DownloadedFile.__repr__.<locals>.__str__)r8   ÚfileÚmoder   )r;   Ústater>   r<   r   r   r   r:   —   s    zDownloadedFile.__repr__)Ú__name__Ú
__module__Ú__qualname__r0   r:   r   r   r   r   r7   ”   s   r7   c                 C   sl   t   |¡}|j ¡ }|j}|s,t d| ¡‚|s>t d| ¡‚| D ]}||jkrB|  S qBt d| ¡‚dS )zC
    Given a URL determine the appropriate transport instance.
    zURL missing scheme '%s'.zURL missing hostname '%s'.zUnsupported URL scheme '%s'.N)r   ÚschemeÚlowerÚnetlocr   ÚNetworkErrorÚschemes)Z
transportsr
   Zurl_componentsrC   rE   Ú	transportr   r   r   Údetermine_transport¨   s    



rI   c                 C   s„   |dkr| d S |  d¡d  ¡  ¡ }|  d¡d d }d}| D ],}| ¡ D ]}||||fkrL|    S qLq@d| }t |¡‚dS )zx
    Given the value of a 'Content-Type' header, return the appropriate
    codec for decoding the request content.
    Nr   ú;ú/ú/*ú*/*z-Unsupported media in Content-Type header '%s')ÚsplitÚstriprD   Úget_media_typesr   ÚNoCodecAvailable)Zdecodersr5   Z	main_typeZwildcard_typeÚcodecÚ
media_typeÚmsgr   r   r   Únegotiate_decoder½   s    rU   c                 C   s¶   |dkr| d S t dd„ | d¡D ƒƒ}| D ]&}| ¡ D ]}||kr8|    S q8q,| D ]6}| ¡ D ](}|j d¡d d |krd|    S qdqXd|kr | d S d	| }t |¡‚dS )
zs
    Given the value of a 'Accept' header, return the appropriate codec for
    encoding the response content.
    Nr   c                 S   s"   g | ]}|  d ¡d  ¡  ¡ ‘qS )rJ   r   )rN   rO   rD   ©r   Úitemr   r   r   r   Ú   s   ÿz%negotiate_encoder.<locals>.<listcomp>ú,rK   rL   rM   z'Unsupported media in Accept header '%s')ÚsetrN   rP   rS   r   rQ   )ÚencodersÚacceptZ
acceptablerR   rS   rT   r   r   r   Únegotiate_encoderÒ   s"    þr\   c                 C   s"   t | dd�} | sd}t |¡‚| S )NF)Ú
allow_listzParameter %s: May not be empty.)Ú_validate_form_fieldr   ÚParameterError©ÚvaluerT   r   r   r   Úvalidate_path_paramô   s
    
rb   c                 C   s   t | ƒS r!   ©r^   )ra   r   r   r   Úvalidate_query_paramü   s    rd   c                 C   sl   |dkrt | ƒS |dkr$t| dd�S |dkr4t| ƒS |dkrVt| ƒsRd}t |¡‚| S d}t || ¡‚d S )	Núapplication/jsonúmultipart/form-dataT©Úallow_filesú!application/x-www-form-urlencodedr2   zMust be an file upload.ú/Unsupported encoding "%s" for outgoing request.)Ú_validate_json_dataÚ_validate_form_objectr)   r   r_   rF   ©ra   ÚencodingrT   r   r   r   Úvalidate_body_param   s    
ro   c                 C   sJ   |dkrt | ƒS |dkr$t| dd�S |dkr4t| ƒS d}t || ¡‚d S )Nre   rf   Trg   ri   rj   )rk   r^   r   rF   rm   r   r   r   Úvalidate_form_param  s    rp   Fc                    s.   t | tƒsd}t |¡‚‡ fdd„|  ¡ D ƒS )zQ
    Ensure that `value` can be encoded as form data or as query parameters.
    zMust be an object.c                    s"   i | ]\}}t |ƒt|ˆ d �“qS )rg   )r   r^   ©r   Zitem_keyZitem_valrg   r   r   r   "  s   ÿ z)_validate_form_object.<locals>.<dictcomp>)r#   r'   r   r_   Úitems)ra   rh   rT   r   rg   r   rl     s    


þrl   Tc                 C   s�   t | tƒr| S t | tƒs | dkr0ddddœ|  S t | ttfƒrFd|  S |rnt | ttfƒrnt| ƒsndd„ | D ƒS |r~t| ƒr~| S d	}t 	|¡‚dS )
zÈ
    Ensure that `value` can be encoded as a single form data or a query parameter.
    Basic types that has a simple string representation are supported.
    A list of basic types is also valid.
    NÚtrueÚfalser3   )TFNz%sc                 S   s   g | ]}t |d d d�‘qS )F)rh   r]   rc   rV   r   r   r   r   6  s   ÿz(_validate_form_field.<locals>.<listcomp>zMust be a primitive type.)
r#   r   ÚboolÚintÚfloatr%   r&   r)   r   r_   )ra   rh   r]   rT   r   r   r   r^   (  s    
þr^   c                 C   sp   | dkst | ttttfƒr| S t | ttfƒrBt| ƒsBdd„ | D ƒS t | tƒr^dd„ |  	¡ D ƒS d}t
 |¡‚dS )z7
    Ensure that `value` can be encoded into JSON.
    Nc                 S   s   g | ]}t |ƒ‘qS r   )rk   rV   r   r   r   r   H  s     z'_validate_json_data.<locals>.<listcomp>c                 S   s   i | ]\}}t |ƒt|ƒ“qS r   )r   rk   rq   r   r   r   r   J  s   ÿ z'_validate_json_data.<locals>.<dictcomp>zMust be a JSON primitive.)r#   ru   rv   rw   r   r%   r&   r)   r'   rr   r   r_   r`   r   r   r   rk   A  s    
þrk   )N)N)F)FT) Zcoreapir   Zcoreapi.compatr   r   r   r   Úcollectionsr   r.   r   Útempfiler   r   r    Ú__new__Ú__defaults__r)   r1   r6   r7   ÚNamedTemporaryFilerI   rU   r\   rb   rd   ro   rp   rl   r^   rk   r   r   r   r   Ú<module>   s2   
Y

"

