U
    9%e86                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlZ	ddl
mZmZmZ ddlmZ e	  edZG dd dZG d	d
 d
eZG dd deZdS )    N)ClassVarSequence   )OpenFileget_filesystem_classsplit_protocol)known_implementationsz
fsspec.guic                   @   s   e Zd ZU dZg Zeee  ed< g Z	eee  ed< dd Z
dd Zdejd	fd
dZdd Zdd Zdd Zejdd ZdddZdddZdS )SigSlota  Signal-slot mixin, for Panel event passing

    Include this class in a widget manager's superclasses to be able to
    register events and callbacks on Panel widgets managed by that class.

    The method ``_register`` should be called as widgets are added, and external
    code should call ``connect`` to associate callbacks.

    By default, all signals emit a DEBUG logging statement.
    signalsslotsc                 C   s   d| _ i | _i | _|   d S )NF)_ignoring_events_sigs_map_setupself r   I/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/fsspec/gui.py__init__%   s    zSigSlot.__init__c                 C   s   t j | _dS )z(Create GUI elements and register signalsN)pnZpaneZPaneBasepanelr   r   r   r   r   +   s    zSigSlot._setupvalueFc                 C   s   || j krtd| |g ||d| j|< d|dk	rFt|dt|nd|g}|| j|< |dk	rv|jj| j	|dd |rt
| |r| |t| | dS )	a  Watch the given attribute of a widget and assign it a named event

        This is normally called at the time a widget is instantiated, in the
        class which owns it.

        Parameters
        ----------
        widget : pn.layout.Panel or None
            Widget to watch. If None, an anonymous signal not associated with
            any widget.
        name : str
            Name of this event
        thing : str
            Attribute of the given widget to watch
        log_level : int
            When the signal is triggered, a logging event of the given level
            will be fired in the dfviz logger.
        auto : bool
            If True, automatically connects with a method in this class of the
            same name.
        z*Attempt to assign an undeclared signal: %s)widget	callbacksthinglog-NnamenoneT)Zonlychanged)r
   
ValueErrorr   joingetattrstrr   paramwatch_signalhasattrconnect)r   r   r   r   	log_levelautownr   r   r   	_register0   s"    

zSigSlot._registerc              	   O   s6   z| j j||W S  ttfk
r0   tdY nX dS )z!Display in a notebook or a serverz)Panel does not seem to be set up properlyN)r   _repr_mimebundle_r   AttributeErrorNotImplementedErrorr   argskwargsr   r   r   r,   \   s    zSigSlot._repr_mimebundle_c                 C   s   | j | d | dS )a  Associate call back with given event

        The callback must be a function which takes the "new" value of the
        watched attribute as the only parameter. If the callback return False,
        this cancels any further processing of the given event.

        Alternatively, the callback can be a string, in which case it means
        emitting the correspondingly-named event (i.e., connect to self)
        r   N)r   append)r   signalZslotr   r   r   r'   c   s    
zSigSlot.connectc                 C   sL   | j sHd|jj|jg}|| jkrH| j| | jkrH| | j| |j dS )zThis is called by a an action on a widget

        Within an self.ignore_events context, nothing happens.

        Tests can execute this method by directly changing the values of
        widget components.
        r   N)r   r    objr   r   r   _emitnew)r   eventr*   r   r   r   r%   o   s    zSigSlot._signalc                 c   s   d| _ z
dV  W 5 d| _ X dS )zjTemporarily turn off events processing in this instance

        (does not propagate to children)
        TFN)r   r   r   r   r   ignore_events|   s    
zSigSlot.ignore_eventsNc                 C   s   t | j| d d|| | j| d D ]j}t|trF| | q,z||}|dkr^W  qW q, tk
r } zt d||f  W 5 d}~X Y q,X q,dS )zAn event happened, call its callbacks

        This method can be used in tests to simulate message passing without
        directly changing visual elements.

        Calling of callbacks will halt whenever one returns False.
        r   z{}: {}r   Fz6Exception (%s) while executing callback for signal: %sN)	loggerr   r   format
isinstancer"   r5   	Exception	exception)r   sigr   callbackreter   r   r   r5      s    

zSigSlot._emitc                 C   s   | j j|dd | S )z<Open a new browser tab and display this instance's interfaceF)threadsverbose)r   show)r   rB   r   r   r   rD      s    zSigSlot.show)N)F)__name__
__module____qualname____doc__r
   r   r   r"   __annotations__r   r   r   loggingDEBUGr+   r,   r'   r%   
