Search code examples
pythonpython-3.xyamlpyyaml

YAML just upload "part" without "urls"


Hey i have yaml code below and i want to add parts using python i.e. part-4,.... In doing so I only manage to create urls using the which I don't want.... can anyone help me? Current codeblock from python:

d = {'urls':{f'part-{i}': {'updated': 16662709610, 'url': 'http://any-do0main1.com/', 'url-name': f'crl{i}.der'}}
with open('test.yaml', 'a+') as yaml_file:
    yaml.dump(d, yaml_file, default_flow_style=False)

yaml:

urls:
  part-0:
    updated: 1666270610
    url: http://any-domain1.com/
    url-name: cr0.der
  part-1:
    updated: 1666270610
    url: http://crl.domain/
    url-name: crl1.der
  part-2:
    updated: 1666270900
    url: http://crl.swisssign.net/
    url-name: crl2.der
urls:
  part-3:
    updated: 16662709610
    url: http://any-do0main1.com/
    url-name: cr3.der 

Solution

  • Not sure that you can append a YAML like a regular file. Here is a possible solution:

    import yaml
    import re
    
    with open('test.yaml', 'r') as yaml_file_loaded:
        yaml_loaded = yaml.safe_load(yaml_file_loaded)  #load yaml
        part_keys = list(yaml_loaded['urls'].keys())  #get all part-# keys
        all_numbers = []
        for i in part_keys:  #parse all part+#
            all_numbers = all_numbers + re.findall(r'\d+',i) #get all # as str
        all_numbers = [int(i) for i in all_numbers]  # convert all # to int
        max_num = max(all_numbers)
        i = max_num + 1 # increment i with 1 from largest part-#
    # d is modified arbitrarily.
    d = {f'part-{i}': {'updated': 16662709610+i, 'url': f'http://any-do0maini{i}.com/', 'url-name': f'crl{i}.der'}}  # make the new entry
    yaml_loaded["urls"].update(d)  # update with a new value. If same key, then replace
    
    with open('test.yaml', 'w') as yaml_file:
        yaml.dump(yaml_loaded, yaml_file, default_flow_style=False)  # write new entry.
    

    This code loads this

    urls:
      part-0:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-1:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-2:
        updated: 16662709612
        url: http://any-do0maini2.com/
        url-name: crl2.der
    

    and appends so it becomes:

    urls:
      part-0:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-1:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-2:
        updated: 16662709612
        url: http://any-do0maini2.com/
        url-name: crl2.der
      part-3:
        updated: 16662709613
        url: http://any-do0maini3.com/
        url-name: crl3.der
    

    Furthermore...

    It handles also loading this one:

    urls:
      part-0:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-1:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-2:
        updated: 16662709612
        url: http://any-do0maini2.com/
        url-name: crl2.der
      part-3341:
        updated: 16662709613
        url: http://any-do0maini3.com/
        url-name: crl3.der
    

    and appends so it becomes:

    urls:
      part-0:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-1:
        updated: 16662709610
        url: http://any-do0main1.com/
        url-name: crl0.der
      part-2:
        updated: 16662709612
        url: http://any-do0maini2.com/
        url-name: crl2.der
      part-3341:
        updated: 16662709613
        url: http://any-do0maini3.com/
        url-name: crl3.der
      part-3342:
        updated: 16662712952
        url: http://any-do0maini3342.com/
        url-name: crl3342.der