o
    ic                     @  s|   d dl mZ 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ddZG dd dZdS )    )annotationsN)web)	lru_cache)merge_json_recursive)znodeDefs.jsonzcommands.jsonzsettings.json	file_pathstrreturndictc                 C  sz   t j| si S z t| ddd}t|W  d    W S 1 s!w   Y  W d S  tjy<   td|   i  Y S w )Nrzutf-8)encodingzError loading )	ospathexistsopenjsonloadJSONDecodeErrorloggingerror)r   f r   5/mnt/c/Users/fbmor/ComfyUI/app/custom_node_manager.pysafe_load_json_file   s   (r   c                   @  s&   e Zd Zedddd Zdd ZdS )CustomNodeManager   )maxsizec                 C  s   i }t dD ]q}tttj|dD ]b}tj|d}tj|s%qttj|dD ]G}tjtj	|}||vrCi ||< tj|d}t
|}tD ]}	tj||	}
|	dd }t
|
}|rj|||< qP|rvt|| |||< q/qq|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_datar   r   r   build_translations#   s6   z$CustomNodeManager.build_translationsc           
        s   g d | d fdd}|D ]-\}} D ]&}tj||}tj|r=|dkr1td|| |t	d| |g qq| dfd	d
}	d S )N)example_workflowsexampleexamplesworkflow	workflowsz/workflow_templatesc           
        s   g }t dD ]} D ]}tj|d| d}t|}|| qqi }|D ]%}tjtjtj|}tj	tj|d }	|
|g |	 q)t|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_namesr   r   get_workflow_templatesb   s&   

z<CustomNodeManager.add_routes.<locals>.get_workflow_templatesr7   zdFound example workflow folder '%s' for custom node '%s', consider renaming it to 'example_workflows'z/api/workflow_templates/z/i18nc                   s   t   S )z<Returns translations from all custom nodes' locales folders.)r   r@   r6   )rA   )r)   r   r   get_i18n   s   z.CustomNodeManager.add_routes.<locals>.get_i18n)
getr   r   r$   r   r   debug
add_routesr   static)
r)   routeswebapploadedModulesrK   module_name
module_dirrC   workflows_dirrL   r   )rJ   r)   r   rO   ^   s,   zCustomNodeManager.add_routesN)__name__
__module____qualname__r   r6   rO   r   r   r   r   r   "   s    
:r   )r   r   r   r	   )
__future__r   r   r    r#   aiohttpr   r   r   	functoolsr   utils.json_utilr   r'   r   r   r   r   r   r   <module>   s    
