o
    i'                     @   sx  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 e
eZedZedZddd fd	d
d fddd ffZdZdZdZdZejG dd dZdedefddZdedefddZ			d,de	eeddf ee f deded edeeddf f
d!d"Zd#eddfd$d%Z				d-de	eeddf ee f d&eded'e	edf dedefd(d)ZG d*d+ d+eZ dS ).a  A tiny library for composing SRT files.

Based on https://github.com/cdown/srt with parsing, subtitle modifying
functionality and Python 2 support removed. This is because of
https://github.com/rany2/edge-tts/issues/383.

Typing support was added, and more Python 3 features were used.

Copyright (c) 2014-2023 Christopher Down
Copyright (c) 2025- rany <rany@riseup.net>

This file is licensed under the MIT License (MIT).
See the LICENSE-MIT file for details.
    N)	timedelta)	GeneratorListUnionz\n\n+z
No contentc                 C   s   | j   S N)contentstripsub r   O/home/kim/smarthome/.venv/lib/python3.10/site-packages/edge_tts/srt_composer.py<lambda>       r   zStart time < 0 secondsc                 C   s
   | j tk S r   )startZERO_TIMEDELTAr	   r   r   r   r      s   
 zSubtitle start time >= end timec                 C   s   | j | jkS r   )r   endr	   r   r   r   r       r   i  <      i  c                
   @   s   e Zd ZdZdeedf dedededdf
dd	Zdefd
dZ	de
defddZde
defddZdefddZddeedf defddZdS )Subtitlea  
    The metadata relating to a single subtitle. Subtitles are sorted by start
    time by default. If no index was provided, index 0 will be used on writing
    an SRT block.

    :param index: The SRT index for this subtitle
    :type index: int or None
    :param start: The time that the subtitle should start being shown
    :type start: :py:class:`datetime.timedelta`
    :param end: The time that the subtitle should stop being shown
    :type end: :py:class:`datetime.timedelta`
    :param str content: The subtitle content. Should not contain OS-specific
                        line separators, only \\n. This is taken care of
                        already if you use :py:func:`srt.parse` to generate
                        Subtitle objects.
    indexNr   r   r   returnc                 C   s   || _ || _|| _|| _d S r   )r   r   r   r   )selfr   r   r   r   r   r   r   __init__=   s   
zSubtitle.__init__c                 C   s   t tt|  S r   )hash	frozensetvarsitems)r   r   r   r   __hash__E   s   zSubtitle.__hash__otherc                 C   s   t |tstS t| t|kS r   )
isinstancer   NotImplementedr   r   r   r   r   r   __eq__H   s   
zSubtitle.__eq__c                 C   s.   t |tstS | j| j| jf|j|j|jfk S r   )r   r   r    r   r   r   r!   r   r   r   __lt__N   s   
zSubtitle.__lt__c                 C   sF   t t| dt t| d}ddd | D }t| j d| dS )N	iteritemsr   z, c                 s   s"    | ]\}}| d |V  qdS )=Nr   ).0kvr   r   r   	<genexpr>[   s     z$Subtitle.__repr__.<locals>.<genexpr>())getattrr   jointype__name__)r   Z	var_itemsZ	item_listr   r   r   __repr__X   s   zSubtitle.__repr__eolc                 C   sV   t | j}|du rd}n
|dkr|d|}d}|j| jpdt| jt| j||dS )a%  
        Convert the current :py:class:`Subtitle` to an SRT block.

        :param str eol: The end of line string to use (default "\\n")
        :returns: The metadata of the current :py:class:`Subtitle` object as an
                  SRT formatted subtitle block
        :rtype: str
        N
z3{idx}{eol}{start} --> {end}{eol}{content}{eol}{eol}r   )idxr   r   r   r1   )make_legal_contentr   replaceformatr   timedelta_to_srt_timestampr   r   )r   r1   Zoutput_contenttemplater   r   r   to_srt^   s   
	zSubtitle.to_srtr   )r/   
__module____qualname____doc__r   intr   strr   r   objectboolr"   r#   r0   r9   r   r   r   r   r   )   s$    


 r   r   r   c                 C   s@   | r| d dkrd| vr| S t d| d}td| | |S )aL  
    Remove illegal content from a content block. Illegal content includes:

    * Blank lines
    * Starting or ending with a blank line

    .. doctest::

        >>> make_legal_content('\nfoo\n\nbar\n')
        'foo\nbar'

    :param str content: The content to make legal
    :returns: The legalised content
    :rtype: srt
    r   r2   z

zLegalised content %r to %r)MULTI_WS_REGEXr
   r   LOGinfo)r   Zlegal_contentr   r   r   r4   x   s
   r4   timedelta_timestampc                 C   sh   t | jt\}}|| jt 7 }t |t\}}| jt }t|ddt|ddt|ddt|dS )a  
    Convert a :py:class:`~datetime.timedelta` to an SRT timestamp.

    .. doctest::

        >>> import datetime
        >>> delta = datetime.timedelta(hours=1, minutes=23, seconds=4)
        >>> timedelta_to_srt_timestamp(delta)
        '01:23:04,000'

    :param datetime.timedelta timedelta_timestamp: A datetime to convert to an
                                                   SRT timestamp
    :returns: The timestamp in SRT format
    :rtype: str
    Z02:,Z03)	divmodsecondsSECONDS_IN_HOURdaysHOURS_IN_DAYSECONDS_IN_MINUTEmicrosecondsMICROSECONDS_IN_MILLISECONDr=   )rD   ZhrsZsecs_remainderZminsZsecsmsecsr   r   r   r7      s
   
