1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00

Add Translations 2.0 migrate script (#34261)

This commit is contained in:
Paulus Schoutsen 2020-04-15 13:53:52 -07:00 committed by GitHub
parent 9f1bffe3be
commit 72cc656b7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 6 deletions

View File

@ -1,9 +1,8 @@
"""Find translation keys that are in Lokalise but no longer defined in source."""
import json
from .const import INTEGRATIONS_DIR, PROJECT_ID
from .lokalise import Lokalise
from .util import get_lokalise_token
from .const import INTEGRATIONS_DIR
from .lokalise import get_api
def find_extra(base, translations, path_prefix, missing_keys):
@ -50,7 +49,7 @@ def run():
print("No missing translations!")
return
lokalise = Lokalise(PROJECT_ID, get_lokalise_token())
lokalise = get_api()
to_delete = []

View File

@ -1,6 +1,14 @@
"""API for Lokalise."""
import requests
from .const import PROJECT_ID
from .util import get_lokalise_token
def get_api() -> "Lokalise":
"""Get Lokalise API."""
return Lokalise(PROJECT_ID, get_lokalise_token())
class Lokalise:
"""Lokalise API."""
@ -28,7 +36,7 @@ class Lokalise:
return req.json()
def keys_list(self, params={}):
"""Fetch key ID from a name.
"""List keys.
https://app.lokalise.com/api2docs/curl/#transition-list-all-keys-get
"""
@ -40,3 +48,10 @@ class Lokalise:
https://app.lokalise.com/api2docs/curl/#transition-delete-multiple-keys-delete
"""
return self.request("DELETE", "keys", {"keys": key_ids})
def keys_bulk_update(self, updates):
"""Update multiple keys.
https://app.lokalise.com/api2docs/curl/#transition-bulk-update-put
"""
return self.request("PUT", "keys", {"keys": updates})

View File

@ -0,0 +1,50 @@
"""Migrate things."""
import json
from pprint import pprint
from .const import INTEGRATIONS_DIR
from .lokalise import get_api
MIGRATED = {}
def run():
"""Migrate translations."""
to_migrate = {}
for integration in INTEGRATIONS_DIR.iterdir():
strings_file = integration / "strings.json"
if not strings_file.is_file():
continue
if integration.name in MIGRATED:
continue
strings = json.loads(strings_file.read_text())
if "title" in strings:
from_key = f"component::{integration.name}::config::title"
to_key = f"component::{integration.name}::title"
to_migrate[from_key] = to_key
updates = []
lokalise = get_api()
print("Gathering IDs")
for from_key, to_key in to_migrate.items():
key_data = lokalise.keys_list({"filter_keys": from_key})
if len(key_data) != 1:
print(
f"Lookin up {from_key} key in Lokalise returns {len(key_data)} results, expected 1"
)
continue
updates.append({"key_id": key_data[0]["key_id"], "key_name": to_key})
pprint(updates)
print()
print("Updating keys")
pprint(lokalise.keys_bulk_update(updates).json())

View File

@ -11,7 +11,9 @@ def get_base_arg_parser():
"""Get a base argument parser."""
parser = argparse.ArgumentParser(description="Home Assistant Translations")
parser.add_argument(
"action", type=str, choices=["download", "clean", "upload", "develop"]
"action",
type=str,
choices=["download", "clean", "upload", "develop", "migrate"],
)
parser.add_argument("--debug", action="store_true", help="Enable log output")
return parser