Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
config1 = ConfigFactory.parse_string(
"""
x : { v1: 1 }
b1 : {v2: 2 }
b = [${b1}]
""",
resolve=False
)
config2 = ConfigFactory.parse_string(
"""
b2 : ${x} {v2: 3}
b += [${b2}]
""",
resolve=False
)
merged = ConfigTree.merge_configs(config1, config2)
ConfigParser.resolve_substitutions(merged)
b = merged.get("b")
assert len(b) == 2
assert b[0] == {'v2': 2}
assert b[1] == {'v1': 1, 'v2': 3}
def test_self_merge_ref_substitutions_object3(self):
config1 = ConfigFactory.parse_string(
"""
b1 : { v1: 1 }
b = [${b1}]
""",
resolve=False
)
config2 = ConfigFactory.parse_string(
"""
b1 : { v1: 2, v2: 3 }
""",
resolve=False
)
merged = ConfigTree.merge_configs(config1, config2)
resolved = ConfigParser.resolve_substitutions(merged)
assert resolved.get("b1") == {"v1": 2, "v2": 3}
b = resolved.get("b")
assert len(b) == 1
assert b[0] == {"v1": 2, "v2": 3}
config1 = ConfigFactory.parse_string(
"""
a : { }
b : 1
c : ${a} { d : [ ${b} ] }
""",
resolve=False
)
config2 = ConfigFactory.parse_string(
"""
e : ${a} {
}
""",
resolve=False
)
merged = ConfigTree.merge_configs(config1, config2)
resolved = ConfigParser.resolve_substitutions(merged)
assert resolved.get("c.d") == [1]
def parse_requirements(self, reqs_file=None, overrides=None):
os = None
session = self._session
if overrides:
overrides = ConfigFactory.parse_string("\n".join(overrides))
os = overrides.pop("os", None)
ConfigTree.merge_configs(session.config, overrides)
if reqs_file:
contents = Path(reqs_file).read_text()
else:
contents = ensure_text(sys.stdin.read())
session.finalize_config(session.config)
requirements_manager = self._get_requirements_manager(os_override=os)
requirements_manager.translator.enabled = False
print(requirements_manager.replace(contents))
@classmethod
def merge(cls, configs):
for c in configs:
assert isinstance(c, Config)
ctree = configs[0]._config_tree
for c in configs[1:]:
ctree = ConfigTree.merge_configs(ctree, c._config_tree)
return cls(ctree)