o
    DxMg                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZmZmZmZ ddlmZ d3ddZejd4ddZeed5ddZd6ddZd7ddZd8dd Zd9d#d$Zd:d'd(Zd;d,d-Zd<d1d2Z dS )=z2
Tools for converting old- to new-style metadata.
    )annotationsN)Message)Parser)	GeneratorIterableIteratorLiteral   )Requirementstrreturnbool | Literal['']c                 C  s   | o|  d S )N#)
startswith)r    r   .lib/python3.10/site-packages/wheel/metadata.py	_nonblank   s   r   iterableIterable[str]Iterator[str]c                 C  s   t jtt| S )a  
    Yield valid lines of a string or iterable.
    >>> list(yield_lines(''))
    []
    >>> list(yield_lines(['foo', 'bar']))
    ['foo', 'bar']
    >>> list(yield_lines('foo\nbar'))
    ['foo', 'bar']
    >>> list(yield_lines('\nfoo\n#bar\nbaz #comment'))
    ['foo', 'baz #comment']
    >>> list(yield_lines(['foo\nbar', 'baz', 'bing\n\n\n']))
    ['foo', 'bar', 'baz', 'bing']
    )	itertoolschainfrom_iterablemapyield_lines)r   r   r   r   r      s   r   textc                 C  s   t tttj|  S N)filterr   r   r   strip
splitlines)r   r   r   r   _)   s   r    sstr | Iterator[str]3Generator[tuple[str | None, list[str]], None, None]c                 c  sv    d}g }t | D ]*}|dr.|dr)|s|r||fV  |dd  }g }q	td||| q	||fV  dS )ar  Split a string or iterable thereof into (section, content) pairs
    Each ``section`` is a stripped version of the section header ("[section]")
    and each ``content`` is a list of stripped lines excluding blank lines and
    comment-only lines.  If there are any such lines before the first section
    header, they're returned in a first ``section`` of ``None``.
    N[]r	   zInvalid section heading)r   r   endswithr   
ValueErrorappend)r!   sectionZcontentliner   r   r   split_sections.   s   	



r,   extrac                 C  s   t dd|  S )zConvert an arbitrary string to a standard 'extra' name
    Any runs of non-alphanumeric characters are replaced with a single '_',
    and the result is always lowercased.
    z[^A-Za-z0-9.-]+r    )resublower)r-   r   r   r   
safe_extraI   s   r1   namec                 C  s   t dd| S )zConvert an arbitrary string to a standard distribution name
    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
    z[^A-Za-z0-9.]+-)r.   r/   )r2   r   r   r   	safe_nameQ   s   r4   requirementr
   c                 C  sL   | j rd| j  S g }| jD ]}||j|j  q|r$ddt| S dS )zFReturn the version specifier for a requirement in PEP 345/566 fashion.z @  , )urlZ	specifierr)   operatorversionjoinsorted)r5   Zrequires_distspecr   r   r   requires_to_requires_distX   s   

r?   requirements	list[str]c                 c  s^    | D ])}t |}t|}dtdd |jD }|r"d| d}t|j| | V  qdS )z=Yield Requires-Dist: strings for parsed requirements strings.r7   c                 s  s    | ]}t |V  qd S r   )r1   ).0er   r   r   	<genexpr>l   s    z'convert_requirements.<locals>.<genexpr>r$   r%   N)r
   r?   r<   r=   extrasr4   r2   )r@   ZreqZparsed_requirementr>   rE   r   r   r   convert_requirementsg   s   rF   extras_requiredict[str | None, list[str]]Iterator[tuple[str, str]]c                 c  s    |   D ]K\}}d}|pd}d|v r|dd\}}t|}|r6d|fV  |r.d| d }|d| d7 }|r<d	| }t|D ]}tt|| }d
|fV  q@qdS )a,  
    Convert requirements from a setup()-style dictionary to
    ('Requires-Dist', 'requirement') and ('Provides-Extra', 'extra') tuples.

    extras_require is a dictionary of {extra: [requirements]} as passed to setup(),
    using the empty extra {'': [requirements]} to hold install_requires.
    r8   :r	   Provides-Extra(z) and z
extra == ''z ; Requires-DistN)itemssplitr1   rF   r   r
   )rG   r-   ZdependsZ	conditionZnew_reqZcanonical_reqr   r   r   generate_requirementss   s&   

rQ   egg_info_pathpkginfo_pathr   c              
   C  sF  t |dd}t |}W d   n1 sw   Y  |dd |d= |d= tj| d}tj|rwt |dd}| }W d   n1 sJw   Y  t	t
|d	d
 d}|D ]\}}	t||	iD ]\}
}|
|f| vru|||
< qeq[|d }|r|d  }d|d  td|dd df}|| |d= |S )zN
    Convert .egg-info directory with PKG-INFO to the Metadata 2.1 format
    zutf-8)encodingNzMetadata-Versionz2.1rK   rN   zrequires.txtc                 S  s   | d pdS )Nr   r8   r   )xr   r   r   <lambda>   s    z%pkginfo_to_metadata.<locals>.<lambda>)keyZDescription
r   r	   )openr   parseZreplace_headerospathr<   existsreadr=   r,   rQ   rO   r   lstriptextwrapdedentZset_payload)rR   rS   ZheadersZpkg_infoZrequires_pathZrequires_fileZrequiresZparsed_requirementsr-   ZreqsrW   valuedescriptionZdescription_linesZdedented_descriptionr   r   r   pkginfo_to_metadata   s<   


	rd   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r!   r"   r   r#   )r-   r   r   r   )r2   r   r   r   )r5   r
   r   r   )r@   rA   r   r   )rG   rH   r   rI   )rR   r   rS   r   r   r   )!__doc__Z
__future__r   	functoolsr   Zos.pathr[   r.   r`   Zemail.messager   Zemail.parserr   typingr   r   r   r   Zvendored.packaging.requirementsr
   r   singledispatchr   registerr   r    r,   r1   r4   r?   rF   rQ   rd   r   r   r   r   <module>   s.    






