
    +d                     J    d dl Zd dlZd dlmZ ddlmZ  G d de          ZdS )    Nremake_instance   )BaseSelectorc                   j     e Zd ZdZg Zed             Zd fd	Zd Zd Z	d Z
dd	Zd
 Zd Zd Z xZS )CatSelectoraW  
    The cat selector takes a variety of inputs such as a catalog instance,
    a path to a catalog, or a list of either of those.

    Once the cat selector is populated with these options, the user can
    select which catalog(s) are of interest. These catalogs are stored on
    the ``selected`` property of the class.

    Parameters
    ----------
    cats: list of catalogs, opt
        catalogs used to initalize, can be provided as objects or
        urls pointing to local or remote catalogs.
    done_callback: func, opt
        called when the object's main job has completed. In this case,
        selecting catalog(s).

    Attributes
    ----------
    selected: list of cats
        list of selected cats
    items: list of cats
        list of all the catalog values represented in widget
    labels: list of str
        list of labels for all the catalog represented in widget
    options: dict
        dict of widget labels and values (same as `dict(zip(self.labels, self.values))`)
    children: list of panel objects
        children that will be used to populate the panel when visible
    panel: panel layout object
        instance of a panel layout (row or column) that contains children
        when visible
    watchers: list of param watchers
        watchers that are set on children - cleaned up when visible
        is set to false.
    c                 X    t          |t                    rt          j        |          }|S )z!Function to run on each cat input)
isinstancestrintakeopen_catalog)clscats     ?lib/python3.11/site-packages/intake/interface/catalog/select.py
preprocesszCatSelector.preprocess8   s+     c3 	+%c**C
    Nc                     t          j        dd          | _        t           j                            ddd          | _        || _         t                      j        d	i | ||nt          j
        g| _        dS )
zSet cats to initialize the class.

        The order of the calls in this method matters and is different
        from the order in other panel init methods because the top level
        gui class needs to be able to watch these widgets.
        zSelect Catalogr   )namemargin	      min)size	min_widthwidth_policyN )pnColumnpanelwidgetsMultiSelectwidgetdone_callbacksuper__init__r   r   items)selfcatsr#   kwargs	__class__s       r   r%   zCatSelector.__init__?   sx     Y$4Q???
