generated from ludeeus/integration_blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto-sort and format library.json after a push (#115)
* Sort library.json devices whenever it is edited. * Apply automatic changes * Improve auto-renaming of created issues * Update validate-json-action * reactivate device submission issue template * re-enable auto-submission instructions in README * Revert "Update validate-json-action" This reverts commit 4b2e7aa. * validate JSON before creating auto PR * close issue after pr creation * Andrew's updates to library_sort * rename json_sorter * fix file name in on: * Add library doc scripts --------- Co-authored-by: bmos <[email protected]> Co-authored-by: Andrew Jackson <[email protected]>
- Loading branch information
1 parent
6595158
commit 8f7b1d9
Showing
7 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Battery library document generator.""" | ||
|
||
from __future__ import annotations | ||
|
||
import json | ||
|
||
from pytablewriter import MarkdownTableWriter | ||
|
||
|
||
def generate_device_list(): | ||
"""Generate static file containing the device library.""" | ||
|
||
# Load the existing JSON library file | ||
with open("custom_components/battery_notes/data/library.json", | ||
encoding="UTF-8") as f: | ||
devices_json = json.loads(f.read()) | ||
devices = devices_json.get("devices") | ||
|
||
toc_links: list[str] = [] | ||
tables_output: str = "" | ||
rows = [] | ||
|
||
num_devices = len(devices) | ||
|
||
writer = MarkdownTableWriter() | ||
headers = [ | ||
"Manufacturer", | ||
"Model", | ||
"Battery Type", | ||
] | ||
|
||
writer.header_list = headers | ||
|
||
for device in devices: | ||
if device.get("battery_quantity", 1) > 1: | ||
battery_type_qty = f"{device['battery_quantity']}x {device['battery_type']}" | ||
else: | ||
battery_type_qty = device["battery_type"] | ||
row = [ | ||
device['manufacturer'], | ||
device['model'], | ||
battery_type_qty, | ||
] | ||
rows.append(row) | ||
|
||
writer.value_matrix = rows | ||
tables_output += f"## {num_devices} Devices in library\n\n" | ||
tables_output += "This file is auto generated, do not modify\n\n" | ||
tables_output += writer.dumps() | ||
|
||
with open("library.md", "w", encoding="UTF-8") as md_file: | ||
md_file.write("".join(toc_links) + tables_output) | ||
md_file.close() | ||
|
||
generate_device_list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytablewriter==0.61.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: JSON Library Sorter and Doc Gen | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
paths: | ||
- 'custom_components/battery_notes/data/library.json' | ||
- '.github/workflows/json_librarian.yml' | ||
- '.github/workflows/scripts/library_doc/**' | ||
|
||
jobs: | ||
librarian: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up python 3.12 | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.12" | ||
|
||
- name: Clean up JSON library file | ||
id: update-json | ||
uses: jannekem/run-python-script-action@v1 | ||
with: | ||
script: | | ||
import json | ||
# Load the existing JSON library file | ||
with open("custom_components/battery_notes/data/library.json", "r", encoding="UTF-8") as f: | ||
devices_json = json.loads(f.read()) | ||
devices = devices_json.get("devices") | ||
# Sort the devices by manufacturer and model | ||
devices.sort(key=lambda k: (k["manufacturer"].lower(), k["model"].lower())) | ||
with open("custom_components/battery_notes/data/library.json", "w", encoding="UTF-8") as f: | ||
f.write(json.dumps(devices_json, indent=4)) | ||
- name: Install library doc generator dependencies | ||
run: | | ||
python -m pip install -r ${{ github.workspace }}/.github/scripts/library_doc/requirements.txt | ||
- name: Generate library doc | ||
run: | | ||
python3 ${{ github.workspace }}/.github/scripts/library_doc/generate_file.py | ||
- name: Commit any changes | ||
uses: stefanzweifel/git-auto-commit-action@v5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1782,4 +1782,4 @@ | |
"battery_quantity": 3 | ||
} | ||
] | ||
} | ||
} |