o
    hm                     @  s   d dl m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	m
Z
 d dlmZ dZdZd	Zd%ddZd&ddZd'ddZd(ddZd)ddZd*d#d$ZdS )+    )annotationsN)Path)runtime)caching)calc_md5z3\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}z(\d{2}:\d{2}:\d{2}),(\d{3})).srt.vttstreamstr | io.BytesIO | bytesreturnboolc                 C  s   t | tr
t| } t | trt| d} | d | d}z	|d	 }W n
 t
y4   Y dS w |d}t|dkrR|d  rRtt|d }|rRdS dS )	Nutf-8r   !   F
      T)
isinstancebytesioBytesIOstrencodeseekreaddecodestripUnicodeDecodeErrorsplitlenisdigitresearchSRT_VALIDATION_REGEX)r	   header
header_strlinesmatch r'   Y/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/elements/lib/subtitle_utils.py_is_srt$   s"   





r)   srt_datastr | bytesr   c              
   C  s   t | trz| d} W n ty } ztd|d}~ww t | ts-tdt|  dt	t
d| }d| }| d}|S )a  
    Convert subtitles from SubRip (.srt) format to WebVTT (.vtt) format.
    This function accepts the content of the .srt file either as a string
    or as a BytesIO stream.

    Parameters
    ----------
    srt_data : str or bytes
        The content of the .srt file as a string or a bytes stream.

    Returns
    -------
    bytes
        The content converted into .vtt format.
    r   z+Could not decode the input stream as UTF-8.Nz.Input must be a string or a bytes stream, not .z\1.\2zWEBVTT

)r   r   r   r   
ValueErrorr   	TypeErrortyper    subSRT_CONVERSION_REGEXr   r   )r*   evtt_datavtt_contentr'   r'   r(   _srt_to_vttG   s   


r5   data_or_path
str | Pathc                 C  s   t j| rCt| }|j }|tvr td| ddt t	| d}|
 }W d   n1 s4w   Y  |dkrAt|S |S t| trPtd|  d|  }|d	s]|d
krb|dS t|rjt|S td)z:Handles string data, either as a file path or raw content.zIncorrect subtitle format z5. Subtitles must be in one of the following formats: z, rbNr   zFile z does not exist.WEBVTT r   z=The provided string neither matches valid VTT nor SRT format.)ospathisfiler   suffixlowerSUBTITLE_ALLOWED_FORMATSr-   joinopenr   r5   r   r   
startswithr   r)   )r6   r<   file_extensionfilecontentcontent_stringr'   r'   r(   _handle_string_or_path_datap   s*   



rH   
io.BytesIOc                 C  s&   |  d |  }t| rt|S |S )AHandles io.BytesIO data, converting SRT to VTT content if needed.r   )r   getvaluer)   r5   )r	   stream_datar'   r'   r(   _handle_stream_data   s   
rM   datac                 C  s   t | rt| S | S )rJ   )r)   r5   )rN   r'   r'   r(   _handle_bytes_data   s   rO   coordinatesr   str | bytes | Path | io.BytesIOlabelc                 C  s   t |ttfrt|}nt |tjrt|}nt |tr!t|}n
t	dt
| dt rMt| }t jj|d| | dd}t|d|  |S dS )Nz)Invalid binary data format for subtitle: r,   ztext/vttr   )path_or_datamimetyperP   	file_namer:   )r   r   r   rH   r   r   rM   r   rO   r.   r/   r   existsr   r   get_instancemedia_file_mgraddr   save_media_data)rP   rN   rR   subtitle_datafilenamefile_urlr'   r'   r(   process_subtitle_data   s$   




r^   )r	   r
   r   r   )r*   r+   r   r   )r6   r7   r   r   )r	   rI   r   r   )rN   r   r   r   )rP   r   rN   rQ   rR   r   r   r   )
__future__r   r   r;   r    pathlibr   	streamlitr   streamlit.runtimer   streamlit.utilr   r"   r1   r@   r)   r5   rH   rM   rO   r^   r'   r'   r'   r(   <module>   s"   

#
)

