Add scripts to auto update en.json, strings.json and services.yaml (#520)

* Add scripts to auto update strings.json and services.yaml

* Run services

* simplify

* Run strings

* rerun

* revert

* allow unicode

* Add CODEOWNERS

* Update CODEOWNERS

* set CONF_USE_DEFAULTS docs

* add field_name

* Auto run scripts

* Update desc

* Update README.md, strings.json, and services.yaml

* double quotes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update README.md, strings.json, and services.yaml

* Add newline

* sync changes between en.json and strings.json

* Update README.md, strings.json, and services.yaml

* double quotes

* fix

* Update README.md, strings.json, and services.yaml

* Add comments

* Remove comments

* shorter

* Update README.md, strings.json, and services.yaml

* Rephrase

* Update README.md, strings.json, and services.yaml

* remove key from desc

---------

Co-authored-by: Benjamin Auquite <halomastar@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Bas Nijholt 2023-04-03 14:59:14 -07:00 committed by GitHub
commit b730c7cc90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 231 additions and 166 deletions

25
.github/update-services.py vendored Normal file
View file

@ -0,0 +1,25 @@
from pathlib import Path
import sys
import yaml
sys.path.append(str(Path(__file__).parent.parent))
from custom_components.adaptive_lighting import const # noqa: E402
services_filename = "custom_components/adaptive_lighting/services.yaml"
with open(services_filename) as f:
services = yaml.safe_load(f)
for service_name, dct in services.items():
_docs = {"set_manual_control": const.DOCS_MANUAL_CONTROL, "apply": const.DOCS_APPLY}
alternative_docs = _docs.get(service_name, const.DOCS)
for field_name, field in dct["fields"].items():
description = alternative_docs.get(field_name, const.DOCS[field_name])
field["description"] = description
comment = "# This file is auto-generated by .github/update-services.py."
with open(services_filename, "w") as f:
f.write(comment + "\n")
yaml.dump(services, f, sort_keys=False, width=1000, allow_unicode=True)

31
.github/update-strings.py vendored Normal file
View file

@ -0,0 +1,31 @@
import json
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).parent.parent))
from custom_components.adaptive_lighting import const # noqa: E402
strings_fname = "custom_components/adaptive_lighting/strings.json"
en_fname = "custom_components/adaptive_lighting/translations/en.json"
with open(strings_fname) as f:
strings = json.load(f)
data = {k: f"{k}: {const.DOCS[k]}" for k, _, _ in const.VALIDATION_TUPLES}
strings["options"]["step"]["init"]["data"] = data
with open(strings_fname, "w") as f:
json.dump(strings, f, indent=2, ensure_ascii=False)
f.write("\n")
# Sync changes from strings.json to en.json
with open(en_fname) as f:
en = json.load(f)
en["config"]["step"]["user"] = strings["config"]["step"]["user"]
en["options"]["step"]["init"]["data"] = data
with open(en_fname, "w") as f:
json.dump(en, f, indent=2, ensure_ascii=False)
f.write("\n")

View file

@ -34,17 +34,23 @@ jobs:
- name: Run markdown-code-runner
run: markdown-code-runner --debug README.md
- name: Commit updated README.md
- name: Run update strings.json
run: python .github/update-strings.py
- name: Run update services.yaml
run: python .github/update-services.py
- name: Commit updated README.md, strings.json, and services.yaml
id: commit
run: |
git add README.md
git add -u .
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
if git diff --quiet && git diff --staged --quiet; then
echo "No changes in README.md, skipping commit."
echo "No changes in README.md, strings.json, and services.yaml, skipping commit."
echo "commit_status=skipped" >> $GITHUB_ENV
else
git commit -m "Update README.md"
git commit -m "Update README.md, strings.json, and services.yaml"
echo "commit_status=committed" >> $GITHUB_ENV
fi