
    }tf                        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j                         Zded<    G d	 d
      Z G d d      Zy)    )annotationsN)deque)Iterator)Signal)FileContentsVirtualFileContents_VIRTUAL_FILESz5weakref.WeakValueDictionary[str, VirtualFileContents]r	   c                  j    e Zd ZdZ ee      ZdddZed	d       Z	e	j                  d
d       Z	d	dZy)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: "")
    c                    || _         y N	_contents)selfcontentss     b/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/anywidget/_file_contents.py__init__zVirtualFileContents.__init__   s	    !    c                    | j                   S r   r   r   s    r   r   zVirtualFileContents.contents"   s    ~~r   c                H    || _         | j                  j                  |       y r   )r   changedemit)r   values     r   r   zVirtualFileContents.contents&   s    % r   c                    | j                   S r   )r   r   s    r   __str__zVirtualFileContents.__str__+   s    }}r   N) )r   strreturnr   )r   r   r   None)__name__
__module____qualname____doc__r   r   r   r   propertyr   setterr    r   r   r   r      sH     SkG"   __! !r   r   c                  X    e Zd ZdZ ee      Z e       Zdd	dZd
dZ	d
dZ
ddZddZy)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`)
    c                .   t        j                  |      j                         j                         | _        | j                  j                         st        d      d | _        t        j                         | _
        d | _        |r| j                          y y )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   pathstart_threads      r   r   zFileContents.__init__@   sr    \\$'224==?
zz!!#@AA%)$??,;?  " r   c                      j                   y j                  j                          t        j                   fdd       _          j                   j                          y)zEWatch for file changes (and emitting signals) from a separate thread.Nc                 :    t         j                         d      S )Nr   )maxlen)r   watchr   s   r   <lambda>z.FileContents.watch_in_thread.<locals>.<lambda>S   s    5a8 r   T)targetdaemon)r4   r3   clearr1   Threadstartr   s   `r   r5   zFileContents.watch_in_threadJ   sS    "". "+"2"2 9#
 	%%'r   c                    | j                   y| j                  j                          | j                   j                          d| _         y)z.Stops an actively running thread if it exists.N)r4   r3   setjoinr   s    r   stop_threadzFileContents.stop_threadX   s=    ""*$$&"&r   c              #    K   	 ddl m}m}  || j                  | j
                        D ]  }|D ]  \  }}||j                  k(  r| j                  j                            y||j                  k(  s||j                  k(  sQd| _
        | j                  j                  t        |              ||f    y# t        $ r}t        d      |d}~ww xY ww)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   )Changer;   zkwatchfiles is required to watch for file changes during development. Install with `pip install watchfiles`.N)
stop_event)
watchfilesrG   r;   ImportErrorr.   r3   deletedr   modifiedaddedr   r   r   )r   rG   r;   excchangeschanger6   s          r   r;   zFileContents.watch`   s     	0 TZZD4D4DE 
	G ' 	V^^+LL%%'V__,&,,0F%)DNLL%%c$i0!4.(	
	  	9 	s.   CB7 A3C 7C7	C CCCc                t    | j                   !| j                  j                  d      | _         | j                   S )Nzutf-8)encoding)r   r.   	read_textr   s    r   r   zFileContents.__str__~   s/    >>!!ZZ1171CDN~~r   N)T)r6   zstr | pathlib.Pathr7   bool)r   r    )r   zIterator[tuple[int, str]]r   )r!   r"   r#   r$   r   r   r   rK   r   r5   rE   r;   r   r'   r   r   r   r   /   s2     SkGhG#('<r   r   )
__future__r   r*   r1   weakrefcollectionsr   typingr   psygnalr   __all__WeakValueDictionaryr	   __annotations__r   r   r'   r   r   <module>r]      sR    "      
C  G! E 
 :R Rr   