Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
B
ú{‹`Hã@sfdZddlZddlZddlmZddlZddlmZddl    m
Z
dZ Gdd„deƒZed    œd
d „Z dS) z9
@author:  l1aoxingyu
@contact: sherlockliao01@gmail.com
éN)ÚAny)ÚCfgNode)Ú PathManagerZ_BASE_csneZdZdZedeedœdd„ƒZdeedœdd„Z‡fd    d
„Z    e
d œ‡fd d „ Z ee dœ‡fdd„ Z ‡ZS)raM
    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.
    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.
    F)ÚfilenameÚ allow_unsafec st |d¡v}yt |¡}Wn`tjjk
r||s6‚t t¡}|     d 
|¡¡|  ¡t|dƒ}t  |¡}WdQRXYnXWdQRX‡fdd„‰t |kr
|t }| d¡r¼tj |¡}tt|jddd    gƒƒsètj tj |¡|¡}tj||d
}|t =ˆ||ƒ|S|S) a 
        Just like `yaml.load(open(filename))`, but inherit attributes from its
            `_BASE_`.
        Args:
            filename (str): the file name 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
        ÚrzlLoading config {} with yaml.unsafe_load. Your machine may be at risk if the file contains malicious content.Ncs`xZ| ¡D]N\}}t|tƒrP||krPt||tƒs@td |¡ƒ‚ˆ|||ƒq
|||<q
WdS)Nz"Cannot inherit key '{}' from base!)ÚitemsÚ
isinstanceÚdictÚAssertionErrorÚformat)ÚaÚbÚkÚv)Úmerge_a_into_b©ú./config/config.pyrCs z3CfgNode.load_yaml_with_base.<locals>.merge_a_into_bú~ú/zhttps://zhttp://)r)rÚopenÚyamlZ    safe_loadÚ constructorZConstructorErrorÚloggingÚ    getLoggerÚ__name__Úwarningr ÚcloseZ unsafe_loadÚBASE_KEYÚ
startswithÚosÚpathÚ
expanduserÚanyÚmapÚjoinÚdirnamerÚload_yaml_with_base)rrÚfÚcfgÚloggerZ base_cfg_fileZbase_cfgr)rrr'%s8 
 $
 
 
 
zCfgNode.load_yaml_with_base)Ú cfg_filenamercCstj||d}t|ƒ|ƒ}dS)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`.
        )rN)rr'Útype)Úselfr+rZ
loaded_cfgrrrÚmerge_from_filebs
zCfgNode.merge_from_filecs"t|kstd t¡ƒ‚tƒ |¡S)zO
        Args:
            cfg_other (CfgNode): configs to merge from.
        z0The reserved key '{}' can only be used in files!)rr r ÚsuperÚmerge_from_other_cfg)r-Z    cfg_other)Ú    __class__rrr0qs
 zCfgNode.merge_from_other_cfg)Úcfg_listcs4t|ddd…ƒ}t|ks(td t¡ƒ‚tƒ |¡S)zS
        Args:
            cfg_list (list): list of configs to merge from.
        rNéz0The reserved key '{}' can only be used in files!)Úsetrr r r/Úmerge_from_list)r-r2Úkeys)r1rrr5}s
 zCfgNode.merge_from_list)ÚnameÚvalcsT| d¡rB||kr8||}||kr&dStd |||¡ƒ‚|||<ntƒ ||¡dS)NZ    COMPUTED_zOComputed attributed '{}' already exists with a different value! old={}, new={}.)rÚKeyErrorr r/Ú __setattr__)r-r7r8Zold_val)r1rrr:ˆs
 
zCfgNode.__setattr__)F)F)rÚ
__module__Ú __qualname__Ú__doc__Ú staticmethodÚstrÚboolr'r.r0Úlistr5rr:Ú __classcell__rr)r1rrs<  r)ÚreturncCsddlm}| ¡S)z]
    Get a copy of the default config.
    Returns:
        a fastreid CfgNode instance.
    é)Ú_C)ÚdefaultsrEÚclone)rErrrÚget_cfg™s rH) r=rr ÚtypingrrZ yacs.configrZ_CfgNodeÚ utils.file_iorrrHrrrrÚ<module>s