
    k:i<                        d Z ddlmZ ddl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mZmZmZmZmZ ddlmZmZ erdd	lmZ eee   edf   Z G d
 d      Z G d de      Zy)aG  
    pyte.streams
    ~~~~~~~~~~~~

    This module provides three stream implementations with different
    features; for starters, here's a quick example of how streams are
    typically used:

    >>> import pyte
    >>> screen = pyte.Screen(80, 24)
    >>> stream = pyte.Stream(screen)
    >>> stream.feed("[5B")  # Move the cursor down 5 rows.
    >>> screen.cursor.y
    5

    :copyright: (c) 2011-2012 by Selectel.
    :copyright: (c) 2012-2017 by pyte authors and contributors,
                    see AUTHORS for details.
    :license: LGPL, see LICENSE for more details.
    )annotationsN)defaultdict)Mapping)AnyCallableDict	GeneratorOptionalTYPE_CHECKING   )controlescape)Screenc                  p   e Zd ZdZej
                  dej                  dej                  dej                  dej                  dej                  dej                  dej                  dej                  di	Zej                   d	ej"                  d
ej$                  dej&                  dej(                  dej*                  dej,                  diZej0                  diZi ej4                  dej6                  dej8                  dej:                  dej<                  dej>                  dej@                  dejB                  dejD                  dejF                  dejH                  dejJ                  dejL                  dejN                  dejP                  dejR                  dejT                  dejV                  d ejX                  dejZ                  dej\                  d!ej^                  d"ej`                  d#ejb                  d$ejd                  d%ejf                  d&ejh                  di
Z5 e6 e7jp                  ejs                         ejs                         ejs                         e5js                         d'gd(d)gd*d+g            Z: e;ejx                  ejz                  ej|                  ej~                  ej                  g      ZAeAj                  e        eCj                  d,d-j                   eFeCj.                  eA            z   d.z         ZG[Ad8d9d0ZHd:d1ZId:d2ZJd;d3ZKd<d4ZLd=d5ZMd>d6ZNd?d7ZOy/)@Streama  A stream is a state machine that parses a stream of bytes and
    dispatches events based on what it sees.

    :param pyte.screens.Screen screen: a screen to dispatch events to.
    :param bool strict: check if a given screen implements all required
                        events.

    .. note::

       Stream only accepts text as input, but if for some reason
       you need to feed it with bytes, consider using
       :class:`~pyte.streams.ByteStream` instead.

    .. versionchanged 0.6.0::

       For performance reasons the binding between stream events and
       screen methods was made static. As a result, the stream **will
       not** dispatch events to methods added to screen **after** the
       stream was created.

    .. seealso::

        `man console_codes <http://linux.die.net/man/4/console_codes>`_
            For details on console codes listed bellow in :attr:`basic`,
            :attr:`escape`, :attr:`csi`, :attr:`sharp`.
    bell	backspacetablinefeedcarriage_return	shift_outshift_inresetindexreverse_indexset_tab_stopsave_cursorrestore_cursoralignment_displayinsert_characters	cursor_upcursor_downcursor_forwardcursor_backcursor_down1
cursor_up1cursor_to_columncursor_positionerase_in_displayerase_in_lineinsert_linesdelete_linesdelete_characterserase_charactersreport_device_attributescursor_to_lineclear_tab_stopset_mode
reset_modeselect_graphic_renditionreport_device_statusset_marginsdefine_charsetset_icon_name	set_titledrawdebugz[^ z]+Nc                d    d | _         || _        d| _        d | _        || j	                  |       y y NT)listenerstrictuse_utf8_taking_plain_textattach)selfscreenr@   s      C/var/www/html/bot/venv/lib/python3.12/site-packages/pyte/streams.py__init__zStream.__init__   s6    *."26KK     c                F   | j                   3t        j                  dj                  | j                         t               | j
                  r8| j                  D ])  }t        ||      rt        dj                  ||             || _         d| _	        | j                          y)zvAdds a given screen to the listener queue.

        :param pyte.screens.Screen screen: a screen to attach to.
        NzqAs of version 0.6.0 the listener queue is restricted to a single element. Existing listener {0} will be replaced.z{0} is missing {1})r?   warningswarnformatDeprecationWarningr@   eventshasattr	TypeError_parser_initialize_parser)rD   rE   events      rF   rC   zStream.attach   s    
 ==$MM ; "6$--02DF
 ;; Pvu-#$8$?$?$NOOP 26!rH   c                0    || j                   u rd| _         yy)zRemove a given screen from the listener queue and fails
        silently if it's not attached.

        :param pyte.screens.Screen screen: a screen to detach.
        N)r?   )rD   rE   s     rF   detachzStream.detach   s     T]]" DM #rH   c                |   | j                   }| j                  t        d      | j                  j                  }| j                  j
                  }| j                  }t        |      }d}||k  rH|r- |||      }|r|j                         \  }	} |||	|        nd}n ||||dz          }|dz  }||k  rH|| _        y)zvConsume some data and advances the state as necessary.

        :param str data: a blob of data to feed from.
        NzListener is not setr   Fr   )	_send_to_parserr?   RuntimeErrorr:   _text_patternmatchrB   lenspan)
