o
    VhH                     @  s,  d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dl	mZ d d	l	mZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ e
r~d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ e%e&ee&ee&ef f f Z'G dd deZ(dS )    )annotationsN)Mapping)suppressPath)TYPE_CHECKING)Any)ClassVar)Union)option)canonicalize_name)inline_table)Command)
EnvCommand)RequirementsParser)Python)Option)NormalizedName)Package)InlineTableRepositoryPoolc                      s"  e Zd ZU dZdZedddddeddd	dded
ddddedddddeddddddeddddddedddddgZded< dZdP fddZ	dQdd Z
		!	"dRdSd*d+ZdTd1d2Z			dUdVd:d;Z			dUdWd>d?ZdXdAdBZdYdEdFZedZdHdIZed[dKdLZd\dNdOZ  ZS )]InitCommandinitzICreates a basic <comment>pyproject.toml</> file in the current directory.nameNzName of the package.F)flagdescriptionzDescription of the package.authorzAuthor name of the package.pythonzCompatible Python versions.
dependencyzbPackage to require, with an optional version constraint, e.g. requests:^2.10.0 or requests=2.11.1.T)r   multipledev-dependencyzrPackage to require for development, with an optional version constraint, e.g. requests:^2.10.0 or requests=2.11.1.licenselzLicense of the package.zClassVar[list[Option]]optionszdThe <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the current directory.
returnNonec                   s   t    d | _d S N)super__init___pool)self	__class__ p/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/console/commands/init.pyr)   E   s   

zInitCommand.__init__intc                 C  s^   ddl m} | }| jjdr)|| jjd}| r"| s)| d dS | j	|dS )Nr   r   projectz5<error>The --project path is not a directory.</error>   )project_path)
pathlibr   cwdioinputr   existsis_dir
line_error_init_pyproject)r+   r   r3   r.   r.   r/   handleJ   s   
zInitCommand.handlestandardmdr3   r   allow_interactiveboollayout_namestrreadme_formatc                    sn  ddl m} ddlm} ddlm} ddlm} j	 o|}	||d }
|
j
 rB|
 r5d dS |
jd	rBd
 dS | }|	rVd d d d}|st|j }|	rtjd| d|d}|}d}|	rjd| d|d}|}dpd}|s|	rjddd}d  s|dr|d  |d}|r d| d7  |	r؈jd  d d}| fdd |  r݈ gng }d}|s|	rjddd}d }|s| }d!t|jj  }|	rjd"| d|d}|}|	r%d i }d#r8d#}d$}d%}d&}|	r_|d'r_| d'}|g  d i }d(rrd(}d)}|	r|d'r|s| |g  d ||||||r|d nd |||||d*	}|  }|r|j|d&d+ |  }|! D ]\}}|
