Add script to compare alexa locales with upstream (#114247)

* Add script to compare alexa locales with upstream

* Use a function in script

* Add test base

* Complete output assertion

* Add type annotation

* Add note to docstring

* Update script call example

Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>

---------

Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
This commit is contained in:
Martin Hjelmare
2024-04-18 13:41:34 +02:00
committed by GitHub
parent aee620be9f
commit 47f0d5ed1f
6 changed files with 813 additions and 0 deletions

67
script/alexa_locales.py Normal file
View File

@ -0,0 +1,67 @@
"""Check if upstream Alexa locales are subset of the core Alexa supported locales."""
from pprint import pprint
import re
from bs4 import BeautifulSoup
import requests
from homeassistant.components.alexa import capabilities
SITE = (
"https://developer.amazon.com/en-GB/docs/alexa/device-apis/list-of-interfaces.html"
)
def run_script() -> None:
"""Run the script."""
response = requests.get(SITE, timeout=10)
soup = BeautifulSoup(response.text, "html.parser")
table = soup.find("table")
table_body = table.find_all("tbody")[-1]
rows = table_body.find_all("tr")
data = [[ele.text.strip() for ele in row.find_all("td") if ele] for row in rows]
upstream_locales_raw = {row[0]: row[3] for row in data}
language_pattern = re.compile(r"^[a-z]{2}-[A-Z]{2}$")
upstream_locales = {
upstream_interface: {
name
for word in upstream_locale.split(" ")
if (name := word.strip(",")) and language_pattern.match(name) is not None
}
for upstream_interface, upstream_locale in upstream_locales_raw.items()
if upstream_interface.count(".") == 1 # Skip sub-interfaces
}
interfaces_missing = {}
interfaces_nok = {}
interfaces_ok = {}
for upstream_interface, upstream_locale in upstream_locales.items():
core_interface_name = upstream_interface.replace(".", "")
core_interface = getattr(capabilities, core_interface_name, None)
if core_interface is None:
interfaces_missing[upstream_interface] = upstream_locale
continue
core_locale = core_interface.supported_locales
if not upstream_locale.issubset(core_locale):
interfaces_nok[core_interface_name] = core_locale
else:
interfaces_ok[core_interface_name] = core_locale
print("Missing interfaces:")
pprint(list(interfaces_missing))
print("\n")
print("Interfaces where upstream locales are not subsets of the core locales:")
pprint(list(interfaces_nok))
print("\n")
print("Interfaces checked ok:")
pprint(list(interfaces_ok))
if __name__ == "__main__":
run_script()