mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Migrate translations clean script (#33930)
This commit is contained in:
parent
e9c412bac6
commit
e9e1ec5312
@ -3,13 +3,13 @@ import argparse
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import download, error, upload
|
from . import clean, download, error, upload
|
||||||
|
|
||||||
|
|
||||||
def get_arguments() -> argparse.Namespace:
|
def get_arguments() -> argparse.Namespace:
|
||||||
"""Get parsed passed in arguments."""
|
"""Get parsed passed in arguments."""
|
||||||
parser = argparse.ArgumentParser(description="Home Assistant Scaffolder")
|
parser = argparse.ArgumentParser(description="Home Assistant Scaffolder")
|
||||||
parser.add_argument("action", type=str, choices=["download", "upload"])
|
parser.add_argument("action", type=str, choices=["download", "clean", "upload"])
|
||||||
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
||||||
|
|
||||||
arguments = parser.parse_args()
|
arguments = parser.parse_args()
|
||||||
@ -29,6 +29,8 @@ def main():
|
|||||||
download.run(args)
|
download.run(args)
|
||||||
elif args.action == "upload":
|
elif args.action == "upload":
|
||||||
upload.run(args)
|
upload.run(args)
|
||||||
|
elif args.action == "clean":
|
||||||
|
clean.run()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -1,52 +1,9 @@
|
|||||||
"""Find translation keys that are in Lokalise but no longer defined in source."""
|
"""Find translation keys that are in Lokalise but no longer defined in source."""
|
||||||
import json
|
import json
|
||||||
import pathlib
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import requests
|
from .const import INTEGRATIONS_DIR, PROJECT_ID
|
||||||
|
from .lokalise import Lokalise
|
||||||
INTEGRATION_DIR = pathlib.Path("homeassistant/components")
|
from .util import get_lokalise_token
|
||||||
PROJECT_ID = "130246255a974bd3b5e8a1.51616605"
|
|
||||||
|
|
||||||
|
|
||||||
class Lokalise:
|
|
||||||
"""Lokalise API."""
|
|
||||||
|
|
||||||
def __init__(self, project_id, token):
|
|
||||||
"""Initialize Lokalise API."""
|
|
||||||
self.project_id = project_id
|
|
||||||
self.token = token
|
|
||||||
|
|
||||||
def request(self, method, path, data):
|
|
||||||
"""Make a request to the Lokalise API."""
|
|
||||||
method = method.upper()
|
|
||||||
kwargs = {"headers": {"x-api-token": self.token}}
|
|
||||||
if method == "GET":
|
|
||||||
kwargs["params"] = data
|
|
||||||
else:
|
|
||||||
kwargs["json"] = data
|
|
||||||
|
|
||||||
req = requests.request(
|
|
||||||
method,
|
|
||||||
f"https://api.lokalise.com/api2/projects/{self.project_id}/{path}",
|
|
||||||
**kwargs,
|
|
||||||
)
|
|
||||||
req.raise_for_status()
|
|
||||||
return req.json()
|
|
||||||
|
|
||||||
def keys_list(self, params={}):
|
|
||||||
"""Fetch key ID from a name.
|
|
||||||
|
|
||||||
https://app.lokalise.com/api2docs/curl/#transition-list-all-keys-get
|
|
||||||
"""
|
|
||||||
return self.request("GET", "keys", params)["keys"]
|
|
||||||
|
|
||||||
def keys_delete_multiple(self, key_ids):
|
|
||||||
"""Delete multiple keys.
|
|
||||||
|
|
||||||
https://app.lokalise.com/api2docs/curl/#transition-delete-multiple-keys-delete
|
|
||||||
"""
|
|
||||||
return self.request("DELETE", "keys", {"keys": key_ids})
|
|
||||||
|
|
||||||
|
|
||||||
def find_extra(base, translations, path_prefix, missing_keys):
|
def find_extra(base, translations, path_prefix, missing_keys):
|
||||||
@ -67,7 +24,7 @@ def find():
|
|||||||
"""Find all missing keys."""
|
"""Find all missing keys."""
|
||||||
missing_keys = []
|
missing_keys = []
|
||||||
|
|
||||||
for int_dir in INTEGRATION_DIR.iterdir():
|
for int_dir in INTEGRATIONS_DIR.iterdir():
|
||||||
strings = int_dir / "strings.json"
|
strings = int_dir / "strings.json"
|
||||||
|
|
||||||
if not strings.is_file():
|
if not strings.is_file():
|
||||||
@ -91,9 +48,9 @@ def run():
|
|||||||
|
|
||||||
if not missing_keys:
|
if not missing_keys:
|
||||||
print("No missing translations!")
|
print("No missing translations!")
|
||||||
return 0
|
return
|
||||||
|
|
||||||
lokalise = Lokalise(PROJECT_ID, pathlib.Path(".lokalise_token").read_text().strip())
|
lokalise = Lokalise(PROJECT_ID, get_lokalise_token())
|
||||||
|
|
||||||
to_delete = []
|
to_delete = []
|
||||||
|
|
||||||
@ -107,10 +64,8 @@ def run():
|
|||||||
continue
|
continue
|
||||||
to_delete.append(key_data[0]["key_id"])
|
to_delete.append(key_data[0]["key_id"])
|
||||||
|
|
||||||
|
while input("Type YES to delete these keys: ") != "YES":
|
||||||
|
pass
|
||||||
|
|
||||||
print("Deleting keys:", ", ".join(map(str, to_delete)))
|
print("Deleting keys:", ", ".join(map(str, to_delete)))
|
||||||
print(lokalise.keys_delete_multiple(to_delete))
|
print(lokalise.keys_delete_multiple(to_delete))
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(run())
|
|
42
script/translations/lokalise.py
Normal file
42
script/translations/lokalise.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"""API for Lokalise."""
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
class Lokalise:
|
||||||
|
"""Lokalise API."""
|
||||||
|
|
||||||
|
def __init__(self, project_id, token):
|
||||||
|
"""Initialize Lokalise API."""
|
||||||
|
self.project_id = project_id
|
||||||
|
self.token = token
|
||||||
|
|
||||||
|
def request(self, method, path, data):
|
||||||
|
"""Make a request to the Lokalise API."""
|
||||||
|
method = method.upper()
|
||||||
|
kwargs = {"headers": {"x-api-token": self.token}}
|
||||||
|
if method == "GET":
|
||||||
|
kwargs["params"] = data
|
||||||
|
else:
|
||||||
|
kwargs["json"] = data
|
||||||
|
|
||||||
|
req = requests.request(
|
||||||
|
method,
|
||||||
|
f"https://api.lokalise.com/api2/projects/{self.project_id}/{path}",
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
req.raise_for_status()
|
||||||
|
return req.json()
|
||||||
|
|
||||||
|
def keys_list(self, params={}):
|
||||||
|
"""Fetch key ID from a name.
|
||||||
|
|
||||||
|
https://app.lokalise.com/api2docs/curl/#transition-list-all-keys-get
|
||||||
|
"""
|
||||||
|
return self.request("GET", "keys", params)["keys"]
|
||||||
|
|
||||||
|
def keys_delete_multiple(self, key_ids):
|
||||||
|
"""Delete multiple keys.
|
||||||
|
|
||||||
|
https://app.lokalise.com/api2docs/curl/#transition-delete-multiple-keys-delete
|
||||||
|
"""
|
||||||
|
return self.request("DELETE", "keys", {"keys": key_ids})
|
Loading…
x
Reference in New Issue
Block a user