
    h                     `    d Z ddlmZ ddlmZ dZdZdZdZdZ	d	 Z
d
 Zd Zd Zd Zd Zd ZdS )au  MIME-Type Parser

This module provides basic functions for handling mime-types. It can handle
matching mime-types against a list of media-ranges. See section 14.1 of the
HTTP specification [RFC 2616] for a complete explanation.

   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

Contents:
 - parse_mime_type():   Parses a mime-type into its component parts.
 - parse_media_range(): Media-ranges are mime-types with wild-cards and a 'q'
                          quality parameter.
 - quality():           Determines the quality ('q') of a mime-type when
                          compared against a list of media-ranges.
 - quality_parsed():    Just like quality() except the second parameter must be
                          pre-parsed.
 - best_match():        Choose the mime-type with the highest quality ('q')
                          from a list of candidates.
    )absolute_import)reducez0.1.3zJoe Gregoriozjoe@bitworking.orgzMIT License c                 4   |                      d          }t          d |dd         D                       }|d                                         }|dk    rd}|                     d          \  }}|                                |                                |fS )	aR  Parses a mime-type into its component parts.

    Carves up a mime-type and returns a tuple of the (type, subtype, params)
    where 'params' is a dictionary of all the parameters for the media range.
    For example, the media range 'application/xhtml;q=0.5' would get parsed
    into:

       ('application', 'xhtml', {'q', '0.5'})
    ;c           	      h    g | ]/}t          d  |                    dd          D                       0S )c                 6    g | ]}|                                 S  strip).0ss     /home/visionen/pythonvenv/lib/python3.11/site-packages/google_api_python_client-2.181.0-py3.11.egg/googleapiclient/mimeparse.py
<listcomp>z.parse_mime_type.<locals>.<listcomp>.<listcomp>/   s     777a		777    =   )tuplesplit)r   params     r   r   z#parse_mime_type.<locals>.<listcomp>/   s=    PPPU775;;sA#6#6777	8	8PPPr   r   Nr   *z*/*/)r   dictr   )	mime_typepartsparams	full_typetypesubtypes         r   parse_mime_typer    #   s     OOC  EPPeABBiPPP F a  I C	ooc**OT7JJLL'--//622r   c                     t          |           \  }}}d|vsO|d         rGt          |d                   r2t          |d                   dk    st          |d                   dk     rd|d<   |||fS )a  Parse a media-range into its component parts.

    Carves up a media range and returns a tuple of the (type, subtype,
    params) where 'params' is a dictionary of all the parameters for the media
    range.  For example, the media range 'application/*;q=0.5' would get parsed
    into:

       ('application', '*', {'q', '0.5'})

    In addition this function also guarantees that there is a value for 'q'
    in the params dictionary, filling it in with a proper default if
    necessary.
    qr   r   1)r    float)ranger   r   r   s       r   parse_media_ranger&   ;   s     .e44T7F6c{ 	VC[!! 	 !!!!s'6""r   c                 n   d}d}t          |           \  }}}|D ]\  }}||k    p|dk    p|dk    }	||k    p|dk    p|dk    }
|	r^|
r\t          d fd|                                D             d          }||k    rdpd}|||k    rdpdz  }||z  }||k    r
|}d         }|t          |          fS )	a  Find the best match for a mime-type amongst parsed media-ranges.

    Find the best match for a given mime-type against a list of media_ranges
    that have already been parsed by parse_media_range(). Returns a tuple of
    the fitness value and the value of the 'q' quality parameter of the best
    match, or (-1, 0) if no match was found. Just as for quality_parsed(),
    'parsed_ranges' must be a list of parsed media ranges.
    r   r   c                     | |z   S Nr
   )xys     r   <lambda>z,fitness_and_quality_parsed.<locals>.<lambda>i   s
    QU r   c                 F    g | ]\  }}|d k    |v ||         k    dS )r"   r   r
   )r   keyvaluer   s      r   r   z.fitness_and_quality_parsed.<locals>.<listcomp>j   sF       $eczzcVmm8L8L 8L8L8Lr   d   
   r"   )r&   r   itemsr$   )r   parsed_rangesbest_fitness
best_fit_qtarget_typetarget_subtypetarget_paramsr   r   
type_matchsubtype_matchparam_matchesfitnessr   s                @r   fitness_and_quality_parsedr>   V   s9    LJ3DY3O3O0[.-#0 ) )w[(MDCKM;#;M
~%PCP>S;P 	  	)- 	)"""   (5(;(;(=(=  
  M {*38qG>19r>Q>G}$G%%&#C[
z****r   c                 .    t          | |          d         S )a  Find the best match for a mime-type amongst parsed media-ranges.

    Find the best match for a given mime-type against a list of media_ranges
    that have already been parsed by parse_media_range(). Returns the 'q'
    quality parameter of the best match, 0 if no match was found. This function
    bahaves the same as quality() except that 'parsed_ranges' must be a list of
    parsed media ranges.
    r   )r>   )r   r4   s     r   quality_parsedr@   {   s     &i??BBr   c                 `    d |                     d          D             }t          | |          S )aK  Return the quality ('q') of a mime-type against a list of media-ranges.

    Returns the quality 'q' of a mime-type when compared against the
    media-ranges in ranges. For example:

    >>> quality('text/html','text/*;q=0.3, text/html;q=0.7,
                  text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5')
    0.7

    c                 ,    g | ]}t          |          S r
   r&   r   rs     r   r   zquality.<locals>.<listcomp>   s!    EEEa&q))EEEr   ,)r   r@   )r   rangesr4   s      r   qualityrH      s4     FE6<<3D3DEEEM)]333r   c                 6   t          |                    d                    }d |D             }g }d}| D ]-}|                    t          ||          ||f           |dz  }.|                                 |d         d         d         r|d         d         pdS )aY  Return mime-type with the highest quality ('q') from list of candidates.

    Takes a list of supported mime-types and finds the best match for all the
    media-ranges listed in header. The value of header must be a string that
    conforms to the format of the HTTP Accept: header. The value of 'supported'
    is a list of mime-types. The list of supported mime-types should be sorted
    in order of increasing desirability, in case of a situation where there is
    a tie.

    >>> best_match(['application/xbel+xml', 'text/xml'],
                   'text/*;q=0.5,*/*; q=0.1')
    'text/xml'
    rF   c                 ,    g | ]}t          |          S r
   rC   rD   s     r   r   zbest_match.<locals>.<listcomp>   s!    @@@a&q))@@@r   r   r   r(      r   )_filter_blankr   appendr>   sort)	supportedheadersplit_headerparsed_headerweighted_matchesposr   s          r   
best_matchrU      s     !c!2!233L@@<@@@M
C  	'	=AA3	R	
 	
 	
 	qB"1%A*:2*>q*AGRGr   c              #   D   K   | D ]}|                                 r|V  d S r*   r   )ir   s     r   rL   rL      s9        7799 	GGG r   N)__doc__
__future__r   	functoolsr   __version__
__author__	__email____license____credits__r    r&   r>   r@   rH   rU   rL   r
   r   r   <module>r`      s   
 & ' & & & & &      
 	3 3 30# # #6"+ "+ "+J
C 
C 
C4 4 4 H H H8    r   