o
    di                      @   s8  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ e ZeegZg Zddeee  fddZdedeee  deee ef fddZdeee	ee ef eee  f  fddZde	ee ef defddZdd Zedkre  dS dS )    N)Path)DictListOptionalTupleTypeUnion   )load_subpackages)logging   )BaseOptimumCLICommandCommandInfoRootOptimumCLICommand)EnvironmentCommand)ExportCommandparent_commandc                    s2    durt  tstd  d fdd}|S )a4  
    A decorator to declare optimum-cli subcommands.

    The declaration of an optimum-cli subcommand looks like this:

    ```
    @optimum_cli_subcommand()
    class MySubcommand(BaseOptimumCLICommand):
        <implementation>
    ```

    or

    ```
    @optimum_cli_subcommand(ExportCommand)
    class MySubcommand(BaseOptimumCLICommand):
        <implementation>
    ```

    Args:
        parent_command: (`Optional[Type[BaseOptimumCLICommand]]`):
            The class of the parent command or None if this is a top-level command. Defaults to None.

    NzThe parent command , must be a subclass of BaseOptimumCLICommandc                    s,   t | tstd|  dt|  f d S )NzThe subcommand r   )
issubclassr   
ValueError_OPTIMUM_CLI_SUBCOMMANDSappend)
subcommandr    j/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/commands/optimum_cli.pywrapperA   s   
z'optimum_cli_subcommand.<locals>.wrapper)r   r   r   )r   r   r   r   r   optimum_cli_subcommand$   s   r   rootcommandsreturnc                 C   s   | g}t |}i }|r*|d}|j|v r ||j |||j< |s#n||j7 }|s|rAdd |D }td|  dd| d|S )a  
    Retrieves command instances in the root CLI command from a list of command classes.

    Args:
        root (`RootOptimumCLICommand`):
            The root CLI command instance.
        commands (`List[Type[BaseOptimumCLICommand]]`):
            The list of command classes to retrieve the instances of in root.

    Returns:
        `Dict[Type[BaseOptimumCLICommand], BaseOptimumCLICommand]`: A dictionary mapping a command class to a command
        instance in the root CLI.
    r   c                 s   s    | ]}|j V  qd S N)__name__).0commandr   r   r   	<genexpr>e   s    z6resolve_command_to_command_instance.<locals>.<genexpr>z@Could not find an instance of the following commands in the CLI z: z, .)setpop	__class__removeregistered_subcommandsRuntimeErrorjoin)r   r   to_visitremaining_commandscommand2command_instancecurrent_command_instanceclass_namesr   r   r   #resolve_command_to_command_instanceI   s$   



r3   c            
   
   C   s   g } t tjd }| D ]e}| s|jdkr'|jdvr&td| d qd|j	 }t
j|dd}t|d	g }t|D ]3\}}t|trL|\}}	n|}d
}	t|tsjt|tsjtd| d| dt| d| ||	f q>q| S )a  
    Loads a list of command classes to register to the CLI from the `optimum/commands/register/` directory.
    It will look in any python file if there is a `REGISTER_COMMANDS` list, and load the commands to register
    accordingly.
    At this point, nothing is actually registered in the root CLI.

    Returns:
        `List[Tuple[Union[Type[BaseOptimumCLICommand], CommandInfo], Optional[Type[BaseOptimumCLICommand]]]]`: A list
        of tuples with two elements. The first element corresponds to the command to register, it can either be a
        subclass of `BaseOptimumCLICommand` or a `CommandInfo`. The second element corresponds to the parent command,
        where `None` means that the parent command is the root CLI command.
    registerz.py)__pycache__z	README.mdz	Skipping zM because only python files are allowed when registering commands dynamically.z
.register.zoptimum.commands)packageREGISTER_COMMANDSNzThe command at index z in z is not of the right type: r&   )r   __file__parentiterdiris_dirsuffixnameloggerwarningstem	importlibimport_modulegetattr	enumerate
isinstancetupler   r   r   r   typer   )
commands_to_registerregister_dir_pathfilenamemodule_namemodulecommands_to_register_in_filecommand_idxr$   command_or_command_infoparent_command_clsr   r   r   !dynamic_load_commands_in_registerl   s4   



rQ   rO   c                 C   s<   t | tst| jj| jj| d}n| }|  || dS )a/  
    Registers a command as being a subcommand of `parent_command`.

    Args:
        command_or_command_info (`Union[Type[BaseOptimumCLICommand], CommandInfo]`):
            The command to register.
        parent_command (`BaseOptimumCLICommand`):
            The instance of the parent command.
    )helpsubcommand_classN)rE   r   COMMANDr=   rR   is_subcommand_info_or_raiseregister_subcommand)rO   r   command_infor   r   r   register_optimum_cli_subcommand   s   
rX   c            
      C   s   t ddd} | j}tD ]}t|| d qt  tt  }t| dd |D }|D ]\}}|d u r3| }n|| }t||d q(| }t	|dsO|
  td ||}	|	  d S )	NzOptimum CLI toolzoptimum-cli)usager   c                 S   s   g | ]
\}}|d ur|qS r!   r   )r#   _rP   r   r   r   
<listcomp>   s    zmain.<locals>.<listcomp>funcr   )r   parserOPTIMUM_CLI_ROOT_SUBCOMMANDSrX   r
   r   rQ   r3   
parse_argshasattr
print_helpexitr\   run)
r   r]   subcommand_clsrH   r0   rO   r   parent_command_instanceargsservicer   r   r   main   s(   


rh   __main__r!   ) rA   pathlibr   typingr   r   r   r   r   r   subpackagesr
   utilsr   baser   r   r   envr   exportr   
get_loggerr>   r^   r   r   r3   rQ   rX   rh   r"   r   r   r   r   <module>   s>    %

#"
,
"
