我有一个HOCON文件模板和一些类似属性的配置。此文件针对不同的“名称”(由用户输入提供)进行更新并上载。我试图通过拉取模板、更新必要的值并上传更新后的文件来构建hocon文件。
deployment {
proxy {
// Name has to be replaced with the name of the project
cluster.NAME {
property1 = [a_list]
property2.host = "hostname"
}
}
}我可以使用pyhocon更新值:
from pyhocon import ConfigFactory
conf = ConfigFactory.parse_string(hocon_file_template)
host = "something-TEST.trial.com"
conf.put('deployment.proxy.cluster.NAME.property2.host', host)
new = HOCONConverter.convert(conf, "hocon")但是,我需要将"cluster.NAME“中的”user_input“替换为”测试“。我尝试使用put更改名称,但这会附加到簇树中,而不是将名称更新为"TEST“
host_key = 'deployment.proxy.cluster.' ".{}.property2.host"
conf.put(host_key.format(user_input), host)如何将NAME更新为输入值(在本例中为"TEST")?
发布于 2021-03-25 21:54:23
我能够使用pop删除删除指定的键,即"NAME“,并使用put将键"TEST”添加到树中。
from pyhocon import ConfigFactory
conf = ConfigFactory.parse_string(hocon_file_template)
# Remove NAME from template
conf.pop("deployment.proxy.cluster.NAME")
host = "something-TEST.trial.com"
# Use Test to update required properties
conf.put('deployment.proxy.cluster.TEST.property2.host', host)
new = HOCONConverter.convert(conf, "hocon")https://stackoverflow.com/questions/66789777
复制相似问题