o
    NK&h!                     @   sh  U d dl 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	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZmZm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 d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& zd dl'm(Z( dZ)G dd de(Z*W n e+y   dZ)Y nw zd dl,Z,W n e+y   e-dej.d Y nw da/ee e0d< da1ee& e0d< G dd de$Z2G dd deZ3				d1de4d eee5e4ef ee6e4e4f  f  d!e4d"ee4 fd#d$Z7d%e&fd&d'Z8				d1de4d eee5e4ef ee6e4e4f  f  d!e4d"ee4 d%e3f
d(d)Z9d*e4d+e4d,e4d%e4fd-d.Z:G d/d0 d0eZ;dS )2    N)PyCF_ALLOW_TOP_LEVEL_AWAIT)iscoroutinenew_event_loop)InteractiveConsole)Sequence)FunctionType)Any
NamedTupleOptionalUnion)RequestSanic)Header)Default)Stage)Colors)REPLContext)TransportProtocol)HTTPResponse)ClientTc                       s"   e Zd Zdef fddZ  ZS )SanicClientappc                    s&   | |jjd j}t j|d d S )Nr   )base_url)get_server_locationstateserver_infosettingssuper__init__)selfr   r   	__class__ D/var/www/html/venv/lib/python3.10/site-packages/sanic/cli/console.pyr       s   zSanicClient.__init__)__name__
__module____qualname__r   r   __classcell__r"   r"   r    r#   r      s    r   FzDModule 'readline' not available. History navigation will be limited.)filerepl_apprepl_responsec                   @   s$   e Zd Zdd Zdd Zdd ZdS )REPLProtocolc                 C   s   t j| _d| _d S )NT)r   IDLEstagerequest_bodyr   r"   r"   r#   r   6   s   
zREPLProtocol.__init__c                 C   s   |a | |_|S N)r*   stream)r   responser"   r"   r#   respond:   s   zREPLProtocol.respondc                    s   d S r0   r"   )r   data
end_streamr"   r"   r#   send@   s    zREPLProtocol.sendN)r$   r%   r&   r   r3   r6   r"   r"   r"   r#   r+   5   s    r+   c                   @   s   e Zd ZU eed< eed< dS )Resultrequestr2   N)r$   r%   r&   r   __annotations__r   r"   r"   r"   r#   r7   C   s   
 r7   /GETurlheadersmethodbodyc                 C   sV   t sJ d|p	i }t }t|  t|d||t }|d ur#| |_||_d |_|S )N!No Sanic app has been registered.z1.1)r)   r+   r   encoder   r?   r1   	conn_info)r<   r=   r>   r?   protocolr8   r"   r"   r#   make_requestH   s    
rD   returnc                    s*   t sJ dt | I d H  tsJ tS )Nr@   )r)   handle_requestr*   )r8   r"   r"   r#   r3   `   s
   r3   c                    s(   t | |||}t|I d H }t||S r0   )rD   r3   r7   )r<   r=   r>   r?   r8   r2   r"   r"   r#   dog   s   
rG   namedesc	type_descc              
   C   s:   dt jt j  |  t j d| dt jt j  | t j 
