Question

I am trying to parse and add / remove a show from a flexget config file.

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:

#!/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.

Does anyone have any idea what small thing I am doing wrong? or is this just how the pyYaml in python works?

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.

But I figure there must be a better way.

Était-ce utile?

La solution

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)

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top