灵活地将新数据附加到 yaml 文件 [英] Flexible appending new data to yaml files

查看:62
本文介绍了灵活地将新数据附加到 yaml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的 yaml 文件,它们可能具有不同的嵌套结构

I have different yaml files which may have different nested structure

file1.yaml:

file1.yaml:

test3:
  service1: 
      name1: |
        "somedata"
      name2: |
          "somedata"

file2.yaml:

file2.yaml:

test1: 
  app1: 
     app2:|
       "somedata"
  app7:
     key2: | 
       "testapp"

如您所见,yaml 文件的结构可能有所不同.

So as you can see structure of yaml files may be different.

问题是,我可以以某种方式灵活地管理将一些数据附加到这些文件的特定块吗?

The question is, may I somehow flexibly manage of appending some data to particular blocks of these files?

例如在file1中我想在name1和name 2的keys或service1的级别上写key vaue:

For example in file1 I want to write key vaue on the level of name1 and name 2 keys or service1:

test3:
  service1: 
      name1: |
        "somedata"
      name2: |
          "somedata"
      my-appended-key:| 
              "my appended value"
  my_second_appended_key: | 
          "my second appended valye"

等等.

所以这个想法是能够指定我想在 yaml 中的哪个嵌套块下附加数据.

So the idea is to be able to specify under which nested block in yaml I want to append a data.

我有不同的 yaml 文件,它们可能具有不同的嵌套结构

I have different yaml files which may have different nested structure

file1.yaml:

file1.yaml:

test3:
  service1: 
      name1: |
        "somedata"
      name2: |
          "somedata"

file2.yaml:

file2.yaml:

test1: 
  app1: 
     app2:|
       "somedata"
  app7:
     key2: | 
       "testapp"

如您所见,yaml 文件的结构可能有所不同.

So as you can see structure of yaml files may be different.

问题是,我可以以某种方式灵活地管理将一些数据附加到这些文件的特定块吗?

The question is, may I somehow flexibly manage of appending some data to particular blocks of these files?

例如在file1中我想在name1和name 2的keys或service1的级别上写key vaue:

For example in file1 I want to write key vaue on the level of name1 and name 2 keys or service1:

test3:
  service1: 
      name1: |
        "somedata"
      name2: |
          "somedata"
      my-appended-key:| 
              "my appended value"
  my_second_appended_key: | 
          "my second appended valye"

等等.

现在我针对有关 yaml 文件结构的特定情况进行处理.这是我的代码的一部分:

For now I do it for specific case regarding structure of yaml file. Here is a part of my code:

import gnupg
import re
import argparse

def NewPillarFile():
    with open(args.sensitive) as sensitive_data:
        with open(args.encrypted, "w") as encrypted_result:
            encrypted_result.write('#!yaml|gpg\n\nsecrets:\n    '+args.service+':\n')
            for line in sensitive_data:
                encrypted_value = gpg.encrypt(re.sub(r'^( +?|[A-Za-z0-9]|[A-Za]|[0-9])+( +)?'+args.separator+'( +)?','',line,1), recipients=args.resident, always_trust=True)
                if not encrypted_value.ok:
                    print(encrypted_value.status, '\n', encrypted_value.stderr)
                    break
                line = re.sub(r'^( +)?','',line)
                encrypted_result.write('        '+re.sub(r'( +)?'+args.separator+'.*',': |',line))
                encrypted_result.write(re.sub(re.compile(r'^', re.MULTILINE), '            ', encrypted_value.data.decode())+'\n')

def ExistingPillarFile():
    with open(args.sensitive) as sensitive_data:
        with open(args.encrypted, "a") as encrypted_result:
            encrypted_result.write('    '+args.service+':\n')
            for line in sensitive_data:
                encrypted_value = gpg.encrypt(
                    re.sub(r'^( +?|[A-Za-z0-9]|[A-Za]|[0-9])+( +)?' + args.separator + '( +)?', '', line, 1),
                    recipients=args.resident, always_trust=True)
                if not encrypted_value.ok:
                    print(encrypted_value.status, '\n', encrypted_value.stderr)
                    break
                line = re.sub(r'^( +)?', '', line)
                encrypted_result.write('        ' + re.sub(r'( +)?' + args.separator + '.*', ': |', line))
                encrypted_result.write(re.sub(re.compile(r'^', re.MULTILINE), '            ', encrypted_value.data.decode())+'\n')

