
    i                         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	  e       Z
dZ G d d      Zdeeef   fd	Zdefd
Zy)    N)Dict)Console)get_is_binary_installationservicesc                   J    e Zd ZddededefdZd Zd Zed        Z	d	efd
Z
y)LazySearchModuleNmodule_nameindiceuse_forc                 J    || _         || _        d| _        d| _        || _        y)z
        Lazy loader for a search module.

        Args:
            module_name: Name of the site module (e.g., 'streamingcommunity')
            indice: Sort index for the module
            use_for: Content types this module supports
        N)r	   r
   _module_search_func_use_for)selfr	   r
   r   s       b/home/nidran/Documenti/script/streaming-community/StreamingCommunity/services/_base/site_loader.py__init__zLazySearchModule.__init__   s)     '     c           	      h   | j                   f	 t        j                  dt         d| j                         | _         t        | j                   d      | _        t        | j                   d      | _        yy# t        $ r4}t        j                  d| j                   dt        |               d}~ww xY w)z Load the module on first access.NzStreamingCommunity..search_useForz[red]Failed to load module : )r   	importlibimport_modulefolder_namer	   getattrr   r   	Exceptionconsoleprintstr)r   es     r   _load_modulezLazySearchModule._load_module'   s    <<(66)+a8H8H7IJ  %,DLL($C! 'i @     ;D<L<L;MRPSTUPVxXYs   A$A4 4	B1=/B,,B1c                 F    | j                           | j                  |i |S )a  Execute search function when called.
        
        Args:
            *args: Positional arguments to pass to search function
            **kwargs: Keyword arguments to pass to search function
            
        Returns:
            Result from the search function
        )r"   r   )r   argskwargss      r   __call__zLazySearchModule.__call__4   s(     	 t  $1&11r   c                 R    | j                   | j                          | j                   S )zGet _useFor attribute (loads module if needed).
        
        Returns:
            List of content types this module supports
        )r   r"   )r   s    r   r   zLazySearchModule.use_forA   s$     == }}r   indexc                 H    |dk(  r| S |dk(  r| j                   S t        d      )a  Support tuple unpacking: func, use_for = loaded_functions['name'].
        
        Args:
            index: Index to access (0 for function, 1 for use_for)
            
        Returns:
            Self (as callable) for index 0, use_for for index 1
        r      z.LazySearchModule only supports indices 0 and 1)r   
IndexError)r   r(   s     r   __getitem__zLazySearchModule.__getitem__M   s-     A:KaZ<<IJJr   )N)__name__
__module____qualname__r    intr   r"   r&   propertyr   r,    r   r   r   r      sH     C    s  2 	 	K Kr   r   returnc            
         i } t               r4t        j                  j                  t        j
                  dt              }n@t        j                  j                  t        j                  j                  t                    }g }t        j                  t        j                  j                  |dd            D ]d  }t        j                  j                  t        j                  j                  |            }	 t        |dd      5 }|j                         }ddd       d}d}j                  d      D ]  }	|	j                         }	|sO|	j                  d	      s|	j                  d
      r-	 t!        |	j                  d      d   j                               }ne|sc|	j                  d      s|	j                  d      rA	 |	j                  d      d   j                         j                  d      j                  d      }|| n ||j'                  |||f       g t1        |d       }t3        |      D ]  \  }\  }}}t5        |||      | | d<   ||k(  r#t        j                  j                  ||d      }	 t        |dd      5 }|j7                         }ddd       t        |dd      5 }D ]g  }	|	j                         j                  d	      s|	j                         j                  d
      r|j9                  d| d       W|j9                  |	       i 	 ddd        | S # 1 sw Y   xY w# t"        t$        f$ r Y Nw xY w# t$        $ r Y ^w xY w# t(        $ r/}
t*        j-                  d| dt/        |
              Y d}
~
d}
~
ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# t(        $ r/}
t*        j-                  d| dt/        |
              Y d}
~
d}
~
ww xY w)zLoad and return all available search functions from site modules.
    
    Returns:
        Dictionary mapping '{module_name}_search' to LazySearchModule instances
    StreamingCommunity*z__init__.pyrzutf-8)encodingN
zindice =zindice==r*   z	_useFor =z_useFor="'z.[yellow]Warning: Could not read metadata from r   c                     | d   S )Nr*   r2   )xs    r   <lambda>z'load_search_functions.<locals>.<lambda>   s
    AaD r   )key_searchwz	indice = z,[yellow]Warning: Could not update indice in )r   ospathjoinsys_MEIPASSr   dirname__file__globbasenameopenreadsplitstrip
startswithr0   
ValueErrorr+   appendr   r   r   r    sorted	enumerater   	readlineswrite)loaded_functions	base_pathmodules_metadata	init_filer	   fcontentr
   r   liner!   sorted_modules
new_indice
old_indiceliness                  r   load_search_functionsrb   ^   s     "#GGLL/C[Q	GGOOBGGOOH$=>	YYrww||IsMJK d	gg&&rwwy'AB	diw7 #1&&(# FGd+ zz|4??:#>$//R[B\!$TZZ_Q%7%=%=%?!@ !dook&BdooV`Fa"&**S/!"4":":"<"B"B3"G"M"Mc"R %'*=  ! ''fg(FG9dD ,.AN:CN:S b6
6[*g4D[R\^e4fK=01 #GGLLKG		biw7 &1&iw7 &1! &Dzz|..z:djjl>U>UV_>`)J<r :;	&&b* g# # '
3 
 &   	dMMJ;-WYZ]^_Z`Yabcc	d& && &  	bMMHUWX[\]X^W_`aa	bs   8ML,AM,+L9%M=A M=M MM=N2NN22A-N& N2,L6	1M9MMMM	MMMM	N($NNN#	N2&N/	+N22	O*;$O%%O*c                      t         S )znGet the folder name where site modules are located.
    
    Returns:
        The folder name as a string
    )r   r2   r   r   get_folder_namerd      s
     r   )rC   rF   rJ   r   typingr   rich.consoler   StreamingCommunity.setupr   r   r   r   r    rb   rd   r2   r   r   <module>rh      sd    
 
    ! @ )DK DKNGtC)9$9: GT r   