根据YAML spec
评论是一个展示细节,不能对序列化树或表示图有任何影响
因此,您需要使解析器不兼容以保留 cmets,如果 yaml-cpp 这样做,他们应该在文档中明确说明。
我在ruamel.yaml 中为 Python 做了这个。如果可以接受从您的 C++ 程序嵌入和调用 Python,您可以执行以下操作(我在 Linux Mint 下为此使用了 Python 3.5):
pythonyaml.cpp:
#include <Python.h>
int
update_yaml(const char*yif, const char *yof, const char* obj_path, int val)
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
const char *modname = "update_yaml";
const char *lus = "load_update_save";
Py_Initialize();
// add current directory to search path
PyObject *sys_path = PySys_GetObject("path");
PyList_Append(sys_path, PyUnicode_FromString("."));
pName = PyUnicode_DecodeFSDefault(modname);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, lus);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(4);
PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(yif));
PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(yof));
PyTuple_SetItem(pArgs, 2, PyUnicode_FromString(obj_path));
PyTuple_SetItem(pArgs, 3, PyLong_FromLong(val));
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Old value: %ld\n", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", lus);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", modname);
return 1;
}
Py_Finalize();
return 0;
}
int
main(int argc, char *argv[])
{
const char *yaml_in_file = "input.yaml";
const char *yaml_out_file = "output.yaml";
update_yaml(yaml_in_file, yaml_out_file, "abc.1.klm", 42);
}
创建一个Makefile(适应你的Python3.5安装的路径,它需要安装头文件,如果从源代码编译是正常的,否则你需要安装包python3-dev):
echo -e "SRC:=pythonyaml.cpp\n\ncompile:\n\tgcc \$(SRC) $(/opt/python/3.5/bin/python3-config --cflags --ldflags | tr --delete '\n' | sed 's/-Wstrict-prototypes//') -o pythonyaml" > Makefile
用make编译程序。
创建update_yaml.py,将由pythonyaml加载:
# coding: utf-8
import traceback
import ruamel.yaml
def set_value(data, key_list, value):
"""key list is a set keys to access nested dict and list
dict keys are assumed to be strings, keys for a list must be convertable to integer
"""
key = key_list.pop(0)
if isinstance(data, list):
key = int(key)
item = data[key]
if len(key_list) == 0:
data[key] = value
return item
return set_value(item, key_list, value)
def load_update_save(yaml_in, yaml_out, obj_path, value):
try:
if not isinstance(obj_path, list):
obj_path = obj_path.split('.')
with open(yaml_in) as fp:
data = ruamel.yaml.round_trip_load(fp)
res = set_value(data, obj_path.split('.'), value)
with open(yaml_out, 'w') as fp:
ruamel.yaml.round_trip_dump(data, fp)
return res
except Exception as e:
print('Exception', e)
traceback.print_exc() # to get some useful feedback if your python has errors
创建input.yaml:
abc:
- zero-th item of list
- klm: -999 # the answer?
xyz: last entry # another comment
如果您在 python3.5 中安装了ruamel.yaml 并运行./python_yaml,它将打印Old value: -999,新文件output.yaml 将包含:
abc:
- zero-th item of list
- klm: 42 # the answer?
xyz: last entry # another comment
- 虽然
42 只有两个字符,而-999 有四个字符,但注释仍然与其下方的一个对齐
- 您可以创建一个 Python 列表,而不是提供一个虚线路径
abc.1.klm
在 C++ 中,并将其作为第三个参数传递给 load_update_save()。在这种情况下,您可以拥有不是字符串的其他项的键,或者是包含点的字符串的键
- 根据您的使用情况,您可能希望更改硬编码假设,即为该值设置一个整数(
PyLong_FromLong 用于第四个参数)。 python 程序不需要为此更新。
- 您可以对输入和输出使用相同的文件名,以覆盖输入。
-
是可以使用
ruamel.yaml 更改 python 文件中的注释