所以这个想法是能够指定在 yaml 中的哪个嵌套块下我想附加数据以使脚本更灵活.

So the idea is to be able to specify under which nested block in yaml I want to append a data to make script more flexible.

推荐答案

您可以使用 PyYAML 的低级事件接口.假设您有一个输入 YAML 文件并希望将修改写入输出 YAML 文件,您可以编写一个函数,通过 PyYAML 生成的事件流并在指定位置插入请求的附加值:

You can use PyYAML's low-level event interface. Assuming you have an input YAML file and want to write the modifications to an output YAML file, you can write a function that goes through PyYAML's generated event stream and inserts the requested additional values at the specified locations:

import yaml
from yaml.events import *

class AppendableEvents:
  def __init__(self, path, events):
    self.path = path
    self.events = events

  def correct_position(self, levels):
    if len(self.path) != len(levels):
      return False
    for index, expected in enumerate(self.path):
      if expected != levels[index].cur_id:
        return False
    return True

class Level:
  def __init__(self, mode):
    self.mode = mode
    self.cur_id = -1 if mode == "item" else ""

def append_to_yaml(yamlFile, targetFile, items):
  events = []
  levels = []
  with open(yamlFile, 'r') as handle:
    for event in yaml.parse(handle):
      if isinstance(event, StreamStartEvent) or \
         isinstance(event, StreamEndEvent) or \
         isinstance(event, DocumentStartEvent) or \
         isinstance(event, DocumentEndEvent):
        pass
      elif isinstance(event, CollectionStartEvent):
        if len(levels) > 0:
          if levels[-1].mode == "key":
            # we can only handle scalar keys
            raise ValueError("encountered complex key!")
          else:
            if levels[-1].mode == "value":
              levels[-1].mode = "key"
        if isinstance(event, MappingStartEvent):
          levels.append(Level("key"))
        else: # SequenceStartEvent
          levels.append(Level("item"))
      elif isinstance(event, ScalarEvent):
        if len(levels) > 0:
          if levels[-1].mode == "item":
            levels[-1].cur_id += 1
          elif levels[-1].mode == "key":
            levels[-1].cur_id = event.value
            levels[-1].mode = "value"
          else: # mode == "value"
            levels[-1].mode = "key"
      elif isinstance(event, CollectionEndEvent):
        # here we check whether we want to append anything
        levels.pop()
        for item in items:
          if item.correct_position(levels):
            for additional_event in item.events:
              events.append(additional_event)
      events.append(event)
  with open(targetFile, mode="w") as handle:
    yaml.emit(events, handle)

要使用它,您必须提供要附加为 YAML 事件列表的其他内容,并将所需位置指定为键(或序列索引)列表:

To use it, you must provide the additional stuff you want to append as list of YAML events, and specify the desired position as list of keys (or sequence indexes):

def key(name):
  return ScalarEvent(None, None, (True, True), name)

def literal_value(content):
  return ScalarEvent(None, None, (False, True), content, style="|")

append_to_yaml("file1.yaml", "file1_modified.yaml", [
  AppendableEvents(["test3", "service1"], [
    key("my-appended-key"),
    literal_value("\"my appended value\"\n")]),
  AppendableEvents(["test3"], [
    key("my_second_appended_key"),
    literal_value("\"my second appended value\"\n")])])

此代码正确地将您的 file1.yaml 转换为给定的修改文件.通常,这还允许您附加复杂(序列或映射)节点.下面是如何做到这一点的基本示例:

This code correctly transform your file1.yaml into the given modified file. In general, this also allows you to append complex (sequence or mapping) nodes. Here's a basic example how to do that:

def seq(*scalarValues):
  return [SequenceStartEvent(None, None, True)] + \
    [ScalarEvent(None, None, (True, False), v) for v in scalarValues] + \
    [SequenceEndEvent()]

def map(*scalarValues):
  return [MappingStartEvent(None, None, True)] + \
    [ScalarEvent(None, None, (True, False), v) for v in scalarValues] + \
    [MappingEndEvent()]

append_to_yaml("file1.yaml", "file1_modified.yaml", [
  AppendableEvents(["test3", "service1"], [
    key("my-appended-key")] + seq("one", "two", "three")),
  AppendableEvents(["test3"], [
    key("my_second_appended_key")] + map("foo", "bar"))])

这篇关于灵活地将新数据附加到 yaml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