rD   datasendr:   
match_texttaking_plain_textlengthoffsetrZ   starts
             rF   feedzStream.feed   s    
 ##== 455}}!!''--
 33Tvo "40$)JJLME6eF+,(-%$(fVaZ)@$A!! vo #4rH   c                    	 | j                   J | j                   j                  |      S # t        $ r | j                           w xY wN)rQ   r^   	ExceptionrR   )rD   r]   s     rF   rW   zStream._send_to_parser   sL    	<<+++<<$$T** 	 ##%		s	   (+ Ac                b    | j                         | _        t        | j                        | _        y rf   )_parser_fsmrQ   nextrB   )rD   s    rF   rR   zStream._initialize_parser   s#    '')"&t||"4rH   c           	   #  *  K   | j                   }| j                  J | j                  j                  }j                  t        j
                  t        j                  }}t        j                  }t        j                  dz   }t        j                  t        j                  z   }t        j                  t        j                  z   }dj                  t        j                  t        j                  t        j                   t        j"                  t        j$                  t        j&                  t        j(                  g      }	t+        t        j,                  t        j.                  t        j                  g      }
dfd} ||      } || j0                        } || j2                        } || j4                        }	 d}||k(  rsd}|dk(  r|}ng|dk(  r|}n_|dk(  r |d           nL|d	k(  r| j7                  d       n3|d
v r%d}| j8                  r^j;                  ||       n
 ||           }||v r?|t        j<                  k(  s|t        j>                  k(  r| j8                  r ||           n||k(  rg }d}d}	 d}|dk(  rd}n||	v r ||           ny||v rnt||v r	 ||       n|jA                         r||z  }nQ|dk(  rd n|jC                  tE        tG        |xs d      d             |dk(  rd}n|r ||   |ddi n ||   |  nr||k(  r`d}|dk(  rq|dk(  rxd}	 d}||k(  r|dz  }||
v rn||z  }|dd }|dv rjI                  |       |dv rjK                  |       n||vr ||       ӭw)aR  An FSM implemented as a coroutine.

        This generator is not the most beautiful, but it is as performant
        as possible. When a process generates a lot of output, then this
        will be the bottleneck, because it processes just one character
        at a time.

        Don't change anything without profiling first.
        N>r<   c                b    t        fdt        fd| j                         D                    S )Nc                      S rf    )r;   s   rF   <lambda>z?Stream._parser_fsm.<locals>.create_dispatcher.<locals>.<lambda>   s    u rH   c              3  @   K   | ]  \  }}|t        |      f  y wrf   )getattr).0rS   attrr?   s      rF   	<genexpr>z@Stream._parser_fsm.<locals>.create_dispatcher.<locals>.<genexpr>   s)      34E4 $/034s   )r   dictitems)mappingr;   r?   s    rF   create_dispatcherz-Stream._parser_fsm.<locals>.create_dispatcher   s,    }d 34#*==?34 /4 5 5rH   T[]#%z())modeF?$r   i'  ;privateRPr   0102)rx   zMapping[str, str]returnzDict[str, Callable[..., None]])&basicr?   r:   r;   ctrlESCCSI_C1OSC_C1SPNULDELCANSUBjoinBELBSHTLFVTFFCRsetST_C0ST_C1sharpr   csiselect_other_charsetrA   r7   SISOisdigitappendminintr8   r9   )rD   r   r:   r   r   r   SP_OR_GT
NUL_OR_DEL
CAN_OR_SUBALLOWED_IN_CSIOSC_TERMINATORSry   basic_dispatchsharp_dispatchescape_dispatchcsi_dispatchcharcodeparamscurrentr   paramr;   r?   s                         @@rF   ri   zStream._parser_fsm   sx     

}}(((==}}hhV77S=XX(
XX(
$((DGGTWWdgg"&''477DGG"= >tzz4::txx@A	5
 +51*4::6+DKK8(2 :Ds{ "z3;!DS[!Ds{4d
4611:?%)z==$ !//4/@--/u} DGGOttwwDMM$t$& !%:Ds{"&/,t,.)+
 T
4 #
c#gl*;T&BC3;&(G& 2T 2F ID I 2T 2F ;!A B !z3;S[!%:Ds{d
*.  ab	4<**514<&&u-Z'T
s s   NNc                     y)a,  Select other (non G0 or G1) charset.

        :param str code: character set code, should be a character from
                         ``"@G8"``, otherwise ignored.

        .. note:: We currently follow ``"linux"`` and only use this
                  command to switch from ISO-8859-1 to UTF-8 and back.

        .. versionadded:: 0.6.0

        .. seealso::

           `Standard ECMA-35, Section 15.4            <http://ecma-international.org/publications/standards/Ecma-035.htm>`_
           for a description of VTXXX character set machinery.
        Nro   rD   r   s     rF   r   zStream.select_other_charset~  s    rH   r>   )rE   zOptional[Screen]r@   boolr   None)rE   r   r   r   )r]   strr   r   )r]   r   r   zOptional[bool])r   r   )r   ParserGeneratorr   r   r   r   )P__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   escRISINDNELRIHTSDECSCDECRCr   DECALNr   ICHCUUCUDCUFCUBCNLCPLCHACUPEDELILDLDCHECHHPRDAVPAVPRHVPTBCSMRMSGRDSRDECSTBMHPAr   	frozenset	itertoolschainvaluesrN   r   r   r   r   r   r   _specialupdaterecompiler   maprY   rG   rC   rU   rd   rW   rR   ri   r   ro   rH   rF   r   r   '   s&   : 	&"
