
    +j                         d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 g dZdedefdZ G d	 d
          ZdS )    N)web)	lru_cache)merge_json_recursive)znodeDefs.jsonzcommands.jsonzsettings.json	file_pathreturnc                 "   t           j                            |           si S 	 t          | dd          5 }t	          j        |          cd d d            S # 1 swxY w Y   d S # t          j        $ r t          j        d|             i cY S w xY w)Nrzutf-8)encodingzError loading )	ospathexistsopenjsonloadJSONDecodeErrorloggingerror)r   fs     7/home/wildlama/comfy/ComfyUI/app/custom_node_manager.pysafe_load_json_filer      s    7>>)$$ 	)S7333 	 q9Q<<	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	    2y22333			s4   A# A	A# AA# AA# #(BBc                   >    e Zd Z ed          d             Zd ZdS )CustomNodeManager   )maxsizec           	         i }t          j        d          D ]}t          t          j        t          j                            |d                              D ]b}t          j                            |d          }t          j                            |          sCt          j        t          j                            |d                    D ]}t          j                            t          j        	                    |                    }||vri ||<   t          j                            |d          }t          |          }t          D ]S}	t          j                            ||	          }
|	                    d          d         }t          |
          }|r|||<   T|rt          ||         |          ||<   d|S )a  Load all custom nodes translations during initialization. Translations are
        expected to be loaded from `locales/` folder.

        The folder structure is expected to be the following:
        - custom_nodes/
            - custom_node_1/
                - locales/
                    - en/
                        - main.json
                        - commands.json
                        - settings.json

        returned translations are expected to be in the following format:
        {
            "en": {
                "nodeDefs": {...},
                "commands": {...},
                "settings": {...},
                ...{other main.json keys}
            }
        }
        custom_nodes*/localesz	main.json.r   )folder_pathsget_folder_pathssortedglobr   r   joinr   basenamedirnamer   EXTRA_LOCALE_FILESsplitr   )selftranslationsfoldercustom_node_dirlocales_dirlang_dir	lang_code	main_filenode_translations
extra_fileextra_file_pathkey	json_datas                r   build_translationsz$CustomNodeManager.build_translations!   s   2 "3NCC 	 	F#)$)BGLL4N4N*O*O#P#P   gll?IFFw~~k22  $	"',,{D*I*I J J  H " 0 01J1J K KI 4424Y/ !#X{ C CI(;I(F(F% '9 ? ?
*,',,x*L*L(..s33A6$7$H$H	$ ?5>-c2( 2F(35F3 3Y/'8     c                    
 g d
|                     d          
fd            }|D ]\  }}
D ]}t          j                            ||          }t          j                            |          rH|dk    rt          j        d||           |                    t          j	        d|z   |          g           |                     d           fd            }	d S )	N)example_workflowsexampleexamplesworkflow	workflowsz/workflow_templatesc                   K   g }t          j        d          D ]T}
D ]O}t          j                            |d| d          }t          j        |          }|                    |           PUi }|D ]}t          j                            t          j                            t          j                            |                              }t          j        	                    t          j                            |                    d         }	|
                    |g                               |	           t          j        |          S )zReturns a web response that contains the map of custom_nodes names and their associated workflow templates. The ones without templates are omitted.r   r   z/*.jsonr   )r    r!   r   r   r$   r#   extendr%   r&   splitext
setdefaultappendr   json_response)requestfilesr+   folder_namepatternmatched_filesworkflow_templates_dictfilecustom_nodes_nameworkflow_nameexample_workflow_folder_namess             r   get_workflow_templatesz<CustomNodeManager.add_routes.<locals>.get_workflow_templates`   sD      E&7GG 0 0#@ 0 0K gll63L3L3L3LMMG$(Ig$6$6MLL////0  $   $&G$4$4GOOBGOOD$9$9::% %! !# 0 01A1A$1G1G H H K'223DbIIPP!    $%<===r7   r9   zdFound example workflow folder '%s' for custom node '%s', consider renaming it to 'example_workflows'z/api/workflow_templates/z/i18nc                 T   K   t          j                                                  S )z<Returns translations from all custom nodes' locales folders.)r   rC   r6   )rD   r)   s    r   get_i18nz.CustomNodeManager.add_routes.<locals>.get_i18n   s&       $T%<%<%>%>???r7   )
getr   r   r$   r   r   debug
add_routesr   static)r)   routeswebapploadedModulesrN   module_name
module_dirrF   workflows_dirrP   rM   s   `         @r   rS   zCustomNodeManager.add_routes\   s=   (m(m(m%	)	*	*	> 	> 	> 	> 
+	*	>2 (5 	 	#K<   "Z E E7>>-00 "&999 C'6 6 6 %%J :[ H-   " 
G			@ 	@ 	@ 	@ 
		@ 	@ 	@r7   N)__name__
__module____qualname__r   r6   rS    r7   r   r   r       sP        Yq8 8 8t3@ 3@ 3@ 3@ 3@r7   r   )r   r    r#   aiohttpr   r   r   	functoolsr   utils.json_utilr   r'   strdictr   r   r^   r7   r   <module>rd      s    				                    0 0 0 0 0 0   	3 	4 	 	 	 	o@ o@ o@ o@ o@ o@ o@ o@ o@ o@r7   