
    hR                         d dl mZ d dlZd dlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ  ej        d          Z G d de          ZdS )    )partialN)OptionalTextIOAnyMatch   )UnknownFPSError)SSAEvent)SSAStyle   )
FormatBase)
parse_tags)ms_to_framesframes_to_ms)SSAFilez" *\{ *(\d+) *\} *\{ *(\d+) *\}(.+)c                       e Zd ZdZededee         fd            Ze	 	 dddd	ed
edee	         de
deddfd            Ze	 	 dddd	ed
edee	         de
de
deddfd            ZdS )MicroDVDFormatz'MicroDVD subtitle format implementationtextreturnc                 |    t          t          t          j        |                                                    rdS dS )z5See :meth:`pysubs2.formats.FormatBase.guess_format()`microdvdN)anymapMICRODVD_LINEmatch
splitlines)clsr   s     X/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/pysubs2/formats/microdvd.pyguess_formatzMicroDVDFormat.guess_format   s5     s=&(9(9::;; 	:4    NTsubsr   fpformat_fpsstrict_fps_inferencekwargsc                 0   |D ]}t                               |          }|s |                                \  }	}
}t          t          |	|
f          \  }	}
|R	 |r|	dk    r|
dk    st          d          t          |          }||_        # t
          $ r t          d          w xY wt          t          t          |          |	|
f          \  }}dt          dt          fd}t          || ||          	          }|                    |           dS )
a  
        See :meth:`pysubs2.formats.FormatBase.from_file()`

        Keyword args:
            strict_fps_inference: If True (default), in the case when ``fps`` is not given, it will be read
                from the first subtitle text only if the start and end frame of this subtitle is ``{1}{1}``
                (matches VLC Player behaviour), otherwise :class:`pysubs2.exceptions.UnknownFPSError` is raised.

                When ``strict_fps_inference``
                is False, framerate will be read from the first subtitle text in this case regardless of
                start and end frame (which may result in bogus result, if the first subtitle is not supposed
                to contain framerate). Before introduction of this option, the library behaved as if this
                option was False.

                .. versionchanged:: 1.7.0
                   Added the ``strict_fps_inference`` option.
        Nr   zFrame mismatch, expected {1}{1}zFFramerate was not specified and cannot be read from the MicroDVD file.r$   r   r   c                 @   |                      dd          } dt          t                   dt          fd}t          j        d||           } t          j        dd|           } t          j        d	d
|           } t          j        dd|           } |                                 S )N|\Nr   r   c                 d      fddD             }dd                     d |D                       z  S )Nc                 B    g | ]}|                     d           v |S )r   )group).0cr   s     r   
<listcomp>zZMicroDVDFormat.from_file.<locals>.prepare_text.<locals>.style_replacer.<locals>.<listcomp>O   s-    DDD!U[[^^0C0CA0C0C0Cr    biuz{%s} c              3   "   K   | ]
}d | dV  dS )\1N )r/   r0   s     r   	<genexpr>zYMicroDVDFormat.from_file.<locals>.prepare_text.<locals>.style_replacer.<locals>.<genexpr>P   s*      +D+D!IIII+D+D+D+D+D+Dr    )join)r   tagss   ` r   style_replacerzFMicroDVDFormat.from_file.<locals>.prepare_text.<locals>.style_replacerN   sB    DDDDuDDDD!BGG+D+Dt+D+D+D$D$DDDr    z\{[Yy]:[^}]+\}z\{[Ff]:([^}]+)\}z{\\fn\1}z\{[Ss]:([^}]+)\}z{\\fs\1}z\{P:(\d+),(\d+)\}z{\\pos(\1,\2)})replacer   strresubstrip)r   r;   s     r   prepare_textz.MicroDVDFormat.from_file.<locals>.prepare_textK   s    ||C//E%* E E E E E v/FFv1;EEv1;EEv24EtLLzz||#r    startendr   )r   r   groupsr   int
ValueErrorfloatr$   r	   r   r   r=   r
   append)r   r!   r"   r#   r$   r%   r&   liner   fstartfendr   rC   rD   rA   evs                   r   	from_filezMicroDVDFormat.from_file   sa   (  *	 *	D!''--E !&FD$sVTN33LFD{

@+ LVq[[TQYY()JKKK++C"DH! @ @ @) +? @ @ @@
 W\s;;;fd^LLJE3$3 $3 $ $ $ $ 3\\$5G5GHHHBKKOOOOU*	 *	s   3BB&write_fps_declarationapply_stylesc           
      b   |j         }|t          d          t          t          |          }dt          dt
          ffd}	|r3                    dt	          ddt          |          	                                                     D ]}
d
	                    |
j
                                                  }|r |	|
          rd|z   }t          ||
j        |
j        f          \  }}|dk     rd}|dk     rd}t          d|||fz  |           |r                    d           dS dS )ad  
        See :meth:`pysubs2.formats.FormatBase.to_file()`

        The only supported styling is marking whole lines italic.

        Keyword args:
            write_fps_declaration: If True, create a zero-duration first subtitle ``{1}{1}`` which will contain
                the fps.
            apply_styles: If False, do not write any styling.

        Nz2Framerate must be specified when writing MicroDVD.r(   rJ   r   c                 b   j                             | j        t          j                  }t          | j        |j                   D ]g\  }}|                    dd          }|                    dd          }|                    dd          }|j        s|r|	                                s dS hdS )Nz\h z\n
r+   FT)
stylesgetstyler   DEFAULT_STYLEr   r   r<   italicisspace)rJ   rW   fragmentstyr!   s       r   is_entirely_italicz2MicroDVDFormat.to_file.<locals>.is_entirely_italicq   s    KOODJ0FGGE!+DIudk!J!J ! !##++E377#++E488#++E488z !h !x7G7G7I7I ! 554r    r   r   rB   r*   z{Y:i}z
{%d}{%d}%s)file)r$   r	   r   r   r
   boolinsertr=   get_text_eventsr9   	plaintextr   r   rC   rD   printpop)r   r!   r"   r#   r$   rO   rP   r&   	to_framesr]   rJ   r   rC   rD   s    `            r   to_filezMicroDVDFormat.to_file\   sx    ;(C;!"VWWWLc222		X 	$ 	 	 	 	 	 	 ! 	DKK8!SBBBCCC((** 	> 	>D88DN557788D & 2 24 8 8 &~YTX(>??JE3 qyyQww,%d!33"===== ! 	HHQKKKKK	 	r    )NT)NTT)__name__
__module____qualname____doc__classmethodr=   r   r   r   rH   r_   r   rN   rf   r7   r    r   r   r      s       11     [ Y]/3= =Y =F =S =xPU =(,=?B=GK= = = [=~ W[IM3 39 3& 33 3Xe_ 3'+3BF3Y\3ae3 3 3 [3 3 3r    r   )	functoolsr   r>   typingr   r   r   r   
exceptionsr	   ssaeventr
   ssastyler   baser   
substationr   timer   r   ssafiler   compiler   r   r7   r    r   <module>rv      s         				 / / / / / / / / / / / / ( ( ( ( ( (                   " " " " " " - - - - - - - -       
@AA~ ~ ~ ~ ~Z ~ ~ ~ ~ ~r    