
    AHjZ(                        d dl mZ d dlZd dlZd dlmZm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mZ d dlmZ erd d	lmZ  G d
 de      Zy)    )annotationsN)ABCabstractmethod)contextmanager)Path)TYPE_CHECKINGAnyBinaryIOIteratorLiteralTextIO)eventslogmessages)MouseUp)Appc                     e Zd ZdZdddd	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Zedd	       Zedd
       Z	d dZ
d dZed!d       Zd"dZed"d       Zed"d       Zed"d       Zd"dZd"dZ G d dej*                        Zed#d       Zd"dZd$d%dZddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 	 	 	 	 d'dZ	 	 	 	 	 	 	 	 d(dZy))DriverzA base class for drivers.FTN)debugmousesizec                   || _         || _        || _        || _        t	        j
                         | _        g | _        d| _        d| _	        	 d| _
        y)zInitialize a driver.

        Args:
            app: The App instance.
            debug: Enable debug mode.
            mouse: Enable mouse support,
            size: Initial size of the terminal or `None` to detect.
        NT)_app_debug_mouse_sizeasyncioget_running_loop_loop_down_buttons_last_move_event_auto_restartcursor_origin)selfappr   r   r   s        F/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/driver.py__init__zDriver.__init__   sS      	
--/
(*9=!F59    c                     y)z%Is the driver 'headless' (no output)?F r$   s    r&   is_headlesszDriver.is_headless/        r(   c                     y)z)Is the driver 'inline' (not full-screen)?Fr*   r+   s    r&   	is_inlinezDriver.is_inline4   r-   r(   c                     y)z,Is the driver 'web' (running via a browser)?Fr*   r+   s    r&   is_webzDriver.is_web9   r-   r(   c                     y)zCan this driver be suspended?Fr*   r+   s    r&   can_suspendzDriver.can_suspend>   r-   r(   c                x    t        j                  | j                  j                  |      | j                         y)zYSend a message to the target app.

        Args:
            message: A message.
        )loopN)r   run_coroutine_threadsafer   _post_messager   )r$   messages     r&   send_messagezDriver.send_messageC   s*     	((II##G,4::	
r(   c                4   |j                  | j                         | j                  d}d}n| j                  \  }}t        |t        j
                        rT|xj                  |z  c_        |xj                  |z  c_        |xj                  |z  c_        |xj                  |z  c_	        t        |t        j                        r4|j                  r| j                  j                  |j                         nt        |t        j                        rM|j                  ri|j                  | j                  v rP| j                  j                  |j                         n)t        |t        j                         r| j                  r|j                  s| j"                  t%        t&        j)                  | j                        j+                               }| j                  j-                          | j"                  }|D ]  }| j/                  t        |j0                  |j2                  |j4                  dd||j6                  |j8                  |j:                  |j<                  |j>                  |j@                                || _        | j/                  |       y)z~Perform additional processing on a message, prior to sending.

        Args:
            event: A message to process.
        Nr   )xydelta_xdelta_ybuttonshiftmetactrlscreen_xscreen_ystyle)!
set_senderr   r#   
isinstancer   
MouseEvent_x_y	_screen_x	_screen_y	MouseDownr?   r    appendr   remove	MouseMover!   listdictfromkeyskeysclearr9   widgetr;   r<   r@   rA   rB   rC   rD   rE   )r$   r8   offset_xoffset_ybuttons
move_eventr?   s          r&   process_messagezDriver.process_messageM   s    	499%%HH!%!3!3Hhgv001JJ("JJJ("J))gv//0~~""))'..90~~'..D4F4F"F""))'..9!1!12""))5 t}}T-?-?@EEGH""((*!22
% F%%#NN(ll(ll$%$%#)")--!(!(%/%8%8%/%8%8")--" %,D!'"r(   c                     y)zTWrite data to the output device.

        Args:
            data: Raw data.
        Nr*   )r$   datas     r&   writezDriver.write       r(   c                     y)zFlush any buffered data.Nr*   r+   s    r&   flushzDriver.flush   r_   r(   c                     y)zStart application mode.Nr*   r+   s    r&   start_application_modezDriver.start_application_mode   r_   r(   c                     y)zDisable further input.Nr*   r+   s    r&   disable_inputzDriver.disable_input   r_   r(   c                     y)z%Stop application mode, restore state.Nr*   r+   s    r&   stop_application_modezDriver.stop_application_mode   r_   r(   c                D    | j                          | j                          y)zSuspend application mode.

        Used to suspend application mode and allow uninhibited access to the
        terminal.
        N)rg   closer+   s    r&   suspend_application_modezDriver.suspend_application_mode   s     	""$

r(   c                $    | j                          y)zzResume application mode.

        Used to resume application mode after it has been previously
        suspended.
        N)rc   r+   s    r&   resume_application_modezDriver.resume_application_mode   s     	##%r(   c                      e Zd ZdZy)Driver.SignalResumez?Event sent to the app when a resume signal should be published.N)__name__
