o
    e)                  	   @   s  d dl Z d dlmZ d dlmZmZmZ d dlZd dl	Z	G dd de j
Z	ddeeef deee  dee fd	d
Zddededee fddZdee defddZdedefddZd dedededdfddZdedefddZdededdfddZdS )!    N)Path)ListOptionalUnionc                       s   e Zd Z fddZ  ZS )NumpyJsonEncoderc                    sL   t |tjr
t|S t |tjrt|S t |tjr| S tt	| 
|S N)
isinstancenpintegerintfloatingfloatndarraytolistsuperr   default)selfobj	__class__ E/var/www/myenv/lib/python3.10/site-packages/supervision/utils/file.pyr   
   s   zNumpyJsonEncoder.default)__name__
__module____qualname__r   __classcell__r   r   r   r   r   	   s    r   	directory
extensionsreturnc                 C   sL   t | } g }|dur|D ]}|| d|  q|S || d |S )a+  
    List files in a directory with specified extensions or
        all files if no extensions are provided.

    Args:
        directory (Union[str, Path]): The directory path as a string or Path object.
        extensions (Optional[List[str]]): A list of file extensions to filter.
            Default is None, which lists all files.

    Returns:
        (List[Path]): A list of Path objects for the matching files.

    Examples:
        ```python
        >>> import supervision as sv

        >>> # List all files in the directory
        >>> files = sv.list_files_with_extensions(directory='my_directory')

        >>> # List only files with '.txt' and '.md' extensions
        >>> files = sv.list_files_with_extensions(
        ...     directory='my_directory', extensions=['txt', 'md'])
        ```
    Nz*.*)r   extendglob)r   r   files_with_extensionsextr   r   r   list_files_with_extensions   s   r$   F	file_path
skip_emptyc                 C   sb   t | d"}|rdd |D }ndd |D }W d   |S W d   |S 1 s*w   Y  |S )a  
    Read a text file and return a list of strings without newline characters.
    Optionally skip empty lines.

    Args:
        file_path (str): The path to the text file.
        skip_empty (bool): If True, skip lines that are empty or contain only
            whitespace. Default is False.

    Returns:
        List[str]: A list of strings representing the lines in the text file.
    rc                 S   s   g | ]}|  r|d qS 
)striprstrip.0liner   r   r   
<listcomp>K   s    z!read_txt_file.<locals>.<listcomp>c                 S   s   g | ]}| d qS r(   )r+   r,   r   r   r   r/   M   s    N)open)r%   r&   filelinesr   r   r   read_txt_file<   s   

r3   r2   c                 C   sH   t |d}| D ]	}||d  qW d   dS 1 sw   Y  dS )z
    Write a list of strings to a text file, each string on a new line.

    Args:
        lines (List[str]): The list of strings to be written to the file.
        file_path (str): The path to the text file.
    wr)   N)r0   write)r2   r%   r1   r.   r   r   r   save_text_fileR   s
   "r6   c                 C   :   t | d}t|}W d   |S 1 sw   Y  |S )z
    Read a json file and return a dict.

    Args:
        file_path (str): The path to the json file.

    Returns:
        dict: A dict of annotations information
    r'   N)r0   jsonloadr%   r1   datar   r   r   read_json_file_      

r<      r;   indentc                 C   sB   t |d}tj| |t|d W d   dS 1 sw   Y  dS )z
    Write a dict to a json file.

    Args:
        indent:
        data (dict): dict with unique keys and value as pair.
        file_path (str): The path to the json file.
    r4   )clsr?   N)r0   r8   dumpr   )r;   r%   r?   fpr   r   r   save_json_filen   s   	"rC   c                 C   r7   )z
    Read a yaml file and return a dict.

    Args:
        file_path (str): The path to the yaml file.

    Returns:
        dict: A dict of content information
    r'   N)r0   yaml	safe_loadr:   r   r   r   read_yaml_file{   r=   rF   c                 C   sB   t |d}tj| |ddd W d   dS 1 sw   Y  dS )z
    Save a dict to a json file.

    Args:
        indent:
        data (dict): dict with unique keys and value as pair.
        file_path (str): The path to the json file.
    r4   FN)	sort_keysdefault_flow_style)r0   rD   rA   )r;   r%   outfiler   r   r   save_yaml_file   s   
"rJ   r   )F)r>   )r8   pathlibr   typingr   r   r   numpyr	   rD   JSONEncoderr   strr$   boolr3   r6   dictr<   r   rC   rF   rJ   r   r   r   r   <module>   s(    


(