c++ - 如何防止 yaml-cpp 解析器删除所有注释?

标签 c++ comments yaml

我有一个项目需要读取一个记录良好的 yaml 文件,修改几个值,然后将其写回。问题在于 yaml-cpp 完全去除所有注释并“吃掉”它们。有趣的是,YAML::Emitter 类允许向输出添加注释。有没有办法保留输入中的注释并将它们写回到我看不到的库中?因为就目前而言,我看不到任何使用 YAML::Parser 类的方式(它使用 YAML::Scanner 类,其中注释本身是实际上是“吃掉了”)。

最佳答案

根据YAML spec

Comments are a presentation detail and must not have any effect on the serialization tree or representation graph

因此您需要使解析器不兼容以保留注释,如果 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 文件中的注释

关于c++ - 如何防止 yaml-cpp 解析器删除所有注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39679289/

相关文章:

C++ : How can I know the size of Base class SubObject?

c++ - 通过 TCP 实现 TLV 协议(protocol)

c++ - 使用 Python 删除 C 和 C++ 注释?

android-studio - 无法在 Android Studio 中注释代码块

amazon-web-services - 将 EC2 实例放置在特定子网中时出现 CloudFormation 模板错误

C++ - 没有执行循环

c++ - 是否有人建议扩展 C++ 语言以消除 pimpl?

c# - C#XML注释中的尖括号

php - 在 linuxmint 中安装 php yaml

amazon-web-services - Cloudformation yaml-在 AutoScalingGroup 资源中生成实例列表