MCPcopy Create free account
hub / github.com/VisionRush/DeepFakeDefenders / CfgNode

Class CfgNode

toolkit/yacs.py:63–440  ·  view source on GitHub ↗

CfgNode represents an internal node in the configuration tree. It's a simple dict-like container that allows for attribute-based access to keys.

Source from the content-addressed store, hash-verified

61
62
63class CfgNode(dict):
64 """
65 CfgNode represents an internal node in the configuration tree. It's a simple
66 dict-like container that allows for attribute-based access to keys.
67 """
68
69 IMMUTABLE = "__immutable__"
70 DEPRECATED_KEYS = "__deprecated_keys__"
71 RENAMED_KEYS = "__renamed_keys__"
72 NEW_ALLOWED = "__new_allowed__"
73
74 def __init__(self, init_dict=None, key_list=None, new_allowed=False):
75 """
76 Args:
77 init_dict (dict): the possibly-nested dictionary to initailize the CfgNode.
78 key_list (list[str]): a list of names which index this CfgNode from the root.
79 Currently only used for logging purposes.
80 new_allowed (bool): whether adding new key is allowed when merging with
81 other configs.
82 """
83 # Recursively convert nested dictionaries in init_dict into CfgNodes
84 init_dict = {} if init_dict is None else init_dict
85 key_list = [] if key_list is None else key_list
86 init_dict = self._create_config_tree_from_dict(init_dict, key_list)
87 super(CfgNode, self).__init__(init_dict)
88 # Manage if the CfgNode is frozen or not
89 self.__dict__[CfgNode.IMMUTABLE] = False
90 # Deprecated options
91 # If an option is removed from the code and you don't want to break existing
92 # yaml configs, you can add the full config key as a string to the set below.
93 self.__dict__[CfgNode.DEPRECATED_KEYS] = set()
94 # Renamed options
95 # If you rename a config option, record the mapping from the old name to the new
96 # name in the dictionary below. Optionally, if the type also changed, you can
97 # make the value a tuple that specifies first the renamed key and then
98 # instructions for how to edit the config file.
99 self.__dict__[CfgNode.RENAMED_KEYS] = {
100 # 'EXAMPLE.OLD.KEY': 'EXAMPLE.NEW.KEY', # Dummy example to follow
101 # 'EXAMPLE.OLD.KEY': ( # A more complex example to follow
102 # 'EXAMPLE.NEW.KEY',
103 # "Also convert to a tuple, e.g., 'foo' -> ('foo',) or "
104 # + "'foo:bar' -> ('foo', 'bar')"
105 # ),
106 }
107
108 # Allow new attributes after initialisation
109 self.__dict__[CfgNode.NEW_ALLOWED] = new_allowed
110
111 @classmethod
112 def _create_config_tree_from_dict(cls, dic, key_list):
113 """
114 Create a configuration tree using the given dict.
115 Any dict-like objects inside dict will be treated as a new CfgNode.
116
117 Args:
118 dic (dict):
119 key_list (list[str]): a list of names which index this CfgNode from the root.
120 Currently only used for logging purposes.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected