U
    42i~                     @  s  d dl mZ d dlZd dlmZ d dlZd dlmZ 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dddZdddddZddddddZddddddZdddd d!Zd"Zd#d$d#dd%d&d'd(Zdd)d*d+d,ZdS )-    )annotationsN)Path)Iterable)Sequence   )
UsageError)failabsolutepath)
commonpathsafe_existsr   ziniconfig.IniConfigpathreturnc              
   C  sH   zt t| W S  t jk
rB } ztt||W 5 d}~X Y nX dS )zParse the given generic '.ini' file using legacy IniConfig parser, returning
    the parsed object.

    Raise UsageError if the file cannot be parsed.
    N)	iniconfigZ	IniConfigstr
ParseErrorr   )r   exc r   </tmp/pip-unpacked-wheel-7n2p7kht/_pytest/config/findpaths.py_parse_ini_config   s    r   z!dict[str, str | list[str]] | None)filepathr   c              
     sV  | j dkr<t| }d|kr*t|d  S | jdkr8i S n| j dkrt| }d|jkrht|d  S d|jkrttjdddd	 n| j d
krRt	j
dkrddl}nddl}| jdd}z||}W n: |jk
r } zt|  d| |W 5 d}~X Y nX |di di dd}|dk	rRddddd  fdd| D S dS )zLoad pytest configuration from the given file path, if supported.

    Return None if the file does not contain valid pytest configuration.
    z.iniZpytest
pytest.iniz.cfgztool:pytest	setup.cfg)filenameF)Zpytracez.toml)      r   Nzutf-8)encodingz: ZtoolZini_optionsobjectzstr | list[str])vr   c                 S  s   t | tr| S t| S N)
isinstancelistr   )r    r   r   r   make_scalarM   s    z/load_config_dict_from_file.<locals>.make_scalarc                   s   i | ]\}}| |qS r   r   ).0kr    r$   r   r   
<dictcomp>P   s      z.load_config_dict_from_file.<locals>.<dictcomp>)suffixr   dictitemsnamesectionsr   CFG_PYTEST_SECTIONformatsysversion_infotomllibZtomli	read_textloadsZTOMLDecodeErrorr   get)r   r   r2   Z	toml_textconfigr   resultr   r'   r   load_config_dict_from_file   s4    






&
r8   zIterable[Path]z;tuple[Path | None, Path | None, dict[str, str | list[str]]])invocation_dirargsr   c           
      C  s   dddddg}dd |D }|s&| g}d}|D ]t}t |}|f|jD ]Z}|D ]P}|| }| rN|jdkrx|dkrx|}t|}	|	dk	rN|||	f      S qNqFq.|dk	r|j|i fS ddi fS )	zxSearch in the list of arguments for a valid ini-file for pytest,
    and return a tuple of (rootdir, inifile, cfg-dict).r   z.pytest.inizpyproject.tomlztox.inir   c                 S  s   g | ]}t |d s|qS )-)r   
startswith)r%   xr   r   r   
<listcomp>b   s      z!locate_config.<locals>.<listcomp>N)r
   parentsis_filer,   r8   parent)
r9   r:   Zconfig_namesZfound_pyproject_tomlargZargpathbaseZconfig_namepZ
ini_configr   r   r   locate_configU   s0    rE   )r9   pathsr   c                 C  s   d }|D ]X}|  sq|d kr$|}q||jks||kr:qq||jkrJ|}qt||}|d k	r|}q|d krp| }n| r~|j}|S r!   )existsr?   r   r@   rA   )r9   rF   Zcommon_ancestorr   Zsharedr   r   r   get_common_ancestorv   s$    

rH   zIterable[str]z
list[Path])r:   r   c                   sV   ddddddddddddd	d
d fdd| D } fdd|D S )Nr   bool)r=   r   c                 S  s
   |  dS )Nr;   )r<   r=   r   r   r   	is_option   s    z%get_dirs_from_args.<locals>.is_optionc                 S  s   |  dd S )Nz::r   )splitrJ   r   r   r   get_file_part_from_node_id   s    z6get_dirs_from_args.<locals>.get_file_part_from_node_idr   r   c                 S  s   |   r| S | jS r!   )is_dirrA   )r   r   r   r   get_dir_from_path   s    z-get_dirs_from_args.<locals>.get_dir_from_pathc                 3  s"   | ]}|st  |V  qd S r!   r	   )r%   rB   )rM   rK   r   r   	<genexpr>   s   z%get_dirs_from_args.<locals>.<genexpr>c                   s   g | ]}t |r |qS r   r   )r%   r   )rO   r   r   r>      s      z&get_dirs_from_args.<locals>.<listcomp>r   )r:   Zpossible_pathsr   )rO   rM   rK   r   get_dirs_from_args   s    rQ   z][pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead.z
str | NonezSequence[str]z4tuple[Path, Path | None, dict[str, str | list[str]]])inifiler:   rootdir_cmd_argr9   r   c                 C  s  d}t |}| r8t| }|}t|p&i }|dkr|j}nt||}	t||	g\}}}|dkr|dkr|	f|	jD ]}
|
d  rp|
} qqp||	gkrt||\}}}|dkrt|||	g}t|r|	}|rtt	j
|}| std| d|dk	st|||p
i fS )a  Determine the rootdir, inifile and ini configuration values from the
    command line arguments.

    :param inifile:
        The `--inifile` command line argument, if given.
    :param args:
        The free command line arguments.
    :param rootdir_cmd_arg:
        The `--rootdir` command line argument, if given.
    :param invocation_dir:
        The working directory when pytest was invoked.
    Nzsetup.pyzDirectory 'z+' not found. Check your '--rootdir' option.)rQ   r
   r8   rA   rH   rE   r?   r@   
is_fs_rootosr   
expandvarsrN   r   AssertionError)rR   r:   rS   r9   rootdirdirsZinipath_ZinipathZinicfgZancestorZpossible_rootdirr   r   r   determine_setup   s@    

 
rZ   rI   )rD   r   c                 C  s   t jt| d t jkS )z
    Return True if the given path is pointing to the root of the
    file system ("/" on Unix and "C:\\" on Windows for example).
    r   )rU   r   
splitdriver   sep)rD   r   r   r   rT      s    rT   )
__future__r   rU   pathlibr   r0   typingr   r   r   
exceptionsr   Z_pytest.outcomesr   Z_pytest.pathlibr
   r   r   r   r8   rE   rH   rQ   r.   rZ   rT   r   r   r   r   <module>   s&   7!6