__module____qualname____doc__r*   r(   r&   SignalResumern      s    Mr(   rs   c              #  b   K   | j                   }d| _         	 d || _         y# || _         w xY ww)a  A context manager used to tell the driver to not auto-restart.

        For drivers that support the application being suspended by the
        operating system, this context manager is used to mark a body of
        code as one that will manage its own stop and start.
        FN)r"   )r$   auto_restarts     r&   no_automatic_restartzDriver.no_automatic_restart   s3      ))"	.!-DDs   /# /	,/c                     y)zPerform any final cleanup.Nr*   r+   s    r&   ri   zDriver.close   r_   r(   c                .    ddl }|j                  |       y)a0  Open a URL in the default web browser.

        Args:
            url: The URL to open.
            new_tab: Whether to open the URL in a new tab.
                This is only relevant when running via the WebDriver,
                and is ignored when called while running through the terminal.
        r   N)
webbrowseropen)r$   urlnew_tabry   s       r&   open_urlzDriver.open_url   s     	r(   download)open_methodencoding	mime_typenamec                    d fd}t        |t              rd}	nd}	t        j                  |||	f      }
|
j	                          y)a  Save the file `path_or_file` to `save_path`.

        If running via web through Textual Web or Textual Serve,
        this will initiate a download in the web browser.

        Args:
            binary: The binary file to save.
            delivery_key: The unique key that was used to deliver the file.
            save_path: The location to save the file to.
            open_method: *web only* Whether to open the file in the browser or
                to prompt the user to download it. When running via a standard
                (non-web) terminal, this is ignored.
            encoding: *web only* The text encoding to use when saving the file.
                This will be passed to Python's `open()` built-in function.
                When running via web, this will be used to set the charset
                in the `Content-Type` header.
            mime_type: *web only* The MIME type of the file. This will be used to
                set the `Content-Type` header in the HTTP response.
            name: A user-defined named which will be returned in [`DeliveryComplete`][textual.events.DeliveryComplete]
                and [`DeliveryComplete`][textual.events.DeliveryComplete].

        c                @   	 t        |
xs d      5 }| j                  }|j                  }d}	  ||      }|sj                  	       n	 ||       (	 d d d        | j                  s| j                          y y # 1 sw Y   'xY w# t        $ rf}t        j                  d|        dd l}t        j                  t        |j                                      j                  	|       Y d }~d }~ww xY w# | j                  s| j                          w w xY w)Nzutf-8)r   i   )	save_pathr   zFailed to deliver file: r   )	exceptionr   )rz   readr^   _delivery_complete	Exceptionr   error	tracebackstr
format_exc_delivery_failedclosedri   )binarymodedestination_filer   r^   
chunk_sizer]   r   r   delivery_keyr   r   r   r$   s            r&   save_file_threadz/Driver.deliver_binary.<locals>.save_file_thread   s   #th.A' $%!;;D,22E!*J#J/# 33 ,	 4  "d  "$0 }}LLN %1$ $  P 		4UG<= 		#i22456%%le$%OOP }}LLN %sB   B AB B  B	B 	C;AC61C> 6C;;C> >Dwbw)targetargsN)r   BinaryIO | TextIOr   r   returnNone)rG   r
   	threadingThreadstart)r$   r   r   r   r   r   r   r   r   r   threads   ` `` ` `   r&   deliver_binaryzDriver.deliver_binary   sD    D	# 	#: fh'DD!!)9Or(   c                    | j                   j                  | j                   j                  t        j                  |||             y)zsCalled when a file has been delivered successfully.

        Delivers a DeliveryComplete event to the app.
        )keypathr   N)r   call_from_threadpost_messager   DeliveryComplete)r$   r   r   r   s       r&   r   zDriver._delivery_complete  s5     			""II""##94P	
r(   c                    | j                   j                  | j                   j                  t        j                  |||             y)z`Called when a file delivery fails.

        Delivers a DeliveryFailed event to the app.
        )r   r   r   N)r   r   r   r   DeliveryFailed)r$   r   r   r   s       r&   r   zDriver._delivery_failed#  s5     			""II""!!lidS	
r(   )
r%   zApp[Any]r   boolr   r   r   ztuple[int, int] | Noner   r   )r   r   )r8   zmessages.Messager   r   )r]   r   r   r   )r   r   )r   zIterator[None])T)r{   r   r|   r   r   r   )r   r   r   r   r   r   r   zLiteral['browser', 'download']r   
str | Noner   r   r   r   r   r   )r   r   r   zPath | Noner   r   r   r   )r   r   r   BaseExceptionr   r   r   r   )ro   rp   rq   rr   r'   propertyr,   r/   r1   r3   r9   r[   r   r^   ra   rc   re   rg   rj   rl   r   Eventrs   r   rv   ri   r}   r   r   r   r*   r(   r&   r   r      s   # '+:: 	:
 : %: 
:6        
7#r  ' & & % % 4 4&Nv|| N . .)& 7A# $E!E 	E
 E 4E E E E 
EN



,7

?I

	





,9

AK

	

r(   r   )
__future__r   r   r   abcr   r   
contextlibr   pathlibr   typingr   r	   r
   r   r   r   textualr   r   r   textual.eventsr   textual.appr   r   r*   r(   r&   <module>r      s:    "   # %  J J ) ) "\
S \
r(   