o
    2g                     @  s   U d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ dZded	< dddZdddZG dd deZdS )zShared code between dmypy.py and dmypy_server.py.

This should be pretty lightweight and not depend on other mypy code (other than ipc).
    )annotationsN)TracebackType)AnyFinalIterableIteratorTextIO)IPCBasez.dmypy.jsonr   DEFAULT_STATUS_FILE
connectionr	   returnr   c              
   C  sh   |   }|s
tdzt|}W n ty" } ztd|d}~ww t|ts2tdt| d|S )zReceive single JSON data frame from a connection.

    Raise OSError if the data received is not valid JSON or if it is
    not a dict.
    zNo data receivedzData received is not valid JSONNzData received is not a dict ())readOSErrorjsonloads	Exception
isinstancedicttype)r   bdatadatae r   G/home/garg/my-data/venv/lib/python3.10/site-packages/mypy/dmypy_util.pyreceive   s   

r   r   Nonec                 C  s   |  t| dS )zSend data to a connection encoded and framed.

    The data must be JSON-serializable. We assume that a single send call is a
    single frame to be sent on the connect.
    N)writer   dumps)r   r   r   r   r   send$   s   r   c                   @  s   e Zd ZdZdHd
dZdIddZdJddZdKddZdLddZdMddZ	dNdd Z
dMd!d"ZdOd#d$ZdPdQd'd(ZdOd)d*ZdPdRd,d-ZdPdSd0d1ZdPdTd4d5ZdOd6d7ZdNd8d9ZdPdUd<d=ZdVd?d@ZdOdAdBZdWdEdFZdGS )XWriteToConnzAHelper class to write to a connection instead of standard output.serverr	   
output_keystrisattyboolr   r   c                 C  s   || _ || _|| _d S N)r!   r"   _isatty)selfr!   r"   r$   r   r   r   __init__0   s   
zWriteToConn.__init__r   c                 C  s   | S r&   r   r(   r   r   r   	__enter__5      zWriteToConn.__enter__ttype[BaseException] | NonevalueBaseException | None	tracebackTracebackType | Nonec                 C     d S r&   r   )r(   r-   r/   r1   r   r   r   __exit__8   s   zWriteToConn.__exit__Iterator[str]c                 C     t jr&   ioUnsupportedOperationr*   r   r   r   __iter__@      zWriteToConn.__iter__c                 C  r6   r&   r7   r*   r   r   r   __next__C   r;   zWriteToConn.__next__c                 C  r3   r&   r   r*   r   r   r   closeF   r,   zWriteToConn.closeintc                 C  s   t r&   )r   r*   r   r   r   filenoI   r,   zWriteToConn.filenoc                 C  r3   r&   r   r*   r   r   r   flushL   r,   zWriteToConn.flushc                 C  s   | j S r&   )r'   r*   r   r   r   r$   O   r;   zWriteToConn.isattyr   nc                 C  r6   r&   r7   )r(   rA   r   r   r   r   R   r;   zWriteToConn.readc                 C     dS NFr   r*   r   r   r   readableU   r,   zWriteToConn.readablelimitc                 C  r6   r&   r7   )r(   rE   r   r   r   readlineX   r;   zWriteToConn.readlinehint	list[str]c                 C  r6   r&   r7   )r(   rG   r   r   r   	readlines[   r;   zWriteToConn.readlinesoffsetwhencec                 C  r6   r&   r7   )r(   rJ   rK   r   r   r   seek^   r;   zWriteToConn.seekc                 C  rB   rC   r   r*   r   r   r   seekablea   r,   zWriteToConn.seekablec                 C  r6   r&   r7   r*   r   r   r   telld   r;   zWriteToConn.tellsize
int | Nonec                 C  r6   r&   r7   )r(   rO   r   r   r   truncateg   r;   zWriteToConn.truncateoutputc                 C  s"   i }||| j < t| j| t|S r&   )r"   r   r!   len)r(   rR   respr   r   r   r   j   s   
zWriteToConn.writec                 C  rB   )NTr   r*   r   r   r   writablep   r,   zWriteToConn.writablelinesIterable[str]c                 C  s   |D ]}|  | qd S r&   )r   )r(   rV   sr   r   r   
writeliness   s   zWriteToConn.writelinesN)r!   r	   r"   r#   r$   r%   r   r   )r   r   )r-   r.   r/   r0   r1   r2   r   r   )r   r5   )r   r#   )r   r   )r   r>   )r   r%   )r   )rA   r>   r   r#   )rE   r>   r   r#   )rG   r>   r   rH   )rJ   r>   rK   r>   r   r>   )rO   rP   r   r>   )rR   r#   r   r>   )rV   rW   r   r   )__name__
__module____qualname____doc__r)   r+   r4   r:   r<   r=   r?   r@   r$   r   rD   rF   rI   rL   rM   rN   rQ   r   rU   rY   r   r   r   r   r    -   s,    













r    )r   r	   r   r   )r   r	   r   r   r   r   )r]   
__future__r   r8   r   typesr   typingr   r   r   r   r   mypy.ipcr	   r
   __annotations__r   r   r    r   r   r   r   <module>   s    

	