简体   繁体   English

如何解析并追加到flexget yaml配置文件?

[英]How to parse and append to flexget yaml config file?

I am trying to parse and add / remove a show from a flexget config file. 我正在尝试从flexget配置文件中解析和添加/删除节目。

I eventualy plan to create a small webpage with checkboxes beside the show names and add / remove them but first I need to get the yaml parseing done right at the moment I have this code: 我最终打算在显示名称旁边创建一个带有复选框的小型网页,然后添加/删除它们,但是首先,我需要立即获得以下代码的yaml解析:

#!/usr/bin/env python
import yaml
with open("shows.yml") as f:
        doc = yaml.safe_load(f)

shows = doc['series']['shows']
sets = doc['series']['settings']
shows.append('new show')
# this makes the list a little neater while printed to the file
showsa = []
for a in test:
  showsa.append(a)

testa = {'series': { 'shows': showsa, 'settings': sets }}

with open("showsnew.yml", 'w') as g:
        g.write( yaml.dump(testa, default_flow_style=False) )

This opens the old config adds a new show to the list and then prints it out to the file and it is almost perfect except the outputed config is a little messed up like this: 这将打开旧的配置,将新的显示添加到列表中,然后将其显示到文件中,除了输出的配置有点像这样,它几乎是完美的:

**What I should get is:**                    **But instead I get:**
shows:                                         shows:
  settings:                                      settings:
    shows:                                         shows:
      setting1: value                                setting1: value
      setting2:                                      setting2:
        - value                                      - value
        - value                                      - value
  shows:                                         shows:
    - show1                                      - show1
    - show2                                      - show2
    - new show                                   - new show

While its not a massive difference (just the lines with '-' being 2 spaces back) I think it could end up messing up the config at a later stage. 虽然它没有太大的区别(只是带有'-'的行退了2个空格),但我认为它可能最终会在以后弄乱配置。

I know its simple enough to add and delete a show manualy but as I use a database for other show information it would be handy to be able to script adding the show to flexget and the database and a few other tasks. 我知道它足够简单,可以手动添加和删除表演,但是当我使用数据库来获取其他表演信息时,能够通过脚本将表演添加到flexget和数据库以及其他一些任务将非常方便。

Does anyone have any idea what small thing I am doing wrong? 有人知道我做错了什么小事吗? or is this just how the pyYaml in python works? 还是这就是python中的pyYaml的工作方式?

At the moment I am considering the possibility of doing a parse of the file and adding 2 spaces to each line that starts with the '-' symbol. 目前,我正在考虑对文件进行解析并在以'-'开头的每一行中添加2个空格的可能性。 Or writing each show line by line into the file (or string before hand) with the correct spacing at the start. 或以开始时正确的间距将每个节目逐行写入文件(或之前的字符串)。

But I figure there must be a better way. 但我认为必须有更好的方法。

I just stumbled in the same exact problem right now. 我现在偶然发现了同样的问题。
I came up with this : 我想出了这个:

def writeYaml(data, filename):

  sections = data.keys()
  dump = yaml.dump(data, default_flow_style=False)

  # set 2 indent spaces in every line
  out = dump.replace('\n', '\n  ')
  # re-set indentation for sections only
  for i in sections:
      out = out.replace('\n  %s' % i, '\n%s' % i)

  with open(filename, 'w') as yaml_file:
      yaml_file.write(out)

我继续并实现了一个简单的函数来解析该文件,该文件给出了将多余的2个空格添加到受影响的行的功能,因为它只需不到一秒钟的时间即可完成并且不引人注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM