我想使用自动化来使用python 3脚本创建hocon配置。我读到了光明(https://github.com/lightbend/config)推荐的pyhocon (https://github.com/chimpler/pyhocon)。
我在找出如何创建hocon对象并将数据作为Hocon写入文件时遇到了问题。对我来说,替换的语法在结果中很重要。
例如,我期望文件myconfig.conf的输出如下所示:
{
Environment: "dev"
JobName: ${Environment}"-hello-bob"
}
所以,我认为有一种方法可以这样做:
config2 = ConfigFactory.parse_string("{}")
config2.put("Environment", "dev")
#Some type of object for references or special syntax for ${Environment}
config2.put("JobName", "${Environment}")
然后,在创建填充对象之后,应该有一种简单的方法将其写入一个或多个文件:
filename = "myconfig.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(config2.to_hocon_str)
有人想出办法了吗?这个库只能用于读取数据,这似乎很奇怪。
发布于 2019-12-20 19:10:10
因此,我决定查看JVM (Java/Scala)库(https://github.com/lightbend/config)的文档。在阅读了文档之后,有了一个关于hocon示例(https://github.com/lightbend/config#examples-of-hocon)的明确章节。在本文档中,他们对7种有效的hocon样式进行了分类。我之所以调用这些样式,是因为如果我要自动生成这些文件,我就会选择一种方式来写出并坚持它。
所有这些都是有效的HOCON。
1.从有效的JSON开始:
{
"foo" : {
"bar" : 10,
"baz" : 12
}
}
2.放下牙根支撑:
"foo" : {
"bar" : 10,
"baz" : 12
}
3.删除引号:
foo : {
bar : 10,
baz : 12
}
4.在{:
foo {
bar = 10,
baz = 12
}
5.删除逗号:
foo {
bar = 10
baz = 12
}
6.对未引号的键使用虚线符号:
foo.bar=10
foo.baz=12
7.将虚线符号字段放在一行上:
foo.bar=10, foo.baz=12
因为我将使用pyhocon库,所以我需要在库中寻找写解决方案。我从chimpler's git (https://github.com/chimpler/pyhocon)那里找到了一些帮助。我发现它们有两种风格,可以简单地写出来。一个是json,另一个是没有出现在列表上的东西,上面用Light弯曲来描述。
风格1:纯JSON,女巫可以用两种方式写出:
HOCONConverter.to_json
#Using HOCONConverter.to_json
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./json_coverted.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(HOCONConverter.to_json(confTree))
HOCONConverter.to_json结果
{
"Environment": "Dev",
"Test": "${Environment}"
}
或者使用json.dump
#Using json.dump
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./json_dumped.conf"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(json.dumps(confTree,indent=4))
使用json.dump结果
{
"Environment": "Dev",
"Test": "${Environment}"
}
Pyhocon的另一种风格,没有列在lightbend的名单上
# HOCONConverter.to_hocon
confTree = ConfigFactory.parse_string("{}")
confTree.put("Environment","Dev")
confTree.put("Test","${Environment}")
filename = "./hocon_coverted.txt"
print("Write to disk as {}".format(filename))
with open(filename, "w") as fd:
fd.write(HOCONConverter.to_hocon(confTree))
Pyhocon的另一种风格,没有按光弯道结果列出
Environment = "Dev"
Test = "${Environment}"
因此,要回答我自己的问题,使用Python 3中的pyhocon动态生成hocon conf文件的唯一可靠方法是使用json方法之一(转换器或转储)。但这仍是一个悬而未决的问题。问题是,将json读取到pyhocon ConfTree对象是否能够取消替换在json中的引用?
例如,如果我读取该文件
{
"Environment": "Dev",
"Test": "${Environment}"
}
ConfTree对象会得到"Dev“作为测试的值吗?
不,不会的。这是我的测试
filename = "json_coverted.conf"
print("Reading file{}".format(filename))
conf = ConfigFactory.parse_file(filename)
key="Test"
value=conf.get(key)
print("Key:{} Value:{}".format(key,value))
测试结果显示在屏幕上
Reading filejson_coverted.conf
Key:Test Value:${Environment}
那么,人们是如何使用pyhocon替换的呢?
因此,我不会用任何一个库来写甜点。如果我想使用替换的话,这必须是一个手动的过程。所以,我只使用这个库来阅读糖果。
发布于 2020-10-05 11:46:52
这个例子可能会回答你的问题
from pyhocon.converter import HOCONConverter
import pyhocon
string = '{"Environment": "Dev","Test": ${Environment}}'
factory = pyhocon.ConfigFactory.parse_string(string, resolve=True)
factory.put('somekey','somevalue')
print(HOCONConverter().to_hocon(factory))
返回
Environment = "Dev"
Test = "Dev"
somekey = "somevalue"
https://stackoverflow.com/questions/59417529
复制相似问题