o
    ϯi                     @   sh   d dl Z d dlZd dlmZmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZ dZG dd deZdS )    N)AnyCallableDictIOListUnion)	g_pathmgr)CfgNode_BASE_c                
       s   e Zd ZdZededeee ee f fddZ	e	ddede
deeef fdd	Zdd
ede
ddfddZdd deg df f fddZdee deg df f fddZdededdf fddZ  ZS )r	   aQ  
    Our own extended version of :class:`yacs.config.CfgNode`.
    It contains the following extra features:

    1. The :meth:`merge_from_file` method supports the "_BASE_" key,
       which allows the new CfgNode to inherit all the attributes from the
       base configuration file(s).
    2. Keys that start with "COMPUTED_" are treated as insertion-only
       "computed" attributes. They can be inserted regardless of whether
       the CfgNode is frozen or not.
    3. With "allow_unsafe=True", it supports pyyaml tags that evaluate
       expressions in config. See examples in
       https://pyyaml.org/wiki/PyYAMLDocumentation#yaml-tags-and-python-types
       Note that this may lead to arbitrary code execution: you must not
       load a config file from untrusted sources before manually inspecting
       the content of the file.
    filenamereturnc                 C   s   t |dS )zs
        Defines how a config file is opened. May be overridden to support
        different file schemas.
        r)r   open)clsr    r   H/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/common/config.py	_open_cfg"   s   zCfgNode._open_cfgFallow_unsafec           
         s\   I}zt|}W n9 tjjyF    s tt}|d	 |
   }t|}W d   n1 s?w   Y  Y nw W d   n1 sQw   Y  dtttf dtttf ddffdddtdtttf f fd	d
}t|v rt|t tri }|t }|D ]	}	||	| qn|t }	||	}|t= || |S |S )a  
        Just like `yaml.load(open(filename))`, but inherit attributes from its
            `_BASE_`.

        Args:
            filename (str or file-like object): the file name or file of the current config.
                Will be used to find the base config file.
            allow_unsafe (bool): whether to allow loading the config file with
                `yaml.unsafe_load`.

        Returns:
            (dict): the loaded yaml
        zlLoading config {} with yaml.unsafe_load. Your machine may be at risk if the file contains malicious content.Nabr   c                    s\   |   D ]'\}}t|tr'||v r't|| tsJ d| |||  q|||< qd S )Nz"Cannot inherit key '{}' from base!)items
isinstancedictformat)r   r   kv)merge_a_into_br   r   r   L   s   
z3CfgNode.load_yaml_with_base.<locals>.merge_a_into_bbase_cfg_filec                    sN   |  drtj| } tt| j g ds tjtj| } j|  dS )N~)/zhttps://zhttp://r   )	
startswithospath
expanduseranymapjoindirnameload_yaml_with_base)r   )r   r   r   r   r   _load_with_baseW   s
   
z4CfgNode.load_yaml_with_base.<locals>._load_with_base)r   yaml	safe_loadconstructorConstructorErrorlogging	getLogger__name__warningr   closeunsafe_loadr   strr   BASE_KEYr   list)
r   r   r   fcfgloggerr*   base_cfgbase_cfg_filesr   r   )r   r   r   r   r   r)   *   sH   
*"
zCfgNode.load_yaml_with_basecfg_filenameNc                 C   s(   | j ||d}t| |}| | dS )z
        Merge configs from a given yaml file.

        Args:
            cfg_filename: the file name of the yaml config.
            allow_unsafe: whether to allow loading the config file with
                `yaml.unsafe_load`.
        r    N)r)   typemerge_from_other_cfg)selfr=   r   
loaded_cfgr   r   r   merge_from_filen   s   	zCfgNode.merge_from_file	cfg_otherc                    s"   t |vsJ dt t |S )zO
        Args:
            cfg_other (CfgNode): configs to merge from.
        0The reserved key '{}' can only be used in files!)r6   r   superr?   )r@   rC   	__class__r   r   r?   |   s   
zCfgNode.merge_from_other_cfgcfg_listc                    s4   t |ddd }t|vsJ dtt |S )zS
        Args:
            cfg_list (list): list of configs to merge from.
        r   N   rD   )setr6   r   rE   merge_from_list)r@   rH   keysrF   r   r   rK      s
   
zCfgNode.merge_from_listnamevalc                    sV   | dr"|| v r| | }||krd S td||||| |< d S t || d S )N	COMPUTED_zOComputed attributed '{}' already exists with a different value! old={}, new={}.)r!   KeyErrorr   rE   __setattr__)r@   rM   rN   old_valrF   r   r   rQ      s   

zCfgNode.__setattr__)F)r1   
__module____qualname____doc__classmethodr5   r   r   bytesr   boolr   r   r)   rB   r   r?   r   rK   rQ   __classcell__r   r   rF   r   r	      s"    $
C"
"r	   )r/   r"   typingr   r   r   r   r   r   r+   iopath.common.file_ior   yacs.configr	   _CfgNoder6   r   r   r   r   <module>   s    