o
    Gi1                     @   s|   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dZG dd deZdS )z
Usage example:
    TODO
    N)ArgumentParser	Namespace)Path   )logging   )BaseDiffusersCLICommandModularPipelineBlockszconfig.jsonargsc                 C   s   t | j| jS N)CustomBlocksCommandblock_module_nameblock_class_name)r
    r   T/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/commands/custom_blocks.pyconversion_command_factory"   s   r   c                   @   sd   e Zd ZedefddZddedefdd	Zd
d ZdddZ	dd Z
dejfddZdd ZdS )r   parserc                 C   s>   |  d}|jdtddd |jdtd dd |jtd d S )	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_parserr   r   r   register_subcommand'   s   
z'CustomBlocksCommand.register_subcommandr   Nr   r   c                 C   s    t d| _t|| _|| _d S )Nzdiffusers-cli/custom_blocks)r   
get_loggerloggerr   r   r   )selfr   r   r   r   r   __init__8   s   

zCustomBlocksCommand.__init__c                 C   s   |  | j}tdd |D }| jd ur1| || j\}}|d u r0|d u r0td| j d| n| jd| d|d  d |d d |d d	 }}d
| jj }t	j
|t| j}t	j
|}|j| t|| t  d S )Nc                 S   s   h | ]\}}|qS r   r   ).0cls_r   r   r   	<setcomp>@   s    z*CustomBlocksCommand.run.<locals>.<setcomp>zB`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__)_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)r!   outclasses_foundchild_classparent_classmodule_namespecmoduler   r   r   run=   s,   
zCustomBlocksCommand.runc                 C   s&   |D ]\}}||kr||f  S qdS )N)NNr   )r!   
candidateschosenr$   baser   r   r   r*   ]   s
   z!CustomBlocksCommand._choose_blockc           	   
      s   |j dd}z	tj||d}W n ty& } ztd|d| |d }~ww g }|jD ]%}t|tjs5q, fdd|jD }t	D ]}||v rP|
|j|f qBq,|S )Nzutf-8)encoding)filenamezCould not parse z: c                    s"   g | ]} |  d ur qS r   )_get_base_name)r#   bbnamer!   r   r   
<listcomp>p   s   " z8CustomBlocksCommand._get_class_names.<locals>.<listcomp>)	read_textastparseSyntaxErrorr+   body
isinstanceClassDefbasesEXPECTED_PARENT_CLASSESappendname)	r!   	file_pathsourcetreeeresultsnode
base_namesallowedr   rG   r   r(   c   s$   
z$CustomBlocksCommand._get_class_namesrZ   c                 C   sH   t |tjr	|jS t |tjr"| |j}|r| d|j S |jS d S )N.)rO   rK   Nameid	AttributerE   valueattr)r!   rZ   valr   r   r   rE   y   s   z"CustomBlocksCommand._get_base_namec                 C   s:   t | jddddd }| | d| i}d|iS )Nz.py r]   r   auto_map)r   r   replacersplit)r!   r;   r:   r>   rf   r   r   r   _create_automap   s   z#CustomBlocksCommand._create_automap)r   Nr   )__name__
__module____qualname__staticmethodr   r   r   r"   r?   r*   r(   rK   exprrE   ri   r   r   r   r   r   &   s    
 r   )__doc__rK   importlib.utilr.   r6   argparser   r   pathlibr   utilsr   rd   r   rR   CONFIGr   r   r   r   r   r   <module>   s   