
    mjM                     @    d dl Z d dlmZ d ZddZd Z G d d      Zy)	    N)Pathc                 P   t        j                         }t        d      }t        |      }t	        | dd      5 }|j                  |      x}r(|j                  |d|        |j                  |      x}r(ddd       |j                         S # 1 sw Y   |j                         S xY w)zReturn the hex digest of a filei   rbr   )	bufferingN)hashlibsha256	bytearray
memoryviewopenreadintoupdate	hexdigest)	file_pathhbmvfns         J/DATA/.local/lib/python3.12/site-packages/trame_client/utils/web_module.pyfile_digestr      s    A*A	AB	i	+q::b>!a!HHRV ::b>!a! 
, ;;= 
, ;;=s   ;BB%c                     t        |       } |t        |       }t        |      |kD  r|d| }| j                  | j                   d| | j
                         }|j                  | j                                |S )zEWrite a new file sibbling to file_path with a digest hash in his nameN-)r   r   len	with_namestemsuffixwrite_bytes
read_bytes)r   digestdigest_sizeoutput_files       r   file_with_digestr"      s|    YI~Y'
6{[ % %%(8&)BRBRAS&TUKI0023    c                     | j                         |D cg c]  }t        |      j                          }}t        fd|D              S c c}w )z7Helper to check that *other_paths are nested under pathc              3   |   K   | ]3  }j                   d t        |j                          |j                   k(   5 y wN)partsr   ).0baseabs_paths     r   	<genexpr>z!is_relative_to.<locals>.<genexpr>(   s2      DSD(TZZ)TZZ7Os   9<)resolver   all)pathother_pathsotherabs_other_pathsr*   s       @r   is_relative_tor2   !   sS     ||~H:EF+tE{**,+OF  DS   Gs    Ac                   F    e Zd ZdZd
dZd Zd Zd Zd Zd Z	e
d	        Zy)	WebModulez{Helper class to create/define a module while dynamically computing hash
    to properly prevent invalid browser cache.
    Nc                     || _         g | _        i | _        g | _        g | _        g | _        || j                  j                  |       y y r&   )_digest_size_serving_entries_serve_vue_use_styles_scriptsappend)selfvue_user    s      r   __init__zWebModule.__init__2   sK    ' "MM  ) r#   c                 R   t        |      j                         }|j                         st        d| d      | j                  D ]S  }|\  }}t        ||      s| j                  dkD  rt        || j                        }|j                  |      }| d| c S  t        d|       )NzFile z does not existr   )r    /zNo parent found for )	r   r,   exists
ValueErrorr7   r2   r6   r"   relative_to)r=   r   entryfs_pathwww_pathr_paths         r   	_add_filezWebModule._add_file=   s    O++-	!uYK?@@**E %GXi1$$q( 0!t/@/@!I #..w7"1VH-- + /	{;<<r#   c                 <   t        |      j                         }|j                         st        d| d      || j                  v r!t        d| d| j                  |    d|       | j
                  j                  ||f       t        |      | j                  |<   y)z9Register a new directory to serve with its alias endpointz
Directory z+ does not exist - Won't be able to serve itz
Entry for z is already set for zand want to override with N)r   r,   rB   rC   r8   r7   r<   str)r=   directory_to_servewww_base_namerF   s       r   serve_directoryzWebModule.serve_directoryO   s    )*224~~WI%PQ  DKK']O+?M@Z?[,WI7 
 	$$g}%=>%(\M"r#   c                 X    | j                   j                  | j                  |             y)zWAdd a script file to serve while computing unique name and proper http path internally.N)r;   r<   rI   r=   r   s     r   add_script_filezWebModule.add_script_file_   s    T^^I67r#   c                 X    | j                   j                  | j                  |             y)zTAdd a css file to serve while computing unique name and proper http path internally.N)r:   r<   rI   rP   s     r   add_style_filezWebModule.add_style_filec   s    DNN956r#   c                 :    | j                   j                  |       y)z Register a vue plugin expressionN)r9   r<   )r=   r>   s     r   add_vue_usezWebModule.add_vue_useg   s    W%r#   c                 p    t        | j                  | j                  | j                  | j                        S )z2Return the generated trame module as a dictionary.)servescriptsstylesr>   )dictr8   r;   r:   r9   )r=   s    r   modulezWebModule.modulek   s-     ++MM<<MM	
 	
r#   )N   )__name__
__module____qualname____doc__r?   rI   rN   rQ   rS   rU   propertyr[    r#   r   r4   r4   -   s9    	*=$2 87& 
 
r#   r4   )N(   )r   pathlibr   r   r"   r2   r4   rb   r#   r   <module>re      s&     "	F
 F
r#   