U
    Mh                     @   sT   d dl mZmZ ddlmZmZ dgZG dd dZdeee dded	d
dZ	dS )    )DictList   )	GlobGroupGlobPattern	Directoryc                   @   sr   e Zd ZdZeedddZee d dddZedd	d
Z	eedddZ
dd Zdee eedddZdS )r   zA file structure representation. Organized as Directory nodes that have lists of
    their Directory children. Directories for a package are created by calling
    :meth:`PackageImporter.file_structure`.)nameis_dirc                 C   s   || _ || _i | _d S )N)r   r	   children)selfr   r	    r   ]/var/www/html/venv/lib/python3.8/site-packages/torch/package/file_structure_representation.py__init__   s    zDirectory.__init__)dirsreturnc                 C   sJ   t |dkr| S |d }|| jkr2t|d| j|< | j| |dd S )a$  Builds path of Directories if not yet built and returns last directory
        in list.

        Args:
            dirs (List[str]): List of directory names that are treated like a path.

        Returns:
            :class:`Directory`: The last Directory specified in the dirs list.
        r   Tr   N)lenr
   r   _get_dir)r   r   dir_namer   r   r   r      s    

zDirectory._get_dir)	file_pathc                 C   s.   | d^ }}| |}t|d|j|< dS )zAdds a file to a Directory.

        Args:
            file_path (str): Path of file to add. Last element is added as a file while
                other paths items are added as directories.
        /FN)splitr   r   r
   )r   r   r   filedirr   r   r   	_add_file$   s    
zDirectory._add_file)filenamer   c                 C   s\   |j ddd}|d }t|dkr*|d nd}|| j krX|dkrHdS | j| |S dS )zChecks if a file is present in a :class:`Directory`.

        Args:
            filename (str): Path of file to search for.
        Returns:
            bool: If a :class:`Directory` contains the specified file.
        r   r   )maxsplitr   NTF)r   r   r
   keyshas_file)r   r   ZlineagechildZgrandchildrenr   r   r   r   /   s    zDirectory.has_filec                 C   s   g }|  | d|S )N )_stringify_treejoin)r   str_listr   r   r   __str__A   s    
zDirectory.__str__r   
   ─── )r"   preambledir_ptrc                 C   s$  d}d}d}d}| | | | j d ||kr<|| }n|| }g }g }	| j D ]$\}
}|jrp|	 |
 qV| |
 qVtt|	D ]N\}}
|t|	d krt|dkr| j|
 ||| q| j|
 ||| qtt|D ]:\}}|t|d kr|n|}| | | | d qdS )	zCRecursive method to generate print-friendly version of a Directory.z    u   │   u
   ├── u
   └── 
r   r   N)	appendr   r
   itemsr	   	enumeratesortedr   r    )r   r"   r%   r&   spacebranchteelastZ	file_keysZdir_keyskeyvalindexr   Zpointerr   r   r   r    F   s*    
zDirectory._stringify_treeN)r   r$   )__name__
__module____qualname____doc__strboolr   r   r   r   r   r#   r    r   r   r   r   r   	   s     **r   r   )r   	file_listincludeexcluder   c                 C   s:   t ||dd}t| d}|D ]}||r|| q|S )a  Return a :class:`Directory` file structure representation created from a list of files.

    Args:
        filename (str): The name given to the top-level directory that will be the
            relative root for all file paths found in the file_list.

        file_list (List[str]): List of files to add to the top-level directory.

        include (Union[List[str], str]): An optional pattern that limits what is included from the file_list to
            files whose name matches the pattern.

        exclude (Union[List[str], str]): An optional pattern that excludes files whose name match the pattern.

    Returns:
            :class:`Directory`: a :class:`Directory` file structure representation created from a list of files.
    r   )r<   	separatorT)r   r   matchesr   )r   r:   r;   r<   Zglob_patternZtop_dirr   r   r   r    _create_directory_from_file_listm   s    

r?   N)r9   r   )
typingr   r   Z
glob_groupr   r   __all__r   r7   r?   r   r   r   r   <module>   s   g  