contextlibcontextmanagerr8   r5   rD   r   r   r   r   r	      s    
  
,

r	   c                       sv   e Zd ZdZddgZdddddgZ fd	d
Zdd Z fddZdd Z	dd Z
dd Zedd Zdd Z  ZS )SingleSelectzCA multiselect which only allows you to select one item for an event	_selectedselectedset_optionsset_selectionaddclearselectc                    s   || _ t   d S N)r1   superr   )r   r1   	__class__r   r   r      s    zSingleSelect.__init__c                 C   s@   t jjf | j| _| | jdd | d d | d| j d S )NrO   r   rP   )r   widgetsZMultiSelectr1   r   r+   r'   
select_oner   r   r   r   r      s    zSingleSelect._setupc                    s   t  j|| d S rV   )rW   r%   r/   rX   r   r   r%      s    zSingleSelect._signalc              	   G   sJ   |   ( | jjr | jjd gng }|| j_W 5 Q R X | d| jj d S )NrP   )r8   r   r   r5   )r   _valr   r   r   r[      s    
zSingleSelect.select_onec                 C   s   || j _d S rV   r   options)r   r`   r   r   r   rQ      s    zSingleSelect.set_optionsc                 C   s   g | j _d S rV   r_   r   r   r   r   rT      s    zSingleSelect.clearc                 C   s   | j jS rV   r   r   r   r   r   r   r      s    zSingleSelect.valuec                 C   s   |g| j _d S rV   ra   )r   Z	selectionr   r   r   rR      s    zSingleSelect.set_selection)rE   rF   rG   rH   r
   r   r   r   r%   r[   rQ   rT   propertyr   rR   __classcell__r   r   rX   r   rN      s   
rN   c                       s   e Zd ZdZdddddddgZd	d
gZd' fdd	Zdd Zd(ddZe	dd Z
e	dd Ze	dd Zd)ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z  ZS )*FileSelectorzPanel-based graphical file selector widget

    Instances of this widget are interactive and can be displayed in jupyter by having
    them as the output of a cell,  or in a separate browser tab using ``.show()``.
    protocol_changedselection_changeddirectory_enteredhome_clicked
up_clicked
go_clickedfilters_changedset_filtersZgo_homeNc                    sb   |rt |\| _}ndt  | _}|| _|p0d| _|| _dd |pFg D | _d| _t	 
  dS )a  

        Parameters
        ----------
        url : str (optional)
            Initial value of the URL to populate the dialog; should include protocol
        filters : list(str) (optional)
            File endings to include in the listings. If not included, all files are
            allowed. Does not affect directories.
            If given, the endings will appear as checkboxes in the interface
        ignore : list(str) (optional)
            Regex(s) of file basename patterns to ignore, e.g., "\." for typical
            hidden files on posix
        kwargs : dict (optional)
            To pass to file system instance
        filez{}c                 S   s   g | ]}t |qS r   )recompile.0ir   r   r   
<listcomp>   s     z)FileSelector.__init__.<locals>.<listcomp>N)r   init_protocolosgetcwdinit_urlinit_kwargsfiltersignore_fsrW   r   )r   urlry   rz   r1   rX   r   r   r      s    
zFileSelector.__init__c              
   C   s  t jjd| jdddd| _t jjtt| jddd| _	t jjd	| j
dd
| _t jjdddd| _tdd| _t jjddddd| _t jjddddd| _| j| j	ddd | j| jdddd | j| jdddd | j| jdddd | d d | jd| j | d d | j	j| _| j| _t jjg g ddd d!| _| j| jd"dd t t | j	| jt | j| j| j| j| j| jj| _| | j |    d S )#Nr|   endZstretch_widthmax)r   r   alignZsizing_modewidth_policyprotocolcenter)r`   r   r   r   r1   )r   r   r   u   ⇨-   )r   r   width
   )sizeu   🏠(      )r   r   heightr   u   ‹re   T)r)   rj   Zclicksri   rh   rf   rP   rg   Fmin)r   r`   inliner   r   rk   )!r   rZ   Z	TextInputrw   r|   ZSelectsortedr   rt   r   rx   r1   ZButtongorN   mainhomeupr+   r'   rf   r   prev_protocolstorage_optionsprev_kwargsZCheckBoxGroup
filter_selColumnRowr   rl   ry   rj   r   r   r   r   r      s\      
    zFileSelector._setupc                 C   s0   || _ |r|| j_|| j_ng | j_g | j_d S rV   )ry   r   r`   r   )r   ry   r   r   r   rl   (  s    
zFileSelector.set_filtersc                 C   s   t | jjpi S )z'Value of the kwargs box as a dictionary)astliteral_evalr1   r   r   r   r   r   r   1  s    zFileSelector.storage_optionsc                 C   s*   | j dkr$t| jj}|f | j| _ | j S )zCurrent filesystem instanceN)r{   r   r   r   r   )r   clsr   r   r   fs6  s    
zFileSelector.fsc                 C   s$   | j jr | jjd | j jd  S dS )zURL of currently selected itemz://r   N)r   r   r   r   r   r   r   urlpath>  s    zFileSelector.urlpathrbc                 C   s&   | j dkrtdt| j| j |||S )a  Create OpenFile instance for the currently selected item

        For example, in a notebook you might do something like

        .. code-block::

            [ ]: sel = FileSelector(); sel

            # user selects their file

            [ ]: with sel.open_file('rb') as f:
            ...      out = f.read()

        Parameters
        ----------
        mode: str (optional)
            Open mode for the file.
        compression: str (optional)
            The interact with the file as compressed. Set to 'infer' to guess
            compression from the file ending
        encoding: str (optional)
            If using text mode, use this encoding; defaults to UTF8.
        NzNo file selected)r   r   r   r   )r   modecompressionencodingr   r   r   	open_fileG  s    
