o
    poi                  
   @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZ G dd deZd	eeee f d
ee fddZddededed
ee fddZdS )    N)IterableIterator)LooseVersion)Path)AnyOptionalUnion)Requirementyield_linesc                       sT   e Zd ZdZddddededee ded	df
 fd
dZded	efddZ  Z	S )_RequirementWithCommentz# strict Ncommentpip_argumentargsr   r   kwargsreturnc                   sL   t  j|i | || _|d u s|std| || _| j| v | _d S )Nzwrong pip argument: )super__init__r   RuntimeErrorr   strict_stringlowerstrict)selfr   r   r   r   	__class__ \/home/ubuntu/.local/lib/python3.10/site-packages/lightning_utilities/install/requirements.pyr      s   z _RequirementWithComment.__init__unfreezec                 C   s   t | }| jr| d| j S |dkr<| jD ]#\}}|dv r9t|jd }|| | dt|d  d  S q|S |dkr[| jD ]\}}|dv rX|| | d	d
  S qC|S |dkrgt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 r"|d}|d| ||d }}nd}|drE|dd  }z|t|7 }W n tyD   Y  dS w |drM|}q	|drSq	d|v s]td	|r^q	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   )r2   linesr   linecomment_posr   r   r   r   _parse_requirementsB   s2   



rA   base.txtr"   path_dir	file_namer   c                    s^    dvrt d  dt| | }| s td| ||f | } fddt|D S )a8  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>{   s    z%load_requirements.<locals>.<listcomp>)r*   r   existsFileNotFoundError	read_textrA   )rC   rD   r   pathtextr   rH   r   load_requirementsk   s   
rO   )rB   r"   )r<   collections.abcr   r   distutils.versionr   pathlibr   typingr   r   r   pkg_resourcesr	   r
   r   r%   rA   listrO   r   r   r   r   <module>   s   "5$)