o
    nim&                     @   s  U d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
 d dlZd dlZd dlmZ d dlmZ er;d dlmZ g dZee ed< edZd	ed
ee fddZdee d
efddZdee d
dfddZded
eeeef  fddZdee d
efddZ		d1deeef dededee ded
e
ee eeef df fdd Zd2d!ee d
efd"d#Z d!ed
eeef fd$d%Z!d&eeef d!eee  d
e	eee  ef fd'd(Z"d)d*d+ee d
efd,d-Z#d.eeef d
e	eeef eeef f fd/d0Z$dS )3    N)TYPE_CHECKINGAnyDictListOptionalTupleUnion)util)LaunchError)Api)z${env}z${interpreter}z
${program}z${args}DEFAULT_SWEEP_COMMANDz\$\{envvar\:([A-Z0-9_]*)\}
parts_dictreturnc                 C   s   d}d}|  d}t|tsdS |d}t|dkrn&t|dkr+|\}}|p)|}nt|dkr?|\}}}|p9|}|p=|}n	 dS | t|||d	 dS )
zIn place parse sweep path from parts dict.

    Arguments:
        parts_dict (dict): dict(entity=,project=,name=).  Modifies dict inplace.

    Returns:
        None or str if there is an error
    NnamezExpected string sweep_id/         zJExpected sweep_id in form of sweep, project/sweep, or entity/project/sweep)r   projectentity)get
isinstancestrsplitlenupdatedict)r   r   r   sweep_idsweep_splitsplit_projectsplit_entity r!   [/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/launch/sweeps/utils.pyparse_sweep_id   s&   	





r#   
violationsc                 C   sB   d}t | D ]\}}d|d  d| | |< qd|g|  }|S )a  Consolidate schema violation strings from wandb/sweeps into a single string.

    Parameters
    ----------
    violations: list of str
        The warnings to render.

    Returns:
    -------
    violation: str
        The consolidated violation text.

    zMalformed sweep config detected! This may cause your sweep to behave in unexpected ways.
To avoid this, please fix the sweep config schema violations below:z  Violation r   z. 
)	enumeratejoin)r$   violation_baseiwarning	violationr!   r!   r"   0sweep_config_err_text_from_jsonschema_violations9   s   r,   warningsc                 C   s&   t | }t| dkrt| dS dS )zEcho sweep config schema violation warnings from Gorilla to the terminal.

    Parameters
    ----------
    warnings: list of str
        The warnings to render.
    r   N)r,   r   wandbtermwarn)r-   r*   r!   r!   r"   handle_sweep_config_violationsS   s   r0   sweep_config_pathc              
   C   s   zt | }W n ty   td|   Y dS w zt|}W n tjy< } ztd|  W Y d}~dS d}~ww |sFtd dS |S )zLoad a sweep yaml from path.zCouldn't open sweep file: NzError in configuration file: zConfiguration file is empty)openOSErrorr.   	termerroryaml	safe_load	YAMLError)r1   	yaml_fileconfigerrr!   r!   r"   load_sweep_config`   s"   
r;   r9   c                 C   s.   | si S t | }|d u rtd|  d|S )NzCould not load config from z. Check formatting)r	   load_json_yaml_dictr
   )r9   parsed_configr!   r!   r"   load_launch_sweep_configr   s   
r>   Fsweep_configqueuer   author
return_jobc           	      C   s   |  d}|  d}|s|std dS |r |r td dS |r=d||d}|r/||d< n|r5||d< |r;||d< |S d	|d
|g}|rN|d|g7 }|rY|d|g7 }|S |ra|d|g7 }|S )zConstruct sweep scheduler args.

    logs error and returns None if misconfigured,
    otherwise returns args as a dict if is_job else a list of strings.
    job	image_urizhNo 'job' nor 'image_uri' top-level key found in sweep config, exactly one is required for a launch-sweepNzOSweep config has both 'job' and 'image_uri' but a launch-sweep can use only oneWANDB_SWEEP_ID)r   r@   r   rA   z--queuez	--projectz--authorz--jobz--image_uri)r   r.   r4   )	r?   r@   r   rA   rB   rC   rD   	args_dictargsr!   r!   r"   construct_scheduler_args|   sV   


rH   commandc                 C   s   | pt } t| D ]C\}}tt|rKtt|}|ddd D ])}tj	|
d|
d}| | d|   | | | | d  | |< q!q| S )z=Return sweep command, filling in environment variable macros.Nr   )r   r&   SWEEP_COMMAND_ENV_VAR_REGEXsearchr   listfinditerosenvironr   groupstartend)rI   r)   chunkmatchesm_varr!   r!   r"   create_sweep_command   s   6rX   c           
   	   C   s   d| vrt d| g }g }g }i }g }| d  D ]Q\}}z|d }W n ty3   t d|w | d| }	|d|	  ||	 |d| t|g7 }t|tra|r`|d|  n|d|	  |||< q||||t	|g|dS )zCreate various formats of command arguments for the agent.

    Raises:
        ValueError: improperly formatted command dict

    rG   zNo "args" found in command: {}valuez*No "value" found for command["args"]["{}"]=z--)rG   args_no_equalsargs_no_hyphensargs_no_boolean_flags	args_jsonrF   )

ValueErrorformatitemsKeyErrorappendr   r   booljsondumps)
rI   flagsflags_no_hyphensflags_no_booleans
flags_dictr[   paramr9   _value_flagr!   r!   r"   create_sweep_command_args   s<   



rn   rG   c                 C   s|   |sdS t |}i }| D ]#}d| d }||v r/||}| | }|d| ||d d  }qt|dkr:d|fS ||fS )zUse args dict from create_sweep_command_args to construct entrypoint.

    If replace is True, remove macros from entrypoint, fill them in with args
    and then return the args in separate return value.
    )NNz${}Nr   r   )rX   indexr   )rG   rI   entry_point
macro_argsmacromstridxr!   r!   r"   make_launch_sweep_entrypoint   s   
rv   
public_api	PublicApirC   c              
   C   sP   |sdS z|  | W dS  ty' } ztd|  W Y d}~dS d}~ww )zCheck if the job exists using the public api.

    Returns: True if no job is passed, or if the job exists.
    Returns: False if the job is misformatted or doesn't exist.
    TzFailed to load job. NF)rC   	Exceptionr.   r4   )rw   rC   er!   r!   r"   check_job_exists  s   r{   run_specc                 C   sl   |  di  di  di }|  dr| d |d< |  dr$| d |d< |  di  di  di }||fS )zYParse through previous scheduler run_spec.

    returns scheduler_args and settings.
    	overrides
run_config	schedulerresourceresource_argssettings)r   )r|   scheduler_argsr   r!   r!   r"   get_previous_args*  s   

r   )NF)N)%re   rO   retypingr   r   r   r   r   r   r   r5   r.   r	   wandb.sdk.launch.errorsr
   wandb.apis.publicr   rx   r   r   __annotations__compilerK   r   r#   r,   r0   r;   r>   rd   rH   rX   rn   rv   r{   r   r!   r!   r!   r"   <module>   s^   
 $
!

A0



