
    +j.                     @   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Zde
e         deee
e         f         fd	Zd
ededdfdZdededz  fdZdedeed         ef         fdZdedeee
e         f         fdZdS )    N)Path)Literal)normalize_tagscustom_nodesreturnc                      g } t           j                                        D ]8\  }}|t          v r|d         |d         }}|r|                     ||f           9| 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_extss        >/home/wildlama/comfy/ComfyUI/app/assets/services/path_utils.pyget_comfy_models_foldersr      so     ,.G$;AACC * *f***ay&)u 	*NND%=)))N    tagsc                    | st          d          | d                                         }|dk    rt          |           dk     rt          d          	 t          j        | d                  d         }n'# t
          $ r t          d| d          d          w xY w|st          d	| d          d          t          j                            |d                   }| dd
         }n|dk    r;t          j                            t          j	                              }| dd
         }nZ|dk    r;t          j                            t          j
                              }| dd
         }nt          d| d          d          t          ddt          j        f          }|D ]'}|dv s|t          |          z  rt          d          (||r|n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is          r   resolve_destination_from_tagsr4      s    312227==??Dxt99q==IJJJ	D 7Q@CEE 	D 	D 	DBQBBBCCC	D 	RPd1gPPPQQQ7??58,,122h	7??<#C#E#EFF122h			7??<#D#F#FGG122had1gaaabbbCrv.//J ? ?zCFF2=>>>  K7[[R77s   A4 4$B	candidatebasec                     t          t          j                            |                     }t          t          j                            |                    }|                    |          st          d          d S )Nz"destination escapes base directory)r   r&   r'   r(   is_relative_tor"   )r5   r6   cand_absbase_abss       r   validate_path_within_baser;   =   sg    BGOOI..//HBGOOD))**H""8,, ?=>>>? ?r   	file_pathc                 F   	 t          |           \  }}n# t          $ r Y dS w xY wt          |          fdj        D             }|sdS |dk    r;t	          |          dk    r
|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|S r    anchor).0segps     r   
<listcomp>z-compute_relative_filename.<locals>.<listcomp>S   s,    HHHSs3ah2G'G'GS'G'G'Gr   r   r	   r   r   )$get_asset_category_and_relative_pathr"   r   partsr$   join)r<   root_categoryrel_pathrF   insiderC   s        @r   compute_relative_filenamerK   D   s    "Fy"Q"Qxx   tt 	XAHHHHAGHHHE t  !%jj1nnqrr58*xx88E??s    
$$)r   r   tempr   c                 n   t           j                            |           }dt          dt          dt          fd}dt          dt          dt          fd}t           j                            t          j                              } |||          rd |||          fS t           j                            t          j                              } |||          rd |||          fS t           j                            t          j                              } |||          rd |||          fS d	}t                      D ]h\  }}	|	D ]`}
t           j                            |
          } |||          s.t          |          | |||          f}||d
         |d
         k    r|}ai|z|\  }}}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                 F    t          |                               |          S N)r   r8   rN   rO   s     r   _check_is_withinz>get_asset_category_and_relative_path.<locals>._check_is_withinq   s    E{{))&111r   c           	          t           j                            t           j                            t           j        t           j                            | |                    t           j                  S rQ   )r&   r'   relpathrG   r,   rR   s     r   _compute_relativez?get_asset_category_and_relative_path.<locals>._compute_relativet   sE     wGLL!?!?@@"&
 
 	
r   r   r   rL   Nr   r   zCPath is not within input, output, temp, or configured model bases: )r&   r'   r(   strboolr
   r)   r*   get_temp_directoryr   r$   rG   rU   r,   r"   )r<   fp_absrS   rV   
input_baseoutput_base	temp_basebestbucketr/   br:   cand_
rel_insidecombineds                   r   rE   rE   ^   s_   " W__Y''F2 2S 2T 2 2 2 2
 
c 
c 
 
 
 
 !A!C!CDDJ
++ >))&*==== '//,"C"E"EFFK,, @**6;????  ? A ABBI	** <((;;;; )-D133   	 	Awq))H##FH55 MM6+<+<VX+N+NOD|tAwa00	  $6:7<<
33bfh)G)GPPPP
YiYY  r   c           	          t          |           \  }}t          |          f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                 ,    g | ]}|d dj         fv|S r    r?   )rA   partrC   s     r   rD   z5get_name_and_tags_from_asset_path.<locals>.<listcomp>   s3       4T187L+L+L+L+L+Lr   )	rE   r   rO   rF   r   listdictfromkeysr   )r<   rH   	some_pathparent_partsrC   s       @r   !get_name_and_tags_from_asset_pathrm      s      DINNM9YA     L 64nm5Sl5S&T&TUUVVVVr   )r&   pathlibr   typingr   r
   app.assets.helpersr   r+   r   rh   tuplerW   r   r4   r;   rK   rE   rm    r   r   <module>rs      s   				                 - - - - - - $)^$455 $uS$s)^'<"=    "8S	 8eCcN6K 8 8 8 8@? ?C ?D ? ? ? ? t    4>>
767<=> > > >BW WsDI~9N W W W W W Wr   