o
    i                     @  s   d dl mZ 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	G dd dZ
G dd deZG d	d
 d
eZG dd dZdS )    )annotations)	TypedDictN)webc                   @  s   e Zd ZdZdZdS )Sourcecustom_node	templatesN)__name__
__module____qualname__r   r    r   r   2/mnt/c/Users/fbmor/ComfyUI/app/subgraph_manager.pyr      s    r   c                   @  s>   e Zd ZU ded< 	 ded< 	 ded< 	 ded< 	 ded< dS )	SubgraphEntrystrsourcepathnameCustomNodeSubgraphEntryInfoinfodataNr   r	   r
   __annotations__r   r   r   r   r      s   
 r   c                   @  s   e Zd ZU ded< dS )r   r   	node_packNr   r   r   r   r   r   #   s   
 r   c                   @  sp   e Zd Zdd Zd#d	d
Zd$ddZd%d&ddZd%d'ddZd%ddZd%ddZ	d%ddZ
d(ddZd d! Zd"S ))SubgraphManagerc                 C  s   d | _ d | _d S N)cached_custom_node_subgraphscached_blueprint_subgraphs)selfr   r   r   __init__(   s   
zSubgraphManager.__init__filer   r   r   returntuple[str, SubgraphEntry]c                 C  sH   t | |   }|tjtj|d |d|id}||fS )zTCreate a subgraph entry from a file path. Expects normalized path (forward slashes).r   r   )r   r   r   r   )hashlibsha256encode	hexdigestosr   splitextbasename)r   r   r   r   entry_identryr   r   r   _create_entry,   s   zSubgraphManager._create_entryr)   r   c                   sF   t |d ddd}| |d< W d    |S 1 sw   Y  |S )Nr   rzutf-8)encodingr   )openread)r   r)   fr   r   r   load_entry_data7   s   
zSubgraphManager.load_entry_dataFSubgraphEntry | Nonec                   s6   |d u rd S |  }|dd  |r|dd  |S )Nr   r   )copypop)r   r)   remove_datar   r   r   sanitize_entry<   s   zSubgraphManager.sanitize_entryentriesdict[str, SubgraphEntry]c                   s:   |  }t| D ]}| || |I d H ||< q|S r   )r2   listkeysr5   )r   r6   r4   keyr   r   r   sanitize_entriesE   s
   z SubgraphManager.sanitize_entriesc           
        s   |s| j dur| j S i }tdD ].}tj|d}t|D ]}|dd}d|dd  }| 	|t
j|\}}	|	||< q q|| _ |S )z!Load subgraphs from custom nodes.Ncustom_nodesz*/subgraphs/*.json\/zcustom_nodes.)r   folder_pathsget_folder_pathsr%   r   joinglobreplacesplitr*   r   r   )
r   loadedModulesforce_reloadsubgraphs_dictfolderpatternr   r   r(   r)   r   r   r   get_custom_node_subgraphsK   s   
z)SubgraphManager.get_custom_node_subgraphsc                   s   |s| j dur| j S i }tjtjtjtd}tj|rCttj|dD ]}|dd}| 	|t
jd\}}|||< q,|| _ |S )z-Load subgraphs from the blueprints directory.N
blueprintsz*.jsonr=   r>   comfyui)r   r%   r   rB   dirname__file__existsrC   rD   r*   r   r   )r   rG   rH   blueprints_dirr   r(   r)   r   r   r   get_blueprint_subgraphs\   s   
z'SubgraphManager.get_blueprint_subgraphsc                   s0   |  ||I dH }| |I dH }i ||S )zAGet all subgraphs from all sources (custom nodes and blueprints).N)rK   rR   )r   rF   rG   custom_node_subgraphsblueprint_subgraphsr   r   r   get_all_subgraphsm   s   z!SubgraphManager.get_all_subgraphsidc                   sB   |  |I dH |}|dur|ddu r| |I dH  |S )z.Get a specific subgraph by ID from any source.Nr   )rU   getr0   )r   rV   rF   r)   r   r   r   get_subgraphs   s
   zSubgraphManager.get_subgraphc                   s4   | d fdd}| d fdd}d S )Nz/global_subgraphsc                   s,     I d H }tj|ddI d H S )NT)r4   )rU   r   json_responser;   )requestrH   rF   r   r   r   get_global_subgraphs{   s   z8SubgraphManager.add_routes.<locals>.get_global_subgraphsz/global_subgraphs/{id}c                   s8   | j dd }| I d H }t|I d H S )NrV   )
match_inforW   rX   r   rY   r5   )rZ   rV   subgraphr[   r   r   get_global_subgraph   s   z7SubgraphManager.add_routes.<locals>.get_global_subgraph)rW   )r   routesrF   r\   r_   r   r[   r   
add_routesz   s   zSubgraphManager.add_routesN)r   r   r   r   r   r   r   r    )r)   r   )F)r)   r1   r   r1   )r6   r7   r   r7   )rV   r   )r   r	   r
   r   r*   r0   r5   r;   rK   rR   rU   rX   ra   r   r   r   r   r   '   s    

	



r   )
__future__r   typingr   r%   r@   rC   aiohttpr   r!   r   r   r   r   r   r   r   r   <module>   s    