
    ^j
                        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	Z	 G d dej                        Z	 d	 	 	 	 	 ddZdddZdd	Zdd
Z	 d	 	 	 	 	 	 	 ddZddZddZy)    )annotationsN)Path)Anyc                        e Zd Zd fdZ xZS )NumpyJsonEncoderc                
   t        |t        j                        rt        |      S t        |t        j                        rt        |      S t        |t        j                        r|j                         S t        | %  |      S N)

isinstancenpintegerintfloatingfloatndarraytolistsuperdefault)selfobj	__class__s     a/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/utils/file.pyr   zNumpyJsonEncoder.default   s[    c2::&s8Oc2;;':c2::&::<ws##    )r   r   returnr   )__name__
__module____qualname__r   __classcell__)r   s   @r   r   r      s    $ $r   r   c           	        t        |       } g }|| j                  d      D cg c]  }|j                         s| }}i }|D ]o  }|j                  D cg c]!  }|j	                         j                  d      # }}t        t        |            D 	ch c]  }	dj                  ||	d        c}	||<   q t               }
|D ]h  }|j	                         j                  d      }|s%|j                         D ]1  \  }}||
vs||v s|j                  |       |
j                  |       3 j |S |j                  | j                  d             |S c c}w c c}w c c}	w )a  
    List files in a directory with specified extensions or
        all files if no extensions are provided.

    Args:
        directory: The directory path as a string or Path object.
        extensions: A list of file extensions to filter. Extensions may be
            supplied with or without a leading dot (e.g. ``'jpg'`` and
            ``'.jpg'`` are equivalent). Matching is case-insensitive.
            Multi-part extensions are supported (e.g. ``'tar.gz'``). Pass
            ``None`` (default) to list all files; pass an empty list to
            return no files.

    Returns:
        A list of Path objects for the matching files.

    Examples:
        ```pycon
        >>> import supervision as sv
        >>> from pathlib import Path
        >>> import tempfile
        >>> # Keep a reference to the directory object
        >>> tmp_dir_obj = tempfile.TemporaryDirectory()
        >>> tmpdir = tmp_dir_obj.name
        >>> # Create test files
        >>> (Path(tmpdir) / "test1.txt").touch()
        >>> (Path(tmpdir) / "test2.md").touch()
        >>> (Path(tmpdir) / "test3.py").touch()
        >>> # List all files in the directory
        >>> files = sv.list_files_with_extensions(directory=tmpdir)
        >>> len(files)
        3
        >>> # Leading dot accepted; matching is case-insensitive
        >>> files = sv.list_files_with_extensions(
        ...     directory=tmpdir, extensions=['.txt', 'md'])
        >>> len(files)
        2

        ```
    N*.)r   globis_filesuffixeslowerlstriprangelenjoinsetitemsappendaddextend)	directory
extensionsfiles_with_extensionsp
candidates
path_indexpathsuffixr#   index
seen_pathsextnormalized_extensionpath_extensionss                 r   list_files_with_extensionsr;      sa   X YI(*!*!4DA		aD
D+-
 	DAEOv--c2OHO8=c(m8L /4%&)* Jt	
 !$
 	)C#&99;#5#5c#: ')3)9)9); )%oz).Bo.U)006NN4()		) !  	$$Y^^C%89  ' E P s   EE&EEc                   t        t        |             5 }|r0|D cg c]$  }|j                         s|j                  d      & }}n|D cg c]  }|j                  d       }}ddd       |S c c}w c c}w # 1 sw Y   S xY w)a  
    Read a text file and return a list of strings without newline characters.
    Optionally skip empty lines.

    Args:
        file_path: The file path as a string or Path object.
        skip_empty: If True, skip lines that are empty or contain only
            whitespace. Default is False.

    Returns:
        A list of strings representing the lines in the text file.

    Examples:
        ```pycon
        >>> import tempfile
        >>> from pathlib import Path
        >>> from supervision.utils.file import read_txt_file, save_text_file
        >>> with tempfile.TemporaryDirectory() as tmpdir:
        ...     file_path = Path(tmpdir) / "test.txt"
        ...     save_text_file(["line1", " ", "line3"], file_path)
        ...     print(read_txt_file(file_path))
        ...     print(read_txt_file(file_path, skip_empty=True))
        ['line1', ' ', 'line3']
        ['line1', 'line3']

        ```
    
