o
    ॵi                     @   s`   d dl mZmZmZ d dlmZ d dlmZ G dd deZG dd deZ	G dd	 d	e	Z
d
S )    )ActionArgumentDefaultsHelpFormatterArgumentParser)fields)Listc                       sT   e Zd ZdZd fdd	Zdd Zddef fdd	Zd fd
d	Zdd Z	  Z
S )CliArgumentParserz Argument Parser to define and parse command-line args for training.

    Args:
        training_args: dict or list of dict which defines different
            paramters for training.
    Nc                    s4   d|vrt |d< t jdi | || _|   d S )Nformatter_class )r   super__init__training_argsdefine_args)selfr   kwargs	__class__r	   [/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/trainers/cli_argument_parser.pyr      s
   zCliArgumentParser.__init__c                 C   s   dd |D S )Nc                 S   s"   g | ]}| d r|dd qS )--   N)
startswith).0argr	   r	   r   
<listcomp>   s   " z5CliArgumentParser.get_manual_args.<locals>.<listcomp>r	   )r   argsr	   r	   r   get_manual_args   s   z!CliArgumentParser.get_manual_argsr   c                    sJ   |d ur|j nd | _d|v r||dd  | _| || _t ||S )Nz--model   )modelmodel_idindexr   manual_argsr
   _parse_known_args)r   r   	namespacer   r	   r   r       s
   z#CliArgumentParser._parse_known_argsc                    s   t  |S N)r
   
print_help)r   filer   r	   r   r#      s   zCliArgumentParser.print_helpc                 C   s   | j d urCt| j D ]:}|j}t| j |j}d| }t|j|jd d}||d< d|jv r5|jd |d< t|d< | j|fi | q
d S d S )Nr   help)typer%   defaultchoicesaction)	r   r   namegetattrdictr&   metadataSingleActionadd_argument)r   farg_namearg_attrr*   r   r	   r	   r   r   "   s   


zCliArgumentParser.define_argsr"   )NN)__name__
__module____qualname____doc__r   r   r   r    r#   r   __classcell__r	   r	   r   r   r      s    r   c                   @   s0   e Zd ZdZedd Zedd Zdd ZdS )	
DictActionaR  
    argparse action to split an argument into KEY=VALUE form
    on the first = and append to a dictionary. List options can
    be passed as comma separated values, i.e 'KEY=V1,V2,V3', or with explicit
    brackets, i.e. 'KEY=[V1,V2,V3]'. It also support nested brackets to build
    list/tuple values. e.g. 'KEY=[(V1,V2),(V3,V4)]'
    c                 C   sd   zt | W S  ty   Y nw zt| W S  ty   Y nw |  dv r*|  dkS | dkr0d S | S )N)truefalser9   None)int
ValueErrorfloatlower)valr	   r	   r   parse_int_float_bool_str;   s   

z#DictAction.parse_int_float_bool_strc                 C   s   dd }|  ddd} d}| dr"| dr"d	}| d
d } n| dr3| dr3| d
d } n	d| vr<t| S g }t| dkrd|| }t| d| }|| | |d
 d } t| dksD|rjt	|}|S )a  Parse iterable values in the string.
        All elements inside '()' or '[]' are treated as iterable values.
        Args:
            val (str): Value string.
        Returns:
            list | tuple: The expanded list or tuple from the string.
        Examples:
            >>> DictAction._parse_iterable('1,2,3')
            [1, 2, 3]
            >>> DictAction._parse_iterable('[a, b, c]')
            ['a', 'b', 'c']
            >>> DictAction._parse_iterable('[(1, 2, 3), [a, b], c]')
            [(1, 2, 3), ['a', 'b'], 'c']
        c                 S   s   |  d|  dkr|  d|  dksJ d|  t| }t| D ]'\}}| d| }|dkrJ| d| dkrJ| d| dkrJ|} |S q#|S )a  Find the position of next comma in the string.
            If no ',' is found in the string, return the string length. All
            chars inside '()' and '[]' are treated as one element and thus ','
            inside these brackets are ignored.
            ()[]zImbalanced brackets exist in N,)countlen	enumerate)stringendidxcharprer	   r	   r   find_next_comma\   s     z2DictAction.parse_iterable.<locals>.find_next_commaz'"  FrB   rC   Tr   rD   rE   rF   r   N)
stripreplacer   endswithr8   rA   rH   parse_iterableappendtuple)r@   rO   is_tuplevalues	comma_idxelementr	   r	   r   rV   K   s(   

zDictAction.parse_iterablec           	      C   s@   i }|D ]}|j ddd\}}| |||< qt|| j| d S )N=r   )maxsplit)splitrV   setattrdest)	r   parserr!   rZ   option_stringoptionskvkeyr@   r	   r	   r   __call__   s
   zDictAction.__call__N)r3   r4   r5   r6   staticmethodrA   rV   rg   r	   r	   r	   r   r8   2   s    

9r8   c                   @   s   e Zd ZdZdd ZdS )r.   z Argparse action to convert value to tuple or list or nested structure of
    list and tuple, i.e 'V1,V2,V3', or with explicit brackets, i.e. '[V1,V2,V3]'.
    It also support nested brackets to build list/tuple values. e.g. '[(V1,V2),(V3,V4)]'
    c                 C   s4   t |trt|| j| | d S t|| j| d S r"   )
isinstancestrr`   ra   rV   )r   rb   r!   valuerc   r	   r	   r   rg      s   
zSingleAction.__call__N)r3   r4   r5   r6   rg   r	   r	   r	   r   r.      s    r.   N)argparser   r   r   dataclassesr   typingr   r   r8   r.   r	   r	   r	   r   <module>   s    ,[