
    }tf#                    2   d dl m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	 ddl
mZmZmZ eeefZdZdZdZe d	e d
ZdZdZdZdZ	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 ddZddZ ed      dd       Zd dZddZ d!dZ!d"dZ"d#dZ#	 	 	 	 	 	 d$dZ$y)%    )annotationsN)	lru_cache)Any   )_VIRTUAL_FILESFileContentsVirtualFileContentsz(application/vnd.jupyter.widget-view+json   .z.0_anywidget_id_esm_cssa  
function render(view) {
  console.log("Dev note: No _esm defined for this widget:", view);
  let url = "https://anywidget.dev/en/getting-started/";
  view.el.innerHTML = `<p>
    <strong>Dev note</strong>:
    <a href='${url}' target='blank'>Implement an <code>_esm</code> attribute</a>
    on AnyWidget subclass <code>${view.model.get('_anywidget_id')}</code>
    to customize this widget.
  </p>`;
}
export default { render };
c                   t        |       }d}t        | t        t        f      rt	        |       D ]  \  }}t        |t
              r9|t        |       }d||<   |j                  |       |j                  g ||       Ot        |t        t        t        f      skt        |g ||||      }||us|t        |       }|||<    nt        | t              r| j                         D ]  \  }	}t        |t
              r7|t        |       }||	= |j                  |       |j                  g ||	       Mt        |t        t        t        f      sit        |g ||	||      }||us|t        |       }|||	<    nt        d|       ||S | S )a   For internal, see _remove_buffers.

    remove binary types from dicts and lists, but keep track of their paths any part of
    the dict/list that needs modification will be cloned, so the original stays
    untouched e.g. {'x': {'ar': ar}, 'y': [ar2, ar3]}, where ar/ar2/ar3 are binary types
    will result in {'x': {}, 'y': [None, None]}, [ar, ar2, ar3], [['x', 'ar'], ['y', 0],
    ['y', 1]] instead of removing elements from the list, this will make replacing the
    buffers on the js side much easier
    Nz)expected state to be a list or dict, not )type
isinstancelisttuple	enumerate_BINARY_TYPESappenddict_separate_buffersitems
ValueError)
substatepathbuffer_pathsbuffers_t_subiv_vks
             Y/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/anywidget/_util.pyr   r   (   s    
hB#D(T5M*h' 	!DAq!]+<>DQq!##JdJAJ/AdE23&q*D*!*lGLB;|#H~ DG	! 
Hd	#NN$ 	!DAq!]+<>DGq!##JdJAJ/AdE23&q*D*!*lGLB;|#H~ DG	! DXLQRR#411    c                0    g }g }t        | g ||      } | ||fS )a  Return (state_without_buffers, buffer_paths, buffers) for binary message parts.

    A binary message part is a memoryview, bytearray, or python 3 bytes object.

    Examples
    --------
    >>> ar1 = np.arange(8).reshape(4, 2)
    >>> ar2 = np.arange(100).reshape(10, 10)
    >>> state = {
            'plain': [0, 'text'],
            'x': {'ar': memoryview(ar1)},
            'y': {'shape': (10,10), 'data': memoryview(ar2)}
        }
    >>> _remove_buffers(state)
    (
        {
            'plain': [0, 'text'],
            'x': {},
            'y': {'shape': (10, 10)}
        },
        [['x', 'ar'], ['y', 'data']],
        [<memory at 0x114e7fac0>, <memory at 0x114e7fed0>]
    )
    )r   )stater   r   s      r%   remove_buffersr)   W   s+    2 L "GeRw?E,''r&   c                ^    t        ||      D ]  \  }}| }|dd D ]  }||   }	 |||d   <     y)zThe inverse of _remove_buffers.

    ...except here we modify the existing dict/lists.
    Modifying should be fine, since this is used when state comes from the wire.
    N)zip)r(   r   r   buffer_pathbufferobjkeys          r%   put_buffersr1   v   sS      #<9 &V s# 	Cc(C	%KO&r&   c                 &    dt         j                  v S )z#Determines whether in Google Colab.google.colab.output)sysmodules r&   r%   in_colabr7      s     CKK//r&   )maxsizec                 F    t         j                  d   j                          y)zEnables Google Colab's custom widget manager so third-party widgets display.

    See https://github.com/googlecolab/colabtools/issues/498#issuecomment-998308485
    r3   N)r4   r5   enable_custom_widget_managerr6   r&   r%   !enable_custom_widget_manager_oncer;      s     KK%&CCEr&   c                     t               si S t                t        j                  d   j                  j
                  } | i S t        ddd| iiiiS )a  Creates metadata dict for _repr_mimebundle_.

    If in Google Colab, enables custom widgets as a side effect
    and injects the `custom_widget_manager` metadata for more
    consistent rendering.

    See https://github.com/manzt/anywidget/issues/63#issuecomment-1427194000.
    r3   colabcustom_widget_managerurl)r7   r;   r4   r5   _widgets_installed_url_WIDGET_MIME_TYPE)r?   s    r%   get_repr_metadatarC      sS     :	%'