S )Nz  - z: z - )r   BOLDSANICENDBLUE)rH   rI   rJ   r"   r"   r#   _variable_descriptionr   s    
rO   c                       sz   e Zd Zddedee f fddZdd Zdd	 Zd fdd	Z	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )	SanicREPLNr   startc                    s  |a |ttd}dd |jD }d}tdtjd t|tdtjd dtdtjd d	g}d
d |jD }trKt	||d< |
tdtjd d ndtj dtj d}t ji ||d | jj jtO  _t | _|| _t | _t | _tj| jdd| _tj| jdd| _tj| jj dd| _!|| _"| #  d| _$d%dtj& dtj |dg||rdg|ng  ddtj& dtj dtj& dtj dtj& dtj dg | _'d S )N)r   sanicrG   c                 S   s   i | ]}|j |jqS r"   )rH   var.0
user_localr"   r"   r#   
<dictcomp>   s    
z&SanicREPL.__init__.<locals>.<dictcomp> r   rR   zimport sanicrG   zResult(request, response)c                 S   s&   g | ]}t |j|jtt|jqS r"   )rO   rH   rI   strtyperS   rT   r"   r"   r#   
<listcomp>   s    z&SanicREPL.__init__.<locals>.<listcomp>clientzfrom httpx import Client
zRThe HTTP client has been disabled. To enable it, install httpx:
	pip install httpx)localsT)targetdaemonzClosing the REPL.z(Welcome to the Sanic interactive consolez9The following objects are available for your convenience:z
REPL Context:z5
The async/await keywords are available for use here.zTo exit, press zCTRL+Cz, zCTRL+Dz
, or type exit()z.
)(r)   rR   rG   repl_ctxrO   r   BUILTINSrY   HTTPX_AVAILABLEr   appendr   YELLOWrM   r   r   compilecompilerflagsr   r   loop_start	threadingEvent_pause_event_started_eventThread_console_interact_thread_monitor_monitor_threadrun_forever_async_threadr   resumeexit_messagejoinrK   banner_message)r   r   rQ   locals_availableuser_localsclient_availabilityvariable_descriptionsuser_locals_descriptionsr    r"   r#   r   z   s   
	


zSanicREPL.__init__c                 C   s   |   rd S | j  d S r0   )	is_pausedrn   clearr/   r"   r"   r#   pause   s   zSanicREPL.pausec                 C   s   | j   d S r0   )rn   setr/   r"   r"   r#   rw      s   zSanicREPL.resume<input>singlec                    s<   |  dkr|   dS |  rtd dS t |||S )Nra   Fz4Console is paused. Please wait for it to be resumed.)strip	_shutdownr   printr   	runsource)r   sourcefilenamesymbolr    r"   r#   r      s   zSanicREPL.runsourcec                    s6   t j  fdd}jjj|   S )Nc                     sX   t  j} z|  }t|r|I d H }W n ty$   t  d}Y nw | d S )NF)r   r^   r   BaseException	traceback	print_exc
set_result)funcresultcodefuturer   r"   r#   callback   s   
z#SanicREPL.runcode.<locals>.callback)
concurrentfuturesFuturerj   call_soon_threadsafecreate_taskr   )r   r   r   r"   r   r#   runcode   s   
zSanicREPL.runcodec                 C   s   | j   S r0   )rn   is_setr/   r"   r"   r#   r      s   zSanicREPL.is_pausedc                 C   s(   | j   | j| j| jd |   d S )N)bannerexitmsg)ro   r   interactrz   rx   r   r/   r"   r"   r#   rq      s   
zSanicREPL._consolec              	   C   s   t | jtrtjtj  dtj }td| d}|rd S z=	 | j	 s1| j
j  | j  n%| j
j rH|  rH|   ttjddd n| j
j sV|  sV|   td q  ttfyg   Y d S w )NENTERz
Press z  at anytime to start the REPL.

TrX   )endflushg?)
isinstancerk   r   r   rK   rL   rM   inputro   r   r   managerwait_for_ackrr   rQ   _all_workers_ackr   rw   r   sysps1r   timesleepConnectionResetErrorBrokenPipeError)r   enterrQ   r"   r"   r#   rs     s.   


zSanicREPL._monitorc                 C   s   | j jjd d S )N__TERMINATE__)r   r   monitor_publisherr6   r/   r"   r"   r#   r     s   zSanicREPL._shutdownc                 C   s   | j   | j  d S r0   )rt   rQ   rv   r/   r"   r"   r#   run  s   
zSanicREPL.runr0   )r   r   )r$   r%   r&   r   r
   r   r   r   rw   r   r   r   rq   rs   r   r   r'   r"   r"   r    r#   rP   y   s    [rP   )r:   Nr;   N)<concurrent.futuresr   r   rl   r   r   astr   asyncior   r   r   r   collections.abcr   typesr   typingr   r	   r
   r   rR   r   r   sanic.compatr   sanic.helpersr   sanic.http.constantsr   	sanic.logr   sanic.models.ctx_typesr   sanic.models.protocol_typesr   sanic.response.typesr   httpxr   rd   r   ImportErrorreadliner   stderrr)   r9   r*   r+   r7   rY   dicttuplerD   r3   rG   rO   rP   r"   r"   r"   r#   <module>   s   
 
"
"
