From b4c25f04562ec2808c2629c7ca9fc95bf5c53acd Mon Sep 17 00:00:00 2001 From: Marcel Stimberg Date: Wed, 31 Jan 2024 18:49:11 +0100 Subject: [PATCH] Automatically create a json file from the yaml files --- .github/workflows/update_json.yml | 25 +++++++++++++++++++++++++ yaml2json.py | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 .github/workflows/update_json.yml create mode 100644 yaml2json.py diff --git a/.github/workflows/update_json.yml b/.github/workflows/update_json.yml new file mode 100644 index 0000000..e7c6067 --- /dev/null +++ b/.github/workflows/update_json.yml @@ -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 diff --git a/yaml2json.py b/yaml2json.py new file mode 100644 index 0000000..456f820 --- /dev/null +++ b/yaml2json.py @@ -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)