Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18819

How to reorder yaml keys and maintain comments according to a predefined template?

$
0
0

I would like to set a template for the key order and line spacing of a yaml file and apply this to the repository of 100s of yaml files I have. In general, I want like to do the following:

  1. Load the existing yaml file
  2. Reorder the keys and values according to the template
  3. Delete any comments that are just a new line
  4. Apply the line spacing from the template
  5. Save the yaml file

I am using python 3.10 and ruamel.yaml version. At a very basic level, I understand that the YAML object in ruamel.yaml is based upon an ordered dictionary and the accepted answer here seems like a simple way to ensure a specific order of a dictionary's keys, but I don't know how to apply that to the YAML object.

To maintain comments, I presume that the .ca attribute can be copied although I don't know how to then apply the line spacing rules from the template.

Further complicating the matter is that some keys themselves may have multiple values (I think these would be a CommentedSequence in ruamel.yaml ?) each of which should follow the templated order - and the last one will need a blank line after it.

Here is a basic version of the template that should provide an overview of the structure I'm talking about:

template='''name:region:origin:description:go_live_date:status:governance:  business_owner:    am:    eu:    ap:  technical_owner:    am:    eu:    ap:architecture:  protocol:  platform:environments:  - name:    description:    tier:    locations:'''

In the following example, the key order is wrong, there are missing and double line spacing plus some comments:

'''name: MyAppdescription: My wonderful applicationorigin: internalgovernance:  technical_owner:    am:      - Nico Ferrell    ap:      - Benedict Berger      - Elsie Parsons    eu:      - Frances Case  business_owner:    eu:      - Audrey Dalton    am:      - John Carpenter # to be updatedarchitecture:  protocol: [TCP]  platforms: [python_3_10, java_16]status: in productiongo_live_date: 2024-01-01environments:  - name: EU Prod    description: production environment for EMEA    tier: production    locations: [ABC, XYZ]  - name: EU UAT    description: UAT environment for EMEA    locations: [LMN]    tier: uat# further environmental details to be added'''

After applying the template and steps outlined to this example, the resultant file should look like this:

'''name: MyApporigin: internaldescription: My wonderful applicationstatus: in productiongo_live_date: 2024-01-01governance:  technical_owner:    am:      - Nico Ferrell    eu:      - Frances Case    ap:      - Benedict Berger      - Elsie Parsons  business_owner:    am:      - John Carpenter # to be updated    eu:      - Audrey Daltonarchitecture:  protocol: [TCP]  platforms: [python_3_10, java_16]environments:  - name: EU Prod    description: production environment for EMEA    tier: production    locations: [ABC, XYZ]  - name: EU UAT    description: UAT environment for EMEA    tier: uat    locations: [LMN]# further environmental details to be added'''

I don't know how to tackle this and would appreciate some help


Viewing all articles
Browse latest Browse all 18819

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>