如何解析和附加到flexget yaml配置文件?

时间:2023-01-15 09:13:50

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.

虽然它不是一个巨大的差异(只是' - '后面有两个空格的线)我认为它可能会在稍后阶段弄乱配置。

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. Or writing each show line by line into the file (or string before hand) with the correct spacing at the start.

目前我正在考虑对文件进行解析并为每行以“ - ”符号添加2个空格的可能性。或者将每个节目逐行写入文件(或手工字符串),并在开始时使用正确的间距。

But I figure there must be a better way.

但我认为必须有更好的方法。

2 个解决方案

#1


1  

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


0  

I went ahead and implemented a simple function to parse the file that is given out to add the extra 2 spaces to the affected lines as it takes less than a second to complete and is unnoticable.

我继续实现了一个简单的函数来解析给出的文件,以便为受影响的行添加额外的2个空格,因为它需要不到一秒的时间才能完成并且不可察觉。

#1


1  

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


0  

I went ahead and implemented a simple function to parse the file that is given out to add the extra 2 spaces to the affected lines as it takes less than a second to complete and is unnoticable.

我继续实现了一个简单的函数来解析给出的文件,以便为受影响的行添加额外的2个空格,因为它需要不到一秒的时间才能完成并且不可察觉。