a
    dz                     @   s   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	m
Z
mZ d dlmZmZ G dd deZeeee f ee dd	d
Zdeeee	e dddZdS )    N)LooseVersion)Path)AnyIterableIteratorListOptionalUnion)Requirementyield_linesc                       sJ   e Zd ZdZdddeeee edd fddZeedd	d
Z  Z	S )_RequirementWithCommentz# strict Ncommentpip_argument)argsr   r   kwargsreturnc                   sL   t  j|i | || _|d u s2|s2td| || _| j| v | _d S )Nzwrong pip argument: )super__init__r   RuntimeErrorr   strict_stringlowerstrict)selfr   r   r   r   	__class__ q/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_utilities/install/requirements.pyr      s    z _RequirementWithComment.__init__)unfreezer   c                 C   s   t | }| jr| d| j S |dkrv| jD ]F\}}|dv r,t|jd }|| | dt|d  d  S q,nT|dkr| jD ]*\}}|dv r|| | d	d
  S qn|dkrtd|d|S )a;  Remove version restrictions unless they are strict.

        >>> _RequirementWithComment("arrow<=1.2.2,>=1.2.0", comment="# anything").adjust("none")
        'arrow<=1.2.2,>=1.2.0'
        >>> _RequirementWithComment("arrow<=1.2.2,>=1.2.0", comment="# strict").adjust("none")
        'arrow<=1.2.2,>=1.2.0  # strict'
        >>> _RequirementWithComment("arrow<=1.2.2,>=1.2.0", comment="# my name").adjust("all")
        'arrow>=1.2.0'
        >>> _RequirementWithComment("arrow>=1.2.0, <=1.2.2", comment="# strict").adjust("all")
        'arrow<=1.2.2,>=1.2.0  # strict'
        >>> _RequirementWithComment("arrow").adjust("all")
        'arrow'
        >>> _RequirementWithComment("arrow>=1.2.0, <=1.2.2", comment="# cool").adjust("major")
        'arrow<2.0,>=1.2.0'
        >>> _RequirementWithComment("arrow>=1.2.0, <=1.2.2", comment="# strict").adjust("major")
        'arrow<=1.2.2,>=1.2.0  # strict'
        >>> _RequirementWithComment("arrow>=1.2.0").adjust("major")
        'arrow>=1.2.0'
        >>> _RequirementWithComment("arrow").adjust("major")
        'arrow'
        z  major)<z<=r   r!      z.0all,r   nonezUnexpected unfreeze: z value.)	strr   r   specsr   versionreplaceint
ValueError)r   r   outoperatorr(   r    r   r   r   adjust   s    ,z_RequirementWithComment.adjust)
__name__
__module____qualname__r   r   r&   r   r   r.   __classcell__r   r   r   r   r      s   &r   )strsr   c              	   c   s   t | }d}|D ]}d|v rB|d}|d| ||d  }}nd}|dr|dd  }z|t|7 }W n ty   Y  dS 0 |dr|}q|drqd|v std	|rqt	|||d
V  d}qdS )a  Adapted from `pkg_resources.parse_requirements` to include comments.

    >>> txt = ['# ignored', '', 'this # is an', '--piparg', 'example', 'foo # strict', 'thing', '-r different/file.txt']
    >>> [r.adjust('none') for r in _parse_requirements(txt)]
    ['this', 'example', 'foo  # strict', 'thing']
    >>> txt = '\\n'.join(txt)
    >>> [r.adjust('none') for r in _parse_requirements(txt)]
    ['this', 'example', 'foo  # strict', 'thing']
    Nz #r   \z--z-r @z	https?://r   )
r   findendswithstripnextStopIteration
startswithresearchr   )r3   linesr   lineZcomment_posr   r   r   r   _parse_requirements@   s,    





rA   base.txtr#   )path_dir	file_namer   r   c                    s^    dvrt d  dt| | }| s@td| ||f | } fddt|D S )a7  Load requirements from a file.

    >>> import os
    >>> from lightning_utilities import _PROJECT_ROOT
    >>> path_req = os.path.join(_PROJECT_ROOT, "requirements")
    >>> load_requirements(path_req, "docs.txt", unfreeze="major")  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    ['sphinx<6.0,>=4.0', ...]
    >   r#   r%   r    zunsupported option of ""zmissing file for c                    s   g | ]}|  qS r   )r.   ).0reqr   r   r   
<listcomp>w       z%load_requirements.<locals>.<listcomp>)r+   r   existsFileNotFoundError	read_textrA   )rC   rD   r   pathtextr   rH   r   load_requirementsh   s    	rP   )rB   r#   )r=   Zdistutils.versionr   pathlibr   typingr   r   r   r   r   r	   pkg_resourcesr
   r   r   r&   rA   rP   r   r   r   r   <module>   s    4 (