o
    i                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlmZ edhZ	de
eee
e f  fddZd	e
e deee
e f fd
dZdededdfddZdededB fddZdedeed ef fddZdedeee
e f fddZdS )    N)Path)Literal)normalize_tagscustom_nodesreturnc                  C   sJ   g } t j D ]\}}|tv rq|d |d }}|r"| ||f q| S )a  Build list of (folder_name, base_paths[]) for all model locations.

    Includes every category registered in folder_names_and_paths,
    regardless of whether its paths are under the main models_dir,
    but excludes non-model entries like custom_nodes.
    r      )folder_pathsfolder_names_and_pathsitems_NON_MODEL_FOLDER_NAMESappend)targetsnamevaluespaths_exts r   </mnt/c/Users/fbmor/ComfyUI/app/assets/services/path_utils.pyget_comfy_models_folders   s   r   tagsc                 C   sL  | st d| d  }|dkrRt| dk rt dztj| d  d }W n ty6   t d| d  dw |sCt d	| d  dtj|d }| dd
 }n0|dkretjt	 }| dd
 }n|dkrxtjt
 }| dd
 }n
t d| d  dtddtjf}|D ]}|dv s|t|@ rt dq||r|fS g fS )z5Validates and maps tags -> (base_dir, subdirs_for_fs)ztags must not be emptyr   models   z*at least two tags required for model assetr   zunknown model category ''z&no base path configured for category 'Ninputoutputzunknown root tag 'z*'; expected 'models', 'input', or 'output'/\.z..zinvalid path component in tags)
ValueErrorlowerlenr   r	   KeyErrorospathabspathget_input_directoryget_output_directory	frozensetsepset)r   rootbasesbase_dirraw_subdirs
_sep_charsir   r   r   resolve_destination_from_tags   s8   r1   	candidatebasec                 C   s6   t tj| }t tj|}||stdd S )Nz"destination escapes base directory)r   r#   r$   r%   is_relative_tor   )r2   r3   cand_absbase_absr   r   r   validate_path_within_base=   s
   
r7   	file_pathc                    s   zt | \}}W n
 ty   Y dS w t|  fdd jD }|s%dS |dkr?t|dkr5|dd n|d g}d|S d|S )aG  
    Return the model's path relative to the last well-known folder (the model category),
    using forward slashes, eg:
      /.../models/checkpoints/flux/123/flux.safetensors -> "flux/123/flux.safetensors"
      /.../models/text_encoders/clip_g.safetensors -> "clip_g.safetensors"

    For non-model paths, returns None.
    Nc                        g | ]}|d d j fvr|qS r   anchor).0segpr   r   
<listcomp>S   s     z-compute_relative_filename.<locals>.<listcomp>r   r   r   r   )$get_asset_category_and_relative_pathr   r   partsr!   join)r8   root_categoryrel_pathrB   insider   r>   r   compute_relative_filenameD   s   	"

rG   )r   r   tempr   c                 C   sd  t j| }dtdtdtfdd}dtdtdtfdd}t jt }|||r0d|||fS t jt }|||rDd	|||fS t jt }|||rXd
|||fS d}t	 D ]-\}}	|	D ]&}
t j|
}|||sqqct
|||||f}|du s|d |d kr|}qcq]|dur|\}}}t j||}dt jt jt j|t jfS td|  )a  Determine which root category a file path belongs to.

    Categories:
      - 'input': under folder_paths.get_input_directory()
      - 'output': under folder_paths.get_output_directory()
      - 'temp': under folder_paths.get_temp_directory()
      - 'models': under any base path from get_comfy_models_folders()

    Returns:
        (root_category, relative_path_inside_that_root)

    Raises:
        ValueError: path does not belong to any known root.
    childparentr   c                 S   s   t | |S N)r   r4   rI   rJ   r   r   r   _check_is_withinq   s   z>get_asset_category_and_relative_path.<locals>._check_is_withinc              	   S   s&   t jt jt jt j| |t jS rK   )r#   r$   relpathrC   r)   rL   r   r   r   _compute_relativet   s   z?get_asset_category_and_relative_path.<locals>._compute_relativer   r   rH   Nr   r   zCPath is not within input, output, temp, or configured model bases: )r#   r$   r%   strboolr   r&   r'   get_temp_directoryr   r!   rC   rN   r)   r   )r8   fp_absrM   rO   
input_baseoutput_base	temp_basebestbucketr,   br6   cand_
rel_insidecombinedr   r   r   rA   ^   s<   




 rA   c                    sH   t | \}}t|  fdd jjD } jttt|g|fS )zReturn (name, tags) derived from a filesystem path.

    - name: base filename with extension
    - tags: [root_category] + parent folder names in order

    Raises:
        ValueError: path does not belong to any known root.
    c                    r9   r   r:   )r<   partr>   r   r   r@      s    z5get_name_and_tags_from_asset_path.<locals>.<listcomp>)	rA   r   rJ   rB   r   listdictfromkeysr   )r8   rD   	some_pathparent_partsr   r>   r   !get_name_and_tags_from_asset_path   s   	
rd   )r#   pathlibr   typingr   r   app.assets.helpersr   r(   r   r_   tuplerP   r   r1   r7   rG   rA   rd   r   r   r   r   <module>   s     
" 
"A