o
    W+ iq                     @   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                       sX   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
            parameters 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	   s/home/app/PaddleOCR-VL-test/.venv_paddleocr/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                    sX   |d ur|j nd | _d|v r||dd  | _| || _t j||g|R i |S )Nz--model   )modelZmodel_idindexr   Zmanual_argsr
   _parse_known_args)r   r   	namespaceZ
args_extrar   r   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   fZarg_nameZ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falser5   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_commaa   s     z2DictAction.parse_iterable.<locals>.find_next_commaz'"  Fr>   r?   Tr   r@   rA   rB   r   N)
stripreplacer   endswithr4   r=   rD   parse_iterableappendtuple)r<   rK   is_tuplevaluesZ	comma_idxelementr	   r	   r   rR   P   s(   

zDictAction.parse_iterablec           	      C   s@   i }|D ]}|j ddd\}}| |||< qt|| j| d S )N=r   )maxsplit)splitrR   setattrdest)	r   parserr   rV   option_stringoptionskvkeyr<   r	   r	   r   __call__   s
   zDictAction.__call__N)r/   r0   r1   r2   staticmethodr=   rR   rb   r	   r	   r	   r   r4   7   s    

9r4   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[   r\   rR   )r   r]   r   valuer^   r	   r	   r   rb      s   
zSingleAction.__call__N)r/   r0   r1   r2   rb   r	   r	   r	   r   r,      s    r,   N)argparser   r   r   dataclassesr   typingr   r   r4   r,   r	   r	   r	   r   <module>   s    1[