o
    پi                     @   sX   d Z ddlZddlZddlmZ ddlmZmZmZ ddl	Z	e
eZG dd dZdS )z
Configuration argument parser for command-line applications.
Handles merging of YAML configuration files with command-line arguments.
    N)Path)AnyDictListc                   @   s  e Zd ZdZ		ddejdee fddZdee dee fd	d
Z	dee defddZ
dedeeef fddZdeddfddZdeeef dee fddZdee dededdfddZdee dedee ddfddZdee dededdfddZdS )ConfigArgumentMergerzLHandles merging of configuration file arguments with command-line arguments.Nparserboolean_actionsc                 C   s^   |dur|| _ dd |jD | _dd |jD | _dS |dur'|| _i | _dS g | _i | _dS )z0Initialize with list of store_true action names.Nc                 S   s   g | ]}t |tjr|jqS  )
isinstanceargparse_StoreTrueActiondest).0actionr	   r	   X/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/server_args_config_parser.py
<listcomp>   s    
z1ConfigArgumentMerger.__init__.<locals>.<listcomp>c                 S   sP   i | ]$}|j r&t|tjs&t|tjs&d |j vr&d|j vr&d|j vr|j|qS )--configz--helpz-h)option_stringsr
   r   r   _StoreActionr   )r   ar	   r	   r   
<dictcomp>!   s    




z1ConfigArgumentMerger.__init__.<locals>.<dictcomp>)r   _actionsstore_true_actionsunsupported_actions)selfr   r   r	   r	   r   __init__   s   


zConfigArgumentMerger.__init__cli_argsreturnc                 C   sX   |  |}|s	|S | |}| |}|d}|d| }||d d }|| | S )a  
        Merge configuration file arguments with command-line arguments.

        Configuration arguments are inserted after the subcommand to maintain
        proper precedence: CLI > Config > Defaults

        Args:
            cli_args: List of command-line arguments

        Returns:
            Merged argument list with config values inserted

        Raises:
            ValueError: If multiple config files specified or no config file provided
        r   N   )_extract_config_file_path_parse_yaml_config_convert_config_to_argsindex)r   r   config_file_pathconfig_dataconfig_argsconfig_indexbefore_configafter_configr	   r	   r   merge_config_with_args3   s   



z+ConfigArgumentMerger.merge_config_with_argsargsc                 C   sZ   dd t |D }t|dkrtd|sdS |d }|t|d kr'td||d  S )z,Extract the config file path from arguments.c                 S   s   g | ]
\}}|d kr|qS )r   r	   )r   iargr	   r	   r   r   V   s    zBConfigArgumentMerger._extract_config_file_path.<locals>.<listcomp>   z2Multiple config files specified! Only one allowed.Nr   z-No config file specified after --config flag!)	enumeratelen
ValueError)r   r*   config_indicesr&   r	   r	   r   r   T   s   z.ConfigArgumentMerger._extract_config_file_path	file_pathc              
   C   s   |  | zt|d}t|}W d   n1 sw   Y  W n ty9 } ztd| d|   d}~ww |du r@i }t|tsIt	d|S )a8  
        Parse YAML configuration file and convert to argument list.

        Args:
            file_path: Path to the YAML configuration file

        Returns:
            List of arguments in format ['--key', 'value', ...]

        Raises:
            ValueError: If file is not YAML or cannot be read
        rNzFailed to read config file z: z3Config file must contain a dictionary at root level)
_validate_yaml_fileopenyaml	safe_load	Exceptionloggererrorr
   dictr0   )r   r2   filer$   er	   r	   r   r    d   s    

z'ConfigArgumentMerger._parse_yaml_configc                 C   s@   t |}|j dvrtd|j | std| dS )z&Validate that the file is a YAML file.)z.yamlz.ymlz&Config file must be YAML format, got: zConfig file not found: N)r   suffixlowerr0   exists)r   r2   pathr	   r	   r   r4      s   z(ConfigArgumentMerger._validate_yaml_fileconfigc                 C   s   g }|  D ]D\}}|dd}|| jv r)| j| }d| d|jj d}t|t|tr6| ||| qt|t	rC| 
||| q| ||| q|S )z2Convert configuration dictionary to argument list.-_zUnsupported config option 'z' with action '')itemsreplacer   	__class____name__r0   r
   bool_add_boolean_arglist_add_list_arg_add_scalar_arg)r   rB   r*   keyvaluekey_normr   msgr	   r	   r   r!      s   



z,ConfigArgumentMerger._convert_config_to_argsrO   rP   c                 C   sR   | dd}|| jv r|r|d|  dS dS |d| t| g dS )z
        Add boolean argument to the list.

        Only store_true flags:
            - value True -> add flag
            - value False -> skip
        Regular booleans:
            - always add --key true/false
        rC   rD   --N)rG   r   appendextendstrr?   )r   r*   rO   rP   rQ   r	   r	   r   rK      s   

 z%ConfigArgumentMerger._add_boolean_argc                 C   s0   |r| d|  |dd |D  dS dS )zAdd list argument to the list.rS   c                 s   s    | ]}t |V  qd S )N)rV   )r   itemr	   r	   r   	<genexpr>   s    z5ConfigArgumentMerger._add_list_arg.<locals>.<genexpr>N)rT   rU   r   r*   rO   rP   r	   r	   r   rM      s   z"ConfigArgumentMerger._add_list_argc                 C   s   | d| t|g dS )z Add scalar argument to the list.rS   N)rU   rV   rY   r	   r	   r   rN      s   z$ConfigArgumentMerger._add_scalar_arg)NN)rI   
__module____qualname____doc__r   ArgumentParserr   rV   r   r)   r   r   r   r    r4   r!   rJ   rK   rM   rN   r	   r	   r	   r   r      s"    
 !	""r   )r\   r   loggingpathlibr   typingr   r   r   r6   	getLoggerrI   r9   r   r	   r	   r	   r   <module>   s    