N)openstrstriprstrip)	file_path
skip_emptyfilelineliness        r   read_txt_filerG   \   s|    8 
c)n	 937H44::<T[[&HEH3784T[[&8E8	9 L	 I8	9 Ls-   A:A0A0A:A5$A:0
A::Bc                    t        t        |      d      5 }| D ]  }|j                  |dz           	 ddd       y# 1 sw Y   yxY w)z
    Write a list of strings to a text file, each string on a new line.

    Args:
        lines: The list of strings to be written to the file.
        file_path: The file path as a string or Path object.
    wr=   N)r>   r?   write)rF   rB   rD   rE   s       r   save_text_filerK      sH     
c)nc	" $d 	$DJJtd{#	$$ $ $s	   <Ac                    t        t        |             5 }t        j                  |      }ddd       |S # 1 sw Y   S xY w)a  
    Read a json file and return a dict.

    Args:
        file_path: The file path as a string or Path object.

    Returns:
        A dict of annotations information

    Examples:
        ```pycon
        >>> import tempfile
        >>> from pathlib import Path
        >>> from supervision.utils.file import read_json_file, save_json_file
        >>> data = {"key": "value", "list": [1, 2, 3]}
        >>> with tempfile.TemporaryDirectory() as tmpdir:
        ...     file_path = Path(tmpdir) / "test.json"
        ...     save_json_file(data, file_path)
        ...     print(read_json_file(file_path))
        {'key': 'value', 'list': [1, 2, 3]}

        ```
    N)r>   r?   jsonloadrB   rD   datas      r   read_json_filerQ      s9    0 
c)n	 yyKK   5?c                    t        t        |      d      5 }t        j                  | |t        |       ddd       y# 1 sw Y   yxY w)z
    Write a dict to a json file.

    Args:
        data: dict with unique keys and value as pair.
        file_path: The file path as a string or Path object.
        indent:
    rI   )clsindentN)r>   r?   rM   dumpr   )rP   rB   rU   fps       r   save_json_filerX      s@     
c)nc	" Ab		$ 0@A A As	   =Ac                    t        t        |             5 }t        j                  |      }ddd       |S # 1 sw Y   S xY w)z
    Read a yaml file and return a dict.

    Args:
        file_path: The file path as a string or Path object.

    Returns:
        A dict of content information
    N)r>   r?   yaml	safe_loadrO   s      r   read_yaml_filer\      s:     
c)n	 $~~d#$K$KrR   c                    t        t        |      d      5 }t        j                  | |dd       ddd       y# 1 sw Y   yxY w)z
    Save a dict to a yaml file.

    Args:
        data: dict with unique keys and value as pair.
        file_path: The file path as a string or Path object.
    rI   FN)	sort_keysdefault_flow_style)r>   r?   rZ   rV   )rP   rB   outfiles      r   save_yaml_filera      s?     
c)nc	" Kg		$5TJK K Ks	   9Ar	   )r.   
str | Pathr/   zlist[str] | Noner   z
list[Path])F)rB   rb   rC   boolr   	list[str])rF   rd   rB   rb   r   None)rB   rb   r   dict[str, Any])   )rP   rf   rB   rb   rU   r   r   re   )rP   rf   rB   rb   r   re   )
__future__r   rM   pathlibr   typingr   numpyr   rZ   JSONEncoderr   r;   rG   rK   rQ   rX   r\   ra    r   r   <module>rn      s    "     $t'' $ ;?C!C!'7C!C!L"J
$< @AA
A%/A9<A	A
Kr   