U
    5d                  	   @   s   d Z ddlZddl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	d
ddgZejdedZejedddZeeeeedd  eeeeedd dZeeZW 5 Q R X edddd	ZeZdd Zdd ZeddddddddZdd Z dS )z(Utility functions for dealing with files    N)Path)resource_filename   )ParameterError)deprecate_positional_args
find_filesexampleexlist_examplesexample_infoZLIBROSA_DATA_DIRZlibrosazhttps://librosa.org/data/audio/)base_urlregistryZexample_datazregistry.txtz
index.jsonrF)hqc                C   s:   | t krtd| |r d}nd}tt |  d | S )aQ  Retrieve the example recording identified by 'key'.

    The first time an example is requested, it will be downloaded from
    the remote repository over HTTPS.
    All subsequent requests will use a locally cached copy of the recording.

    For a list of examples (and their keys), see `librosa.util.list_examples`.

    By default, local files will be cached in the directory given by
    `pooch.os_cache('librosa')`.  You can override this by setting
    an environment variable ``LIBROSA_DATA_DIR`` prior to importing librosa:

    >>> import os
    >>> os.environ['LIBROSA_DATA_DIR'] = '/path/to/store/data'
    >>> import librosa

    Parameters
    ----------
    key : str
        The identifier for the track to load
    hq : bool
        If ``True``, return the high-quality version of the recording.
        If ``False``, return the 22KHz mono version of the recording.

    Returns
    -------
    path : str
        The path to the requested example file

    Examples
    --------
    Load "Hungarian Dance #5" by Johannes Brahms

    >>> y, sr = librosa.load(librosa.example('brahms'))

    Load "Vibe Ace" by Kevin MacLeod (the example previously packaged with librosa)
    in high-quality mode

    >>> y, sr = librosa.load(librosa.example('vibeace', hq=True))

    See Also
    --------
    librosa.util.list_examples
    pooch.os_cache
    Unknown example key: {}z.hq.oggz.oggpath)
__TRACKMAPr   format	__GOODBOYfetch)keyr   ext r   6/tmp/pip-unpacked-wheel-8l90aumz/librosa/util/files.pyr   *   s    0c                  C   s>   t d t d tt D ]} t d| t|  d  qdS )a=  List the available audio recordings included with librosa.

    Each recording is given a unique identifier (e.g., "brahms" or "nutcracker"),
    listed in the first column of the output.

    A brief description is provided in the second column.

    See Also
    --------
    util.example
    util.example_info
    zAVAILABLE EXAMPLESD--------------------------------------------------------------------z{:10}	{}descN)printsortedr   keysr   )r   r   r   r   r
   i   s    c              	   C   sx   | t krtd| tt |  d d }t|d8}td| t |  d  td |D ]}t| q\W 5 Q R X dS )	a  Display licensing and metadata information for the given example recording.

    The first time an example is requested, it will be downloaded from
    the remote repository over HTTPS.
    All subsequent requests will use a locally cached copy of the recording.

    For a list of examples (and their keys), see `librosa.util.list_examples`.

    By default, local files will be cached in the directory given by
    `pooch.os_cache('librosa')`.  You can override this by setting
    an environment variable ``LIBROSA_DATA_DIR`` prior to importing librosa.

    Parameters
    ----------
    key : str
        The identifier for the recording (see `list_examples`)

    See Also
    --------
    librosa.util.example
    librosa.util.list_examples
    pooch.os_cache
    r   r   z.txtr   z{:10s}	{:s}r   r   N)r   r   r   r   r   openr   )r   licensefdescliner   r   r   r   |   s    T)r   recursecase_sensitivelimitoffsetc                C   s   |dkrdddddddg}nt |tr,|g}t|}|s`td	d
 |D }|tdd
 |D O }t }|rt| D ]}|t|d |O }qtn
t| |}t|}|  ||d }|dk	r|d| }|S )a@  Get a sorted list of (audio) files in a directory or directory sub-tree.

    Examples
    --------
    >>> # Get all audio files in a directory sub-tree
    >>> files = librosa.util.find_files('~/Music')

    >>> # Look only within a specific directory, not the sub-tree
    >>> files = librosa.util.find_files('~/Music', recurse=False)

    >>> # Only look for mp3 files
    >>> files = librosa.util.find_files('~/Music', ext='mp3')

    >>> # Or just mp3 and ogg
    >>> files = librosa.util.find_files('~/Music', ext=['mp3', 'ogg'])

    >>> # Only get the first 10 files
    >>> files = librosa.util.find_files('~/Music', limit=10)

    >>> # Or last 10 files
    >>> files = librosa.util.find_files('~/Music', offset=-10)

    >>> # Avoid including search patterns in the path string
    >>> import glob
    >>> directory = '~/[202206] Music'
    >>> directory = glob.escape(directory)  # Escape the special characters
    >>> files = librosa.util.find_files(directory)

    Parameters
    ----------
    directory : str
        Path to look for files

    ext : str or list of str
        A file extension or list of file extensions to include in the search.

        Default: ``['aac', 'au', 'flac', 'm4a', 'mp3', 'ogg', 'wav']``

    recurse : boolean
        If ``True``, then all subfolders of ``directory`` will be searched.

        Otherwise, only ``directory`` will be searched.

    case_sensitive : boolean
        If ``False``, files matching upper-case version of
        extensions will be included.

    limit : int > 0 or None
        Return at most ``limit`` files. If ``None``, all files are returned.

    offset : int
        Return files starting at ``offset`` within the list.

        Use negative values to offset from the end of the list.

    Returns
    -------
    files : list of str
        The list of audio files.
    NZaacauZflacZm4aZmp3ZoggZwavc                 S   s   g | ]}|  qS r   )lower.0er   r   r   
<listcomp>   s     zfind_files.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )upperr)   r   r   r   r,      s     r   )
isinstancestrsetoswalk__get_fileslistsort)	directoryr   r#   r$   r%   r&   filesr2   r   r   r   r      s&    A

c                 C   sT   t jt j| } t }|D ]0}t j| dt jj | }|tt|O }q|S )z2Helper function to get files in a single directory*)r1   r   abspath
expanduserr0   joinextsepglob)Zdir_name
extensionsZmyfilesZsub_extZglobstrr   r   r   r3     s    r3   )!__doc__r1   r=   jsonpathlibr   pkg_resourcesr   Zpooch
exceptionsr   Z
decoratorsr   __all__environgetZos_cacheZ__data_pathcreater   Zload_registry__name__r/   r   r!   loadr   r   r	   r
   r   r   r3   r   r   r   r   <module>   sT   
   :%    a