如何在 Python 3 中使用 pyhocon 动态生成 Hocon conf 文件?

How can I generate Hocon conf file dynamically using pyhocon in Python 3?

我想使用自动化来通过 python 3 脚本创建 hocon 配置。我读了 lightbend (https://github.com/lightbend/config) recommends 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)

有没有人想办法做到这一点?奇怪的是这个库只能用来读取数据。

因此,我决定查看 JVM (Java/Scala) 库 (https://github.com/lightbend/config). After reading the documentation, there was a clear section on hocon examples (https://github.com/lightbend/config#examples-of-hocon) 的文档。在本文档中,他们对 7 种有效的 hocon 样式进行了分类。我称这些样式是因为如果我要自动生成这些文件,我会选择一种方式来写出来并坚持使用它。

所有这些都是有效的 HOCON。

1.Start 有效 JSON:

{
    "foo" : {
        "bar" : 10,
        "baz" : 12
    }
}

2.Drop 根大括号:

"foo" : {
    "bar" : 10,
    "baz" : 12
}

3.Drop 引用:

foo : {
    bar : 10,
    baz : 12
}

4.Use = 并在 {:

之前省略它
foo {
    bar = 10,
    baz = 12
}

5.Remove 逗号:

foo {
    bar = 10
    baz = 12
}

6.Use 不带引号的键的点符号:

foo.bar=10
foo.baz=12

7.Put 单行上的点分字段:

foo.bar=10, foo.baz=12

因为我将使用 pyhocon 库,所以我需要在库中寻找写入解决方案。我从 chimpler 的 git (https://github.com/chimpler/pyhocon) 那里找到了一些帮助。我发现他们有两种 hocon 风格,可以简单地写出来。一个是 json,另一个是 lightbend 上面描述的列表中没有的东西。

样式一:纯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的其他Style,未被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的其他Style,未被lightbend Result列出

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 呢?

它不能因此,我不会使用任何一个库来写出 confs。如果我想使用替换,它必须是一个手动过程。所以,我只使用这个库来阅读 confs。

这个例子可能会回答您的问题

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))

returns

Environment = "Dev"
Test = "Dev"
somekey = "somevalue"