o
    xi'                     @   s   d dl Z d dlZd dlmZ d dlZddlmZ dZG dd dZde	d	dfd
dZ
de	d	dfddZde	ded	dfddZde	d	efddZdeded	dfddZdS )    N)Any   )LaunchErrorWANDB_LAUNCH_FILE_OVERRIDESc                   @   s&   e Zd ZdZdZdd ZdddZdS )	FileOverrideszCSingleton that read file overrides json from environment variables.Nc                 C   s.   | j d u rt| | _ i | j _| j   | j S N)	_instanceobject__new__	overridesload)cls r   Q/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/launch/inputs/files.pyr
      s
   

zFileOverrides.__new__returnc                 C   s   t jt}|du r:t dt jv r:d}d}t d| t jv r:|t jt d|  7 }|d7 }t d| t jv s |rczt|}t|tsNtdt || _	W dS  tj
yb   tdt w dS )z,Load overrides from an environment variable.N_0 r   _   zInvalid JSON in )osenvirongetFILE_OVERRIDE_ENV_VARjsonloads
isinstancedictr   r   JSONDecodeError)selfr   idxcontentsr   r   r   r      s$   

zFileOverrides.load)r   N)__name__
__module____qualname____doc__r   r
   r   r   r   r   r   r      s
    r   pathr   c                    s   t j rtd  dd v rtd  dt j  t j s.td  dt fdddD sAtd  d	d
S )a  Validate a config file path.

    This function checks if a given config file path is valid. A valid path
    should meet the following criteria:

    - The path must be expressed as a relative path without any upwards path
      traversal, e.g. `../config.json`.
    - The file specified by the path must exist.
    - The file must have a supported extension (`.json`, `.yaml`, or `.yml`).

    Args:
        path (str): The path to validate.

    Raises:
        LaunchError: If the path is not valid.
    zInvalid config path: z!. Please provide a relative path.z..zZ. Please provide a relative path without any upward path traversal, e.g. `../config.json`.z. File does not exist.c                 3   s    | ]}  |V  qd S r   )endswith).0extr%   r   r   	<genexpr>H   s    z'config_path_is_valid.<locals>.<genexpr>).json.yaml.ymlz). Only JSON and YAML files are supported.N)r   r%   isabsr   normpathexistsanyr)   r   r)   r   config_path_is_valid+   s    


r2   c                 C   sL   t  }| |jv r"|j| }|dur$t| }t|| t| | dS dS dS )zDCheck for file overrides in the environment and apply them if found.N)r   r   r   _read_config_file_update_dict_write_config_file)r%   file_overridesr   configr   r   r   override_fileN   s   

r8   r7   c                 C   s   t j| \}}|dkr,t| d}tj||dd W d   dS 1 s%w   Y  dS |dv rNt| d}t|| W d   dS 1 sGw   Y  dS td| )zWrite a config file to disk.

    Args:
        path (str): The path to the config file.
        config (Any): The contents of the config file as a Python object.

    Raises:
        LaunchError: If the file extension is not supported.
    r+   wr   )indentNr,   r-   Unsupported file extension: )	r   r%   splitextopenr   dumpyaml	safe_dumpr   )r%   r7   r   r(   fr   r   r   r5   Y   s   
""r5   c                 C   s   t j| \}}|dkr't| }t|W  d   S 1 s w   Y  dS |dv rFt| }t|W  d   S 1 s?w   Y  dS td| )zRead a config file from disk.

    Args:
        path (str): The path to the config file.

    Returns:
        Any: The contents of the config file as a Python object.
    r+   Nr;   r<   )	r   r%   r=   r>   r   r   r@   	safe_loadr   )r%   r   r(   rB   r   r   r   r3   n   s    	$$r3   targetsourcec                 C   sH   |  D ]\}}t|tr|| vri | |< t| | | q|| |< qdS )zUpdate a dictionary with the contents of another dictionary.

    Args:
        target (Dict): The dictionary to update.
        source (Dict): The dictionary to update from.
    N)itemsr   r   r4   )rD   rE   keyvaluer   r   r   r4      s   

r4   )r   r   typingr   r@   errorsr   r   r   strr2   r8   r5   r3   r   r4   r   r   r   r   <module>   s    #