o
    tf#                     @  s
  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dZd=ddZd>d d!Zd?d#d$Zedd%d@d&d'ZdAd(d)Zd?d*d+Z dBd-d.Z!dCd1d2Z"dDd4d5Z#dEd:d;Z$dS )F    )annotationsN)	lru_cache)Any   )_VIRTUAL_FILESFileContentsVirtualFileContentsz(application/vnd.jupyter.widget-view+json   .z.0Z_anywidget_idZ_esmZ_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 };
substater   pathlistbuffer_pathsbuffersreturnc           
      C  st  t | }d}t| ttfrZt| D ]G\}}t|tr5|du r"t| }d||< || |g || qt|tttfrXt|g ||||}||urX|du rTt| }|||< qnXt| tr| 	 D ]F\}	}t|tr|du rtt| }||	= || |g ||	 qct|tttfrt|g ||	||}||ur|du rt| }|||	< qcnt
d| |dur|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
isinstancer   tuple	enumerate_BINARY_TYPESappenddict_separate_buffersitems
ValueError)
r   r   r   r   Z_t_subiv_vk r    X/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/anywidget/_util.pyr   (   sH   




r   state(tuple[Any, list[list], list[memoryview]]c                 C  s    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   )r"   r   r   r    r    r!   remove_buffersW   s   
r$   r   list[list[str | int]]list[memoryview]Nonec                 C  sB   t ||D ]\}}| }|dd D ]}|| }q|||d < qdS )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   Zbuffer_pathbufferobjkeyr    r    r!   put_buffersv   s   

r-   boolc                   C  s
   dt jv S )z#Determines whether in Google Colab.google.colab.output)sysmodulesr    r    r    r!   in_colab   s   
r2   )maxsizec                   C  s   t jd   dS )zEnables Google Colab's custom widget manager so third-party widgets display.

    See https://github.com/googlecolab/colabtools/issues/498#issuecomment-998308485
    r/   N)r0   r1   Zenable_custom_widget_managerr    r    r    r!   !enable_custom_widget_manager_once   s   r4   c                  C  s>   t  si S t  tjd jj} | du r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.
    r/   NZcolabZcustom_widget_managerurl)r2   r4   r0   r1   Z_widgetsZ_installed_url_WIDGET_MIME_TYPE)r5   r    r    r!   get_repr_metadata   s   	r7   c                   C  s   t ddkS )NZANYWIDGET_HMR1)osgetenvr    r    r    r!   _is_hmr_enabled   s   r;   pathlib.Pathc                 C  s`   d| j v rdS d| j v rdS t sdS zdd l}W dS  ty/   dd l}|jddd Y dS w )	Nzsite-packagesFzdist-packagesr   zbanywidget: Live-reloading feature is disabled. To enable, please install the 'watchfiles' package.r	   )
stacklevelT)partsr;   
watchfilesImportErrorwarningswarn)r   r?   rA   r    r    r!   _should_start_thread   s"   


rC   xpathlib.Path | Nonec                 C  sx   t | tjr| S t | tsdS | ds| drdS d| v p"d| v }|r'dS td| du}|r:t|   S dS )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)rD   Zis_multi_lineZincludes_file_suffixr    r    r!   try_file_path   s   
rP   )FileContents | VirtualFileContents | Nonec                 C  sN   | t v rt |  S t| }|du rdS |}| std| t|t|dS )z+Try to coerce x into a FileContents object.NzFile not found: )r   Zstart_thread)r   rP   is_fileFileNotFoundErrorr   rC   )rD   Z
maybe_pathr   r    r    r!   try_file_contents   s   rT   model_idrJ   	repr_text%tuple[dict[str, Any], dict[str, Any]]c                 C  s   d|t tt| di}|t fS )z1Create a MIME bundle for a widget representation.z
text/plain)Zversion_majorZversion_minorrU   )r6   _PROTOCOL_VERSION_MAJOR_PROTOCOL_VERSION_MINORr7   )rU   rV   datar    r    r!   repr_mimebundle  s   
r[   )
r   r   r   r   r   r   r   r   r   r   )r"   r   r   r#   )r"   r   r   r%   r   r&   r   r'   )r   r.   )r   r'   )r   r   )r   r<   r   r.   )rD   r   r   rE   )rD   r   r   rQ   )rU   rJ   rV   rJ   r   rW   )%
__future__r   r9   rH   rL   r0   	functoolsr   typingr   Z_file_contentsr   r   r   
memoryview	bytearraybytesr   r6   rX   rY   Z_PROTOCOL_VERSIONZ_ANYWIDGET_ID_KEYZ_ESM_KEYZ_CSS_KEYZ_DEFAULT_ESMr   r$   r-   r2   r4   r7   r;   rC   rP   rT   r[   r    r    r    r!   <module>   s:    


/






0