o
    ޥi                     @   s  d dl Z d dlZd dlmZ d dlmZmZmZ d dlm	Z	 dedee
 dee fdd	Z	
	ddee
ef dee
ee
 f defddZ				ddee
ef dededededdfddZ					ddee
ef dee
ee
 f dededededefddZdS )    N)Path)IterableOptionalUnion)load_dotenvpath
indicatorsreturnc                 C   sF   |D ]}t | |}t|dkr|   S q| j| krdS t| j|S )ax  Recursively search for files from the `indicators` list, starting from given path.

    Args:
        path (Path): Starting folder path.
        indicators (Iterable[str]): List of filenames to search for.

    Raises:
        FileNotFoundError: If root is not found.

    Returns:
        Optional[Path]: Path to folder containing at list one of the files from the list.
    r   N)listgloblenparent_pyrootutils_recursive_search)r   r   filefound r   K/home/ubuntu/.local/lib/python3.10/site-packages/pyrootutils/pyrootutils.pyr   	   s   
r   .z.project-rootz	setup.cfgzsetup.pyz.gitzpyproject.tomlsearch_from	indicatorc                 C   s   t | ttfstdt|  } t |tr|g}|  s!tdt|dr/tdd |D s3tdt	| |}|r>| sEtd| |S )a  Recursively searches for project root indicator(s), starting from given path.

    Args:
        search_from (str): Path to folder or file to start search from.
        indicator (Union[str, Iterable[str]], optional): List of filenames to search for. Finding at least one on these files indicates the project root.

    Raises:
        TypeError: If any input type is incorrect.
        FileNotFoundError: If root is not found.

    Returns:
        Path: Path to project root.
    z6search_from must be either a string or pathlib object.z search_from path does not exist.__iter__c                 s   s    | ]}t |tV  qd S )N)
isinstancestr).0ir   r   r   	<genexpr>C   s    zfind_root.<locals>.<genexpr>z.indicator must be a string or list of strings.z.Project root directory not found. Indicators: )
r   r   r   	TypeErrorresolveexistsFileNotFoundErrorhasattrallr   )r   r   r   r   r   r   	find_root!   s   

r#   TFproject_root_env_vardotenv
pythonpathcwdc                 C   sn   t | } tj| std|  |r| tjd< |r#ttj| d |r,tj	d|  |r5t
|  dS dS )a  Set given path as a project root.

    Args:
        path (Union[str, Path]): Project root path.
        project_root_env_var (bool, optional): Whether to set PROJECT_ROOT environment variable.
        dotenv (bool, optional): Whether to load `.env` file from project root.
        pythonpath (bool, optional): Whether to add project root to pythonpath.
        cwd (bool, optional): Whether to set current working directory to project root.

    Raises:
        FileNotFoundError: If root path does not exist.

    Returns:
        None
    z"Project root path does not exist: PROJECT_ROOTz.envr   N)r   osr   r   r    environr   joinsysinsertchdirr   r$   r%   r&   r'   r   r   r   set_rootN   s   
r0   c                 C   s    t | |}t|||||d |S )a  Combines `get_root()` and `set_root()` into one method.

    Recursively searches for files from the `indicators` list, starting from given path.

    Args:
        search_from (str): Path to file or folder to start search from.
        indicator (Union[str, Iterable[str]], optional): List of filenames to search for. Finding at least one on these files indicates the project root.
        project_root_env_var (bool, optional): Whether to set PROJECT_ROOT environment variable.
        dotenv (bool, optional): Whether to load `.env` file from project root.
        pythonpath (bool, optional): Whether to add project root to pythonpath.
        cwd (bool, optional): Whether to set current working directory to project root.

    Raises:
        TypeError: If any input type is incorrect.
        FileNotFoundError: If root is not found.

    Returns:
        Path: Path to project root.
    r/   )r#   r0   )r   r   r$   r%   r&   r'   r   r   r   r   
setup_rootv   s   
!r1   )r   r   )TTFF)r   TTFF)r)   r,   pathlibr   typingr   r   r   r%   r   r   r   r#   boolr0   r1   r   r   r   r   <module>   sh    
	
/

*
	