zFileSelector.open_filec                 C   s   || _ |   d S rV   )ry   rj   )r   valuesr   r   r   rk   c  s    zFileSelector.filters_changedc                 G   s:   | j d krd S | j| j r.| j| j | j_|   d S rV   )r   r   isdirZ_strip_protocolr|   r   rj   r   r]   r   r   r   rf   g  s
    
zFileSelector.selection_changedc                    s    j  jjks j jkr2d  _ jj _  j _t jj j	jdddd d} fdd|D }dd	 |D }d
d	 |D } j
r fdd	| D } jtf || d S )NT)detailc                 S   s   | d S )Nr   r   )xr   r   r   <lambda>w      z)FileSelector.go_clicked.<locals>.<lambda>)keyc                    s(   g | ]  t  fd djD s qS )c                 3   s(   | ] }|  d  ddd V  qdS )r   /r   r\   N)matchrsplitrp   lr   r   	<genexpr>|  s     z5FileSelector.go_clicked.<locals>.<listcomp>.<genexpr>)anyrz   )rq   r   r   r   rs   y  s   z+FileSelector.go_clicked.<locals>.<listcomp>c                 S   s6   i | ].}|d  dkrd|d  ddd  |d qS )type	directoryu   📁 r   r   r   r\   r   rq   or   r   r   
<dictcomp>~  s    z+FileSelector.go_clicked.<locals>.<dictcomp>c                 S   s6   i | ].}|d  dkrd|d  ddd  |d qS )r   rm   u   📄 r   r   r   r\   r   r   r   r   r   r     s    c                    s.   i | ]&\} t  fd djD r| qS )c                 3   s   | ]}  |V  qd S rV   )endswith)rq   extvr   r   r     s     z5FileSelector.go_clicked.<locals>.<dictcomp>.<genexpr>)r   ry   )rq   kr   r   r   r     s    )r   r   r   r   r   r{   r   r   Zlsr|   ry   itemsr   rQ   dict)r   r]   Zlistingfoldersfilesr   r   r   rj   n  s2    

 

zFileSelector.go_clickedc                 G   s   d | _ g | j_d| j_d S )N )r{   r   r`   r|   r   r   r   r   r   re     s    zFileSelector.protocol_changedc                 G   s*   | j | j_| j| j_| j| j_|   d S rV   )rt   r   r   rx   r1   rw   r|   rj   r   r   r   r   rh     s    


zFileSelector.home_clickedc                 G   s    | j | jj| j_|   d S rV   )r   _parentr|   r   rj   r   r   r   r   ri     s    zFileSelector.up_clicked)NNNN)N)r   NN)rE   rF   rG   rH   r
   r   r   r   rl   rb   r   r   r   r   rk   rf   rj   re   rh   ri   rc   r   r   rX   r   rd      s4   	-
	



"rd   )r   rL   rJ   ru   rn   typingr   r   r   r   corer   r   r   registryr   	extension	getLoggerr9   r	   rN   rd   r   r   r   r   <module>   s   
 '