o
    tf                     @  s   U d dl m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
 g dZe Zded< G d	d
 d
ZG dd dZdS )    )annotationsN)deque)Iterator)Signal)FileContentsVirtualFileContents_VIRTUAL_FILESz5weakref.WeakValueDictionary[str, VirtualFileContents]r   c                   @  sL   e Zd ZdZeeZddddZeddd	Z	e	j
ddd	Z	dddZdS )r   a  Stores text file contents in memory and emits a signal when it changes.

    Calling `str(obj)` on this object will always return the current contents.

    Parameters
    ----------
    contents : str, optional
        The initial contents of the file (default: "")
     contentsstrc                 C  s
   || _ d S N	_contents)selfr
    r   a/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/anywidget/_file_contents.py__init__   s   
zVirtualFileContents.__init__returnc                 C     | j S r   r   r   r   r   r   r
   "   s   zVirtualFileContents.contentsvalueNonec                 C  s   || _ | j| d S r   )r   changedemit)r   r   r   r   r   r
   &   s   c                 C  r   r   )r
   r   r   r   r   __str__+   s   zVirtualFileContents.__str__N)r	   )r
   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   propertyr
   setterr   r   r   r   r   r      s    
r   c                   @  sR   e Zd ZdZeeZe ZddddZdddZ	dddZ
dddZdddZdS )r   a  Object that watches for file changes and emits a signal when it changes.

    Calling `str(obj)` on this object will always return the current contents of the
    file (as long as the thread is running).

    Parameters
    ----------
    path : str | pathlib.Path
        The file to read and watch for content changes
    start_thread : bool, optional
        Whether to start watching for changes in a separate thread (default: `True`)
    Tpathstr | pathlib.Pathstart_threadboolc                 C  sP   t |  | _| j stdd | _t	 | _
d | _|r&|   d S d S )Nz!File does not exist: {self._path})pathlibPath
expanduserabsolute_pathis_file
ValueErrorr   	threadingEvent_stop_event_background_threadwatch_in_thread)r   r"   r$   r   r   r   r   @   s   

zFileContents.__init__r   r   c                   s>    j durdS  j  tj fdddd _  j   dS )zEWatch for file changes (and emitting signals) from a separate thread.Nc                     s   t   ddS )Nr   )maxlen)r   watchr   r   r   r   <lambda>S   s    z.FileContents.watch_in_thread.<locals>.<lambda>T)targetdaemon)r0   r/   clearr-   Threadstartr   r   r   r   r1   J   s   


zFileContents.watch_in_threadc                 C  s,   | j du rdS | j  | j   d| _ dS )z.Stops an actively running thread if it exists.N)r0   r/   setjoinr   r   r   r   stop_threadX   s
   



zFileContents.stop_threadIterator[tuple[int, str]]c              
   c  s    z
ddl m}m} W n ty } ztd|d}~ww || j| jdD ]3}|D ].\}}||jkr;| j    dS ||jksE||j	krWd| _
| jt|  ||fV   nq)q%dS )a  Watch for file changes and emit changed/deleted signal events.

        Blocks indefinitely.

        Returns
        -------
        changes : Iterator[tuple[int, str]]
            An iterator that yields any time the file changes until the file is deleted.
        r   )Changer3   zkwatchfiles is required to watch for file changes during development. Install with `pip install watchfiles`.N)Z
stop_event)Z
watchfilesr>   r3   ImportErrorr*   r/   deletedr   modifiedaddedr   r   r   )r   r>   r3   excchangesZchanger"   r   r   r   r3   `   s0   



zFileContents.watchr   c                 C  s    | j d u r| jjdd| _ | j S )Nzutf-8)encoding)r   r*   	read_textr   r   r   r   r   ~   s   
zFileContents.__str__N)T)r"   r#   r$   r%   )r   r   )r   r=   r   )r   r   r   r   r   r   r   r@   r   r1   r<   r3   r   r   r   r   r   r   /   s    



r   )
__future__r   r&   r-   weakrefcollectionsr   typingr   Zpsygnalr   __all__WeakValueDictionaryr   __annotations__r   r   r   r   r   r   <module>   s    