j"|| q|	rd, d |
j# $d-d. d |	rd/d'sd0 dS |
%  |r5|& }t't( |)t*+ }W d    n	1 s!w   Y  d1|j, d2|-  d3 dS )4Nr   )	GitConfigConfig)layout)PyProjectTOMLzpyproject.tomlz[<error>A pyproject.toml file with a project and/or a poetry section already exists.</error>r2   zbuild-systemzP<error>A pyproject.toml file with a defined build-system already exists.</error> zQThis command will guide you through creating your <info>pyproject.toml</> config.r   zPackage name [<comment>z</comment>]: )defaultz0.1.0zVersion [<comment>r   zDescription []: r   z	user.namez
user.emailz <>zAuthor [<comment>z</comment>, n to skip]: c                   s    |  S r'   )_validate_author)vr   r+   r.   r/   <lambda>   s    z-InitCommand._init_pyproject.<locals>.<lambda>r"   zLicense []: r   z>=z%Compatible Python versions [<comment>r   z>Would you like to define your main dependencies interactively?aN          You can specify a package in the following forms:
          - A single name (<b>requests</b>): this will search for matches on PyPI
          - A name and a constraint (<b>requests@^2.23.0</b>)
          - A git url (<b>git+https://github.com/python-poetry/poetry.git</b>)
          - A git url with a revision         (<b>git+https://github.com/python-poetry/poetry.git#develop</b>)
          - A file path (<b>../my-package/my-package.whl</b>)
          - A directory (<b>../my-package/</b>)
          - A url (<b>https://example.com/packages/my-package-0.1.0.tar.gz</b>)
        FTr!   zEWould you like to define your development dependencies interactively?)r   r   rC   r"   r   dependenciesdev_dependencies)with_pyprojectz<info>Generated file</info>z

zDo you confirm generation?z<error>Command aborted</error>zCreated package <info>z</> in <fg=blue>z</>).poetry.core.vcs.gitrD   poetry.config.configrF   poetry.layoutsrG   poetry.pyproject.tomlrH   r6   is_interactivefiler8   is_poetry_projectr:   datagetliner   r   lowercreate_questionaskset_validatorcreater   get_preferred_pythonminor_version	to_string_format_requirements_determine_requirementsconfirmupdategenerate_project_contentitemsappend	as_stringreplacesaveresolver   
ValueErrorrelative_tor   r5   _package_nameas_posix) r+   r3   r?   rA   rC   rD   rF   rG   rH   rX   	pyproject
vcs_configr   questionversionr   author_emailauthorslicense_namer   configrequirementsquestion_texthelp_messagehelp_displayeddev_requirementslayout_create_layoutcontentsectionitempathr.   rN   r/   r;   W   s  

























zInitCommand._init_pyprojectmatcheslist[Package]canonicalized_namer   	list[str]c                 C  sl   g }dd |D }||v }|r| ||| j |D ]}t|dkr' |S |j|kr-q| |j q|S )Nc                 S  s   g | ]}|j qS r.   )r   ).0pr.   r.   r/   
<listcomp>  s    z5InitCommand._generate_choice_list.<locals>.<listcomp>
   )rl   indexpretty_namelenr   )r+   r   r   choicesmatches_namesexact_matchfound_packager.   r.   r/   _generate_choice_list  s   
z!InitCommand._generate_choice_listrequiresallow_prereleasesbool | Nonesource
str | NonerX   list[dict[str, Any]]c                 C  s  |d u r	| j  }|sg }| d}|| j | d}|| j | |}|r| |gd }	d|	v sBd|	v sBd|	v sBd|	v rV| d| d	 ||	 | |}q(t	|	d
 }
| 
 |
}|sm| d d}n?| ||
}dt| d| d}t|dkr|d7 }| | |d | jd|dt|d d}|s| d |r||	d
< |rd|	vr| d}|d |dd  | |}|d u r| |\}}| d| d| d ||	d< |r||	 |r| |}|s*|S g }| |D ]U}d|v sd|v sd|v r|| qd|vr9| j|d
 ||d\}}||d< ||d
< | d| d| d n| j|d
 |d ||d\}}||d
< || q|S )Nz3Package to add or search for (leave blank to skip):z%
Add a package (leave blank to skip):r   giturlr   rx   zAdding <info>z</info>r   z%<error>Unable to find package</error>FzFound <info>z</info> packages matching <c1>z</c1>r   z
Showing the first 10 matchesrI   zI
Enter package # to add, or the complete package name if it is not listed   r2   )attemptsrJ   z&<warning>No package selected</warning>zSEnter the version constraint to require (or leave blank to use the latest version):c                 S  s   | pd  pd S )NrI   )strip)xr.   r.   r/   rO     s    z5InitCommand._determine_requirements.<locals>.<lambda>zUsing version <b>z</b> for <c1>r   r   )r6   rX   r_   ra   _validate_packager`   _parse_requirementsr]   rl   r   	_get_poolsearchr:   r   r   choiceset_max_attempts_find_best_version_for_package)r+   r   r   r   rX   resultrw   follow_up_questionpackage
constraintr   r   r   info_stringpackage_constraint_requirementr   rx   r.   r.   r/   rg   /  s   












