o
    ãË·e£  ã                   @   s(   d Z dd„ Zdd„ Zdd„ Zdd„ Zd	S )
znFunctions for generating and parsing HTTP Accept: headers for
supporting server-directed content negotiation.
c                  G   s²   g }| D ]/}t |ƒtu rd}|}n|\}}t|ƒ}|dks!|dkr'td| ƒ‚d|f }| ||f¡ q| ¡  g }|D ]\}}|dkrJ| |¡ q<| d||f ¡ q<d |¡S )zFGenerate an accept header value

    [str or (str, float)] -> str
    z1.0é   é    zInvalid preference factor: %rz%0.1fz%s; q=%sz, )ÚtypeÚstrÚfloatÚ
ValueErrorÚappendÚsortÚjoin)ÚelementsÚpartsÚelementÚqsÚmtypeÚqÚchunks© r   úJ/var/www/ideatree/venv/lib/python3.10/site-packages/openid/yadis/accept.pyÚgenerateAcceptHeader   s$   

r   c              
   C   sØ   dd„ |   d¡D ƒ}g }|D ]N}dd„ |  d¡D ƒ}| d¡}d|vr$q|  dd¡\}}|D ]#}d	|v rQ|  d	d¡\}	}
|	d
krQzt|
ƒ}W  n tyP   Y q.w q.d}| |||f¡ q| ¡  | ¡  dd„ |D ƒS )z¼Parse an accept header, ignoring any accept-extensions

    returns a list of tuples containing main MIME type, MIME subtype,
    and quality markdown.

    str -> [(str, str, float)]
    c                 S   ó   g | ]}|  ¡ ‘qS r   ©Ústrip)Ú.0Úchunkr   r   r   Ú
<listcomp>-   ó    z%parseAcceptHeader.<locals>.<listcomp>ú,c                 S   r   r   r   )r   Úsr   r   r   r   0   r   ú;r   ú/r   ú=r   g      ð?c                 S   s   g | ]
\}}}|||f‘qS r   r   )r   r   ÚmainÚsubr   r   r   r   J   ó    )ÚsplitÚpopr   r   r   r   Úreverse)Úvaluer   Úacceptr   r   r   r    r!   ÚextÚkÚvr   r   r   r   ÚparseAcceptHeader%   s0   
þ€r+   c                 C   sö   | sd}nd}i }i }| D ].\}}}|dkrt ||ƒ}q|dkr,t | |d¡|ƒ||< qt | ||fd¡|ƒ|||f< qg }d}	|D ]-}
|
 d¡\}}||f|v rX|||f }n| ||¡}|ro| d| |	||
f¡ |	d7 }	qB| ¡  dd„ |D ƒS )aª  Given the result of parsing an Accept: header, and the
    available MIME types, return the acceptable types with their
    quality markdowns.

    For example:

    >>> acceptable = parseAcceptHeader('text/html, text/plain; q=0.5')
    >>> matchTypes(acceptable, ['text/plain', 'text/html', 'image/jpeg'])
    [('text/html', 1.0), ('text/plain', 0.5)]


    Type signature: ([(str, str, float)], [str]) -> [(str, float)]
    r   r   Ú*r   c                 S   s   g | ]
\}}}}||f‘qS r   r   )r   Ú_r   r   r   r   r   r   z   r"   zmatchTypes.<locals>.<listcomp>)ÚmaxÚgetr#   r   r   )Úaccept_typesÚ
have_typesÚdefaultÚ
match_mainÚ	match_subr    r!   r   Úaccepted_listÚorder_maintainerr   r   r   r   Ú
matchTypesM   s2   
 €r7   c                 C   s    t | ƒ}t||ƒ}dd„ |D ƒS )a	  Parse the accept header and return a list of available types in
    preferred order. If a type is unacceptable, it will not be in the
    resulting list.

    This is a convenience wrapper around matchTypes and
    parseAcceptHeader.

    (str, [str]) -> [str]
    c                 S   s   g | ]\}}|‘qS r   r   )r   r   r-   r   r   r   r   ‰   r   z!getAcceptable.<locals>.<listcomp>)r+   r7   )Úaccept_headerr1   ÚacceptedÚ	preferredr   r   r   ÚgetAcceptable}   s   

r;   N)Ú__doc__r   r+   r7   r;   r   r   r   r   Ú<module>   s
    (0