j,,!sQV,WW*""6"""!?TTFJ<


r   c                     t           j                            dd          }| j        j                            | j        d          g| _        || j        g| _        d S )Nz#### Catalogs(   )
max_heightvalue)	r   paneMarkdownr"   paramwatchcallbackwatcherschildren)r'   labels     r   setupzCatSelector.setupM   sS      R @@K##DM7;;
 ,r   c                     |                      |j                   | j        r|                     |j                   d S d S N)expand_nestednewr#   )r'   events     r   r3   zCatSelector.callbackU   sI    59%%% 	*uy)))))	* 	*r   c                 6   d}d}d }t          |          dk    rdS |d         t          | j                                                  }t	          fd|D                       }t	          fdt          |          D                       }||v r#|                    |          d          | d| }n|} |          }	t          |	          D ]-\  }
}|                    ||
z   d	z   | d|j         |f           .t          |          | j
        _        dS )
z$Populate widget with nested catalogsu   │u	   └──c                     g }| j                                         D ]Q\  }}	 |                                d         dk    r|                     |                       B# t          $ r Y Nw xY w|S )N	containercatalog)_entriesr&   describeappendIOError)parentoutkes       r   get_childrenz/CatSelector.expand_nested.<locals>.get_children_   s    C--//  1zz||K0I= (

1133   D Js   ;A
A+*A+r   Nc              3   .   K   | ]\  }}|k    |V  d S r9   r   ).0rG   vr   s      r   	<genexpr>z,CatSelector.expand_nested.<locals>.<genexpr>o   s0      22$!Qc2A222222r   c              3   4   K   | ]\  }\  }}|k    |V  d S r9   r   )rK   irG   rL   r   s       r   rM   z,CatSelector.expand_nested.<locals>.<genexpr>p   s4      CC91fq!!s(CQCCCCCCr       )lenlistoptionsr&   next	enumeratesplitinsertr   dictr"   )r'   r(   downrightrI   oldr   indexprefixr5   rO   childr   s               @r   r:   zCatSelector.expand_nestedZ   sU   		 		 		 t99> 	F1g4<%%''((2222#22222CCCC	#CCCCCD= 	

5))!,<d<<U<<FFF<$$!(++ 	I 	IHAuJJuqy1}&'?'?5:'?'?&GHHHH"3iir   
   c                   	
 g t                      
|}t          | j        j                                                  }d |D             }d |D             	|}t          |          dk    ry|dk    rs|D ] }|j        |v r                    |           !
                    |          
fd|D             }	
fdD             }|dz  }t          |          dk    r|dk    s| 	                               dS )z
        Collapse any items that are nested under cats.
        `max_nestedness` acts as a fail-safe to prevent infinite looping.
        c                 2    g | ]}t          |d           |S r   )getattrrK   r   s     r   
<listcomp>z/CatSelector.collapse_nested.<locals>.<listcomp>   s'    HHH#U(;(;H#HHHr   c                     h | ]	}|j         
S r   rc   re   s     r   	<setcomp>z.CatSelector.collapse_nested.<locals>.<setcomp>   s    ---s37---r   r   c                     g | ]}|v|	S r   r   )rK   r   r5   s     r   rf   z/CatSelector.collapse_nested.<locals>.<listcomp>   s#    CCCcs(/BCcCCCr   c                 "    h | ]}|z
  v 	|S r   r   )rK   cparentsremoveds     r   rh   z.CatSelector.collapse_nested.<locals>.<setcomp>   s(     O O OqWw=N8N O O O Or   rQ   N)
setrS   r"   rT   valuesrR   r   rC   unionremove)r'   r(   max_nestedness
nestednessr\   nestedparents_to_remover   r5   rl   rm   s           @@@r   collapse_nestedzCatSelector.collapse_nested{   sE   
 %%#
4;&--//00HHHHH--f--- #$$q( 	Z!^ 	 ) )7// )OOC(((mm$566GCCCCVCCCF O O O O OH O O O!OJ #$$q( 	Z!^ 	 	Hr   c                 n    |                      | j                   |                     | j                   dS )zRemove the selected catalog - allow the passing of arbitrary
        args so that buttons work. Also remove any nested catalogs.N)rv   selectedrq   )r'   argss     r   remove_selectedzCatSelector.remove_selected   s4     	T]+++DM"""""r   c                       j          j        d  j        D              fd j                                        D             dS )z)Serialize the current state of the objectc                 6    g | ]}|                                 S r   )__getstate__re   s     r   rf   z,CatSelector.__getstate__.<locals>.<listcomp>   s$    >>>CS%%''>>>r   c                 ,    g | ]\  }}|j         v |S r   )rx   )rK   rG   rL   r'   s      r   rf   z,CatSelector.__getstate__.<locals>.<listcomp>   s)    RRRtq!qDM?QRRRRr   )visiblelabelsr(   rx   )r   r   r&   rT   )r'   s   `r   r}   zCatSelector.__getstate__   sV     |k>>4:>>>RRRRt|'9'9';';RRR	
 
 	
r   c                     |d         |d         }}d t          ||          D             | j        _        |                    dg           | _        |                    dd          | _        | S )zSet the current state of the object from the serialized version.
        Works inplace. See ``__getstate__`` to get serialized version and
        ``from_state`` to create a new object.r(   r   c                 4    i | ]\  }}|t          |          S r   r   )rK   r6   r   s      r   
<dictcomp>z,CatSelector.__setstate__.<locals>.<dictcomp>   s&    ___zucuoc&:&:___r   rx   r   T)zipr"   rT   getrx   r   )r'   stater(   r   s       r   __setstate__zCatSelector.__setstate__   sh     V}eHof__SQWY]M^M^___		*b11yyD11r   )NN)r`   )__name__
__module____qualname____doc__r5   classmethodr   r%   r7   r3   r:   rv   rz   r}   r   __classcell__)r*   s   @r   r   r      s        # #J H  [@ @ @ @ @ @- - -* * *
( ( (B   .# # #
 
 
      r   r   )r   r   r   intake.utilsr   baser   r   r   r   r   <module>r      s~         ( ( ( ( ( (      Y Y Y Y Y, Y Y Y Y Yr   