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rG 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&dIdJdKdLdMdNdOO | 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

"

