
    biz                         d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZ d	gZd
ZdefdZ G d de      Zy)z
Usage example:
    TODO
    N)ArgumentParser	Namespace)Path   )logging   )BaseDiffusersCLICommandModularPipelineBlockszconfig.jsonargsc                 B    t        | j                  | j                        S N)CustomBlocksCommandblock_module_nameblock_class_name)r   s    [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/commands/custom_blocks.pyconversion_command_factoryr   "   s    t55t7L7LMM    c                   n    e Zd Zedefd       ZddedefdZd ZddZ	d	 Z
d
ej                  fdZd Zy)r   parserc                     | j                  d      }|j                  dt        dd       |j                  dt        d d       |j                  t               y )	Ncustom_blocksz--block_module_nameblock.pyz>Module filename in which the custom block will be implemented.)typedefaulthelpz--block_class_namezDName of the custom block. If provided None, we will try to infer it.)func)
add_parseradd_argumentstrset_defaultsr   )r   conversion_parsers     r   register_subcommandz'CustomBlocksCommand.register_subcommand'   sg    "--o>&&!Q	 	' 	
 	&& W	 	' 	
 	&&,F&Gr   Nr   r   c                 f    t        j                  d      | _        t        |      | _        || _        y )Nzdiffusers-cli/custom_blocks)r   
get_loggerloggerr   r   r   )selfr   r   s      r   __init__zCustomBlocksCommand.__init__8   s+    (()FG!%&7!8 0r   c                 T   | j                  | j                        }t        |D ch c]  \  }}|	 c}}      }| j                  >| j	                  || j                        \  }}|R|Pt        d| j                   d|       | j                  j                  d| d|d    d       |d   d   |d   d   }}d| j                  j                   }t        j                  j                  |t        | j                              }t        j                  j                  |      }	|j                  j                  |	        t!        |	|             j#                  t%        j&                                t)        d	d
      5 }
|
j+                  d       d d d        y c c}}w # 1 sw Y   y xY w)NzB`block_class_name` could not be retrieved. Available classes from z:
zFound classes: z will be using r   zP. If this needs to be changed, re-run the command specifying `block_class_name`.r   __dynamic__zrequirements.txtw )_get_class_namesr   listr   _choose_block
ValueErrorr%   infostem	importlibutilspec_from_file_locationr   module_from_specloaderexec_modulegetattrsave_pretrainedosgetcwdopenwrite)r&   outcls_classes_foundchild_classparent_classmodule_namespecmodulefs              r   runzCustomBlocksCommand.run=   s   ##D$:$:;4fc1c45  ,(,(:(:3@U@U(V%K"|'; X--.c-B 
 KK!-a@P?Q Ra a ),Aq	3q6!9K $D$:$:$?$?#@A~~55k3tG]G]C^_006'$$&66ryy{C $c* 	aGGBK	 	9 58	 	s   F
=FF'c                 .    |D ]  \  }}||k(  s||fc S  y)N)NN )r&   
candidateschosenr?   bases        r   r.   z!CustomBlocksCommand._choose_block_   s+    # 	!ICf}Dy 	! r   c                    |j                  d      }	 t        j                  ||      }g }|j
                  D ]w  }t        |t        j                        s|j                  D cg c]  }| j                  |      x}| }	}t        D ]$  }
|
|	v s|j                  |j                  |
f       & y |S # t        $ r}t	        d|d|       |d }~ww xY wc c}w )Nzutf-8)encoding)filenamezCould not parse z: )	read_textastparseSyntaxErrorr/   body
isinstanceClassDefbases_get_base_nameEXPECTED_PARENT_CLASSESappendname)r&   	file_pathsourcetreeeresultsnodebbname
base_namesalloweds              r   r,   z$CustomBlocksCommand._get_class_namese   s    $$g$6	K99Vi8D *,II 
	9DdCLL1 *.eAATATUVAW8W7d%eJe 3 9j(NNDIIw#789
	9 !  	K/	}BqcBCJ	K fs#   B5 %C>C5	C>CCrb   c                     t        |t        j                        r|j                  S t        |t        j                        r:| j                  |j                        }|r| d|j                   S |j                  S y )N.)rV   rR   Nameid	AttributerY   valueattr)r&   rb   vals      r   rY   z"CustomBlocksCommand._get_base_name{   s^    dCHH%77Ncmm,%%djj1C+.cU!DII;'=DII=r   c                     t        | j                        j                  dd      j                  dd      d   }| | d| i}d|iS )Nz.pyr+   rh   r   auto_map)r   r   replacersplit)r&   rC   rB   rF   rq   s        r   _create_automapz#CustomBlocksCommand._create_automap   sS    T++,44UB?FFsANrR#n&;-'@AH%%r   )r   Nr   )__name__
__module____qualname__staticmethodr   r"   r   r'   rH   r.   r,   rR   exprrY   rt   rJ   r   r   r   r   &   sV    HN H H 1# 1c 1
 D,388 &r   r   )__doc__rR   importlib.utilr2   r:   argparser   r   pathlibr   utilsr   r+   r	   rZ   CONFIGr   r   rJ   r   r   <module>r      sN   
   	 .   % 33 	NY N`&1 `&r   