Skip to content

Commit

Permalink
Automatically create a json file from the yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
mstimberg committed Jan 31, 2024
1 parent 9b56b7a commit b4c25f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/update_json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Update JSON

on: [push]
jobs:
test:
name: Update JSON
runs-on: ubuntu-latest
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: install yaml
run: python -m pip install pyyaml
- name: update json file
run: python yaml2json.py
- name: commit if necessary
uses: stefanzweifel/git-auto-commit-action@v5
with:
file_pattern: simtools/simtools.json
commit_message: Update JSON from yaml files
21 changes: 21 additions & 0 deletions yaml2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import glob
import os

import yaml
import json

if __name__ == "__main__":
data = {}
for fname in sorted(glob.glob("simtools/*.yaml")):
if os.path.basename(fname) == "simtools.yaml":
continue
with open(fname) as f:
tool_data = {
list(item.keys())[0]: list(item.values())[0]
for item in yaml.load(f, Loader=yaml.FullLoader)
}
name = tool_data["name"]
del tool_data["name"]
data[name] = tool_data
with open("simtools/simtools.json", "w") as f:
json.dump(data, f, indent=2, sort_keys=True)

0 comments on commit b4c25f0

Please sign in to comment.