N



z#InitCommand._determine_requirementsrequired_versiontuple[str, str]c           	      C  sZ   ddl m} ||  }|j||||d}|std| |j }|jd|  fS )Nr   )VersionSelectorr   z-Could not find a matching version of package ^)	poetry.version.version_selectorr   r   find_best_candidaterq   rx   without_localr   re   )	r+   r   r   r   r   r   selectorr   rx   r.   r.   r/   r     s   
z*InitCommand._find_best_version_for_packager}   c              	     s   ddl m} z| jjjj}| jjj}W n |tfy'   t	
 }|  j}Y nw t|t| tr2| jnd |d  fdd|D S )Nr   )PyProjectError)artifact_cacheenvr5   c              
     s&   g | ]}  tjd d|tjdqS )z@\s*latest$rI   )flags)parseresubI)r   r   parserr.   r/   r     s    z3InitCommand._parse_requirements.<locals>.<listcomp>) poetry.core.pyproject.exceptionsr   poetryrY   r   parentpoolr   RuntimeErrorr   r5   r   r   
isinstancer   r   )r+   r}   r   r5   r   r.   r   r/   r     s    
zInitCommand._parse_requirementslist[dict[str, str]]Requirementsc                 C  sZ   i }|D ]&}| d}d|v rt|dkr|d }nt }d|j_|| |||< q|S )Nr   rx   r2   rS   )popr   r   triviatrailri   )r+   r}   r   r   r   r   r.   r.   r/   rf     s   



z InitCommand._format_requirementsrJ   c                 C  sJ   ddl m} ddlm} || p|} | dv rd S || }|s#td| S )Nr   )combine_unicode)AUTHOR_REGEX)nnozKInvalid author string. Must be in the format: John Smith <john@example.com>)poetry.core.utils.helpersr   poetry.core.utils.patternsr   matchrq   )r   rJ   r   r   mr.   r.   r/   rL     s   
zInitCommand._validate_authorr   c                 C  s    | rt |  dkrtd| S )N   zInvalid package definition.)r   splitrq   )r   r.   r.   r/   r     s   zInitCommand._validate_packager   c                 C  sj   ddl m} ddlm} ddlm} t| tr| jj	S | j
d u r2| | _
| j}| j
||d | j
S )Nr   rE   r   )PyPiRepository)	pool_size)rU   rF   poetry.repositoriesr   #poetry.repositories.pypi_repositoryr   r   r   r   r   r*   rb   installer_max_workersadd_repository)r+   rF   r   r   r   r.   r.   r/   r   
  s   


zInitCommand._get_pool)r%   r&   )r%   r0   )Tr=   r>   )
r3   r   r?   r@   rA   rB   rC   rB   r%   r0   )r   r   r   r   r%   r   )NNN)
r   r   r   r   r   r   rX   r   r%   r   )
r   rB   r   r   r   r   r   r   r%   r   )r}   r   r%   r   )r}   r   r%   r   )r   rB   rJ   rB   r%   r   )r   r   r%   r   )r%   r   )__name__
__module____qualname__r   r   r   r$   __annotations__helpr)   r<   r;   r   rg   r   r   rf   staticmethodrL   r   r   __classcell__r.   r.   r,   r/   r   "   sb   
 
 
E 

r   ))
__future__r   r   collections.abcr   
contextlibr   r4   r   typingr   r   r	   r
   cleo.helpersr   packaging.utilsr   tomlkitr   poetry.console.commands.commandr   #poetry.console.commands.env_commandr   %poetry.utils.dependency_specificationr   poetry.utils.env.pythonr   cleo.io.inputs.optionr   r   poetry.core.packages.packager   tomlkit.itemsr   r   r   dictrB   r   r   r.   r.   r.   r/   <module>   s0    