2r7      FT	subtitlesstart_indexin_placeskipc                 c   s    d}t t| |dD ]J\}}|stdi t|}|rMzt| W n* tyL } z|jdu r6td| ntd|j| |d7 }W Y d}~qd}~ww || |_|V  qdS )a  
    Reorder subtitles to be sorted by start time order, and rewrite the indexes
    to be in that same order. This ensures that the SRT file will play in an
    expected fashion after, for example, times were changed in some subtitles
    and they may need to be resorted.

    If skip=True, subtitles will also be skipped if they are considered not to
    be useful. Currently, the conditions to be considered "not useful" are as
    follows:

    - Content is empty, or only whitespace
    - The start time is negative
    - The start time is equal to or later than the end time

    .. doctest::

        >>> from datetime import timedelta
        >>> one = timedelta(seconds=1)
        >>> two = timedelta(seconds=2)
        >>> three = timedelta(seconds=3)
        >>> subs = [
        ...     Subtitle(index=999, start=one, end=two, content='1'),
        ...     Subtitle(index=0, start=two, end=three, content='2'),
        ... ]
        >>> list(sort_and_reindex(subs))  # doctest: +ELLIPSIS
        [Subtitle(...index=1...), Subtitle(...index=2...)]

    :param subtitles: :py:class:`Subtitle` objects in any order
    :param int start_index: The index to start from
    :param bool in_place: Whether to modify subs in-place for performance
                          (version <=1.0.0 behaviour)
    :param bool skip: Whether to skip subtitles considered not useful (see
                      above for rules)
    :returns: The sorted subtitles
    :rtype: :term:`generator` of :py:class:`Subtitle` objects
    r   )r   Nz"Skipped subtitle with no index: %sz Skipped subtitle at index %d: %srP   r   )		enumeratesortedr   r   _should_skip_sub_ShouldSkipExceptionr   rB   rC   )rQ   rR   rS   rT   Zskipped_subsZsub_numsubtitleZ
thrown_excr   r   r   sort_and_reindex   s*   *


rZ   rY   c                 C   s"   t D ]\}}|| rt|qdS )z
    Check if a subtitle should be skipped based on the rules in
    SUBTITLE_SKIP_CONDITIONS.

    :param subtitle: A :py:class:`Subtitle` to check whether to skip
    :raises _ShouldSkipException: If the subtitle should be skipped
    N)SUBTITLE_SKIP_CONDITIONSrX   )rY   Zinfo_msgZsub_skipperr   r   r   rW      s
   rW   reindexr1   c                    s*   |r	t | ||d} d fdd| D S )a  
    Convert an iterator of :py:class:`Subtitle` objects to a string of joined
    SRT blocks.

    .. doctest::

        >>> from datetime import timedelta
        >>> start = timedelta(seconds=1)
        >>> end = timedelta(seconds=2)
        >>> subs = [
        ...     Subtitle(index=1, start=start, end=end, content='x'),
        ...     Subtitle(index=2, start=start, end=end, content='y'),
        ... ]
        >>> compose(subs)  # doctest: +ELLIPSIS
        '1\n00:00:01,000 --> 00:00:02,000\nx\n\n2\n00:00:01,000 --> ...'

    :param subtitles: The subtitles to convert to SRT blocks
    :type subtitles: :term:`iterator` of :py:class:`Subtitle` objects
    :param bool reindex: Whether to reindex subtitles based on start time
    :param int start_index: If reindexing, the index to start reindexing from
    :param str eol: The end of line string to use (default "\\n")
    :returns: A single SRT formatted string, with each input
              :py:class:`Subtitle` represented as an SRT block
    :param bool in_place: Whether to reindex subs in-place for performance
                          (version <=1.0.0 behaviour)
    :rtype: str
    )rR   rS    c                 3   s    | ]	}|j  d V  qdS )r1   N)r9   )r&   rY   r^   r   r   r)      s    zcompose.<locals>.<genexpr>)rZ   r-   )rQ   r\   rR   r1   rS   r   r^   r   compose   s
   "r_   c                   @   s   e Zd ZdZdS )rX   z3
    Raised when a subtitle should be skipped.
    N)r/   r:   r;   r<   r   r   r   r   rX   #  s    rX   )rP   FT)TrP   NF)!r<   	functoolsloggingredatetimer   typingr   r   r   	getLoggerr/   rB   compilerA   r   r[   rI   rL   rK   rN   total_orderingr   r>   r4   r7   r=   r@   rZ   rW   r_   	ExceptionrX   r   r   r   r   <module>   sl    




N
A

*