o
    di                     @   s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZ e	r,ddlmZmZ eddG d	d
 d
ZG dd deZG dd deZdS )z,Optimum command-line interface base classes.    )ABC)ArgumentParserRawTextHelpFormatter)	dataclass)TYPE_CHECKINGOptionalTupleType)	Namespace_SubParsersActionT)frozenc                   @   sR   e Zd ZU eed< eed< dZeed  ed< eZ	eed< e
dd Zd	d
 ZdS )CommandInfonamehelpNBaseOptimumCLICommandsubcommand_classformatter_classc                 C   s
   | j d uS N)r   self r   c/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/commands/base.pyis_subcommand_info"      
zCommandInfo.is_subcommand_infoc                 C   s   | j std|  dd S )NzDThe command info must define a subcommand_class attribute, but got: .)r   
ValueErrorr   r   r   r   is_subcommand_info_or_raise&   s   z'CommandInfo.is_subcommand_info_or_raise)__name__
__module____qualname__str__annotations__r   r   r	   r   r   propertyr   r   r   r   r   r   r      s   
 
r   c                   @   s   e Zd ZU eed< dZeedf ed< 				dded d	ed
 dee deded f
ddZ	e
dd Zejded fddZe
dd ZedddZdefddZdd ZdS )r   COMMANDr   .SUBCOMMANDSNF
subparsersr   argsr
   commandfrom_defaults_factoryparserr   c                    s   |dur| _ |r|du rtd  d| _| _nE|du r(td  d|j j j j jd _  j  fdd} jj|d  j	D ]}t
|ts[td	t| d
 | qJ| _dS )a0  
        Initializes the instance.

        Args:
            subparsers (`Optional[_SubParsersAction]`):
                The parent subparsers this command will create its parser on.
            args (`Optional[Namespace]`, defaults to `None`):
                The parsed arguments that are going to be use in self.run().
            command (`Optional[CommandInfo]`, defaults to `None`):
                The command info for this instance. This can be used to set the class attribute `COMMAND`.
            from_defaults_factory (`bool`, defaults to `False`):
                When setting the parser defaults, we create a second instance of self. By setting
                `from_defaults_factory=True`, we do not do unnecessary actions for setting the defaults, such as
                creating a parser.
        Nz^The instance of the original parser must be passed when creating a defaults factory, command: r   zKA subparsers instance is needed when from_defaults_factory=False, command: )r   c                    s    j  j|  jd jdS )NT)r'   r(   r)   )	__class__r%   r#   r)   )r&   r   r   r   defaults_factoryY   s   z8BaseOptimumCLICommand.__init__.<locals>.defaults_factory)funcz6Subcommands must be instances of CommandInfo, but got z here.)r#   r   r)   r%   
add_parserr   r   
parse_argsset_defaultsr$   
isinstancer   typeregister_subcommandr&   )r   r%   r&   r'   r(   r)   r+   
subcommandr   r   r   __init__/   s,   


zBaseOptimumCLICommand.__init__c                 C   s8   t | dd}|du r| jr| j | _| jS d| _| jS )a2  
        This property handles how subparsers are created, which are only needed when registering a subcommand.
        If `self` does not have any subcommand, no subparsers should be created or it will mess with the command.
        This property ensures that we create subparsers only if needed.
        _subparsersN)getattrr$   r)   add_subparsersr5   r   r%   r   r   r   r%   g   s   z BaseOptimumCLICommand.subparsersc                 C   s
   || _ d S r   )r5   r8   r   r   r   r%   v   r   c                 C   s   t | dsg | _| jS )N_registered_subcommands)hasattrr9   r   r   r   r   registered_subcommandsz   s   
z,BaseOptimumCLICommand.registered_subcommandsc                 C   s   d S r   r   )r)   r   r   r   r.      s   z BaseOptimumCLICommand.parse_argscommand_infoc                 C   s2   |   | j|f | _| j|j| j|d d S )N)r'   )r   r$   r;   appendr   r%   )r   r<   r   r   r   r2      s   z)BaseOptimumCLICommand.register_subcommandc                 C   s   | j   d S r   )r)   
print_helpr   r   r   r   run   s   zBaseOptimumCLICommand.run)NNFN)r)   r   )r   r   r   r   r!   r$   r   r   boolr4   r"   r%   setterr;   staticmethodr.   r2   r?   r   r   r   r   r   +   s8   
 
8

r   c                   @   s8   e Zd ZedddZddedee ded fd	d
ZdS )RootOptimumCLICommandrootzoptimum-cli root command)r   r   Ncli_nameusager&   r
   c                 C   s$   t ||d| _| j | _d | _d S )N)rF   )r   r)   r7   r%   r&   )r   rE   rF   r&   r   r   r   r4      s   
zRootOptimumCLICommand.__init__)NN)r   r   r   r   r#   r    r   r4   r   r   r   r   rC      s    $rC   N)__doc__abcr   argparser   r   dataclassesr   typingr   r   r   r	   r
   r   r   r   rC   r   r   r   r   <module>   s   b