+++
,
5
5
D
DC
{	*AE3<)PQRRr&   c                 2    t        j                  d      dk(  S )NANYWIDGET_HMR1)osgetenvr6   r&   r%   _is_hmr_enabledrI      s    99_%,,r&   c                    d| j                   v ryd| j                   v ryt               sy	 dd l}y# t        $ r dd l}|j                  dd       Y yw xY w)	Nzsite-packagesFzdist-packagesr   zbanywidget: Live-reloading feature is disabled. To enable, please install the 'watchfiles' package.r
   )
stacklevelT)partsrI   
watchfilesImportErrorwarningswarn)r   rM   rO   s      r%   _should_start_threadrQ      sk    $**$$**$    	C 	 	
 	s   0  AAc                T   t        | t        j                        r| S t        | t              sy| j	                  d      s| j	                  d      ryd| v xs d| v }|ryt        j                  d|       du}|r1t        j                  |       j                         j                         S y)a  If possible coerce x into a pathlib.Path object.

    If a string, we handle the following cases:

    - If it's a URL, return None.
    - If it's a multi-line string, return None.
    - If it's a single-line string with a file extension, return a pathlib.Path object.
    - Otherwise, return None.

    Parameters
    ----------
    x : Any
        The object to try to coerce into a pathlib.Path object.

    Returns
    -------
    pathlib.Path | None
        A pathlib.Path object if x is a file path, otherwise None.
    Nzhttp://zhttps://
z[a-zA-Z0-9]\.[a-zA-Z0-9]+$)	r   pathlibPathstr
startswithresearchresolveabsolute)xis_multi_lineincludes_file_suffixs      r%   try_file_pathr`      s    * !W\\"a
 	||I!,,z": AI*M
 99%BAFdR||A&&(1133r&   c                    | t         v r	t         |    S t        |       }|y|}|j                         st        d|       t	        |t        |            S )z+Try to coerce x into a FileContents object.NzFile not found: )r   start_thread)r   r`   is_fileFileNotFoundErrorr   rQ   )r]   
maybe_pathr   s      r%   try_file_contentsrf      sa    Na  q!JD<<>"24& 9::)$/ r&   c                F    d|t         t        t        | di}|t               fS )z1Create a MIME bundle for a widget representation.z
text/plain)version_majorversion_minormodel_id)rB   _PROTOCOL_VERSION_MAJOR_PROTOCOL_VERSION_MINORrC   )rj   	repr_textdatas      r%   repr_mimebundlero     s2     	i44 
D "$$$r&   )
r   r   r   r   r   r   r   r   returnr   )r(   r   rp   z(tuple[Any, list[list], list[memoryview]])r(   r   r   zlist[list[str | int]]r   zlist[memoryview]rp   None)rp   bool)rp   rq   )rp   r   )r   zpathlib.Pathrp   rr   )r]   r   rp   zpathlib.Path | None)r]   r   rp   z)FileContents | VirtualFileContents | None)rj   rW   rm   rW   rp   z%tuple[dict[str, Any], dict[str, Any]])%
__future__r   rG   rU   rY   r4   	functoolsr   typingr   _file_contentsr   r   r	   
memoryview	bytearraybytesr   rB   rk   rl   _PROTOCOL_VERSION_ANYWIDGET_ID_KEY_ESM_KEY_CSS_KEY_DEFAULT_ESMr   r)   r1   r7   r;   rC   rI   rQ   r`   rf   ro   r6   r&   r%   <module>r      s!   " 	  	 
   M MY.>   ./q1H0IL # (,2,2,2-1,2<@,2,2^(>&&'& & 
	&&0
 4F FS*-:-`$%%% +%r&   