E 			=		#F 	

'E
$ 	 	!	
 	 	 	 	# 	" 	" 	 	 	 	$ 	#  	!!" 	*#$ 	!"!
+']#7C> y		+&		 F DHHdkk488TXXt{{KLHOOEBJJrwws299h/00479M "(!4:5\|rH   r   c                  8     e Zd ZdZd fdZd fdZddZ xZS )
ByteStreamaE  A stream which takes bytes as input.

    Bytes are decoded to text using either UTF-8 (default) or the encoding
    selected via :meth:`~pyte.Stream.select_other_charset`.

    .. attribute:: use_utf8

       Assume the input to :meth:`~pyte.streams.ByteStream.feed` is encoded
       using UTF-8. Defaults to ``True``.
    c                l    t        t        | 
  |i |  t        j                  d      d      | _        y )Nzutf-8replace)superr   rG   codecsgetincrementaldecoderutf8_decoder)rD   argskwargs	__class__s      rF   rG   zByteStream.__init__  s1    j$($9&9AF88A)LrH   c                    | j                   r| j                  j                  |      }ndj                  t	        t
        |            }t        t        | #  |       y )Nr<   )	rA   r   decoder   r   chrr   r   rd   )rD   r]   data_strr   s      rF   rd   zByteStream.feed  sC    ==((//5Hwws3~.Hj$$X.rH   c                j    |dk(  r"d| _         | j                  j                          y |dv rd| _         y y )N@FG8T)rA   r   r   r   s     rF   r   zByteStream.select_other_charset  s5    3;!DM##%T\ DM rH   )r   r   r   r   )r]   bytesr   r   r   )r   r   r   r   rG   rd   r   __classcell__)r   s   @rF   r   r     s    	M
/!rH   r   )r   
__future__r   r   r   r   rJ   collectionsr   collections.abcr   typingr   r   r   r	   r
   r   r<   r   r   r   r   screensr   r   r   r   r   r   ro   rH   rF   <module>r     s`   ( #   	  # # J J , HTNC56g gV! !rH   