1
0
Fork 0
mirror of https://github.com/Findus23/matomo-utils.git synced 2024-09-19 15:13:44 +02:00
matomo-utils/localisation/mass-edit.py
2021-09-22 13:44:21 +02:00

132 lines
4.7 KiB
Python

from typing import Dict, List, Tuple
import requests
from config import *
from priorities import priorities, Priority
s = requests.Session()
s.headers.update({"Authorization": "Token " + token})
PROJECT = "matomo" # "matomo-premium-plugins"
def diff_dicts(dict1, dict2):
return {key: dict1[key] for key in set(dict1) - set(dict2)}
def lock_component(component, unlock=False):
lock = not unlock
lock_url = component["lock_url"]
r = s.post(lock_url, data={"lock": lock})
print(r.json())
r.raise_for_status()
def update_setting(component, settings: Dict):
component_url = component["url"]
r = s.patch(component_url, json=settings)
if r.status_code > 200:
print(r.json())
r.raise_for_status()
def create_addon(component, name, configuration: Dict):
component_url = component["url"]
r = s.post(component_url + "addons/", json={"name": name, "configuration": configuration})
if r.status_code > 200:
print(r.json())
r.raise_for_status()
def get_addons(addons: List[str]) -> Tuple[Dict[str, int], Dict[str, Dict]]:
ids = {}
configs = {}
for addon in addons:
r = s.get(addon)
data = r.json()
ids[data["name"]] = data["id"]
configs[data["name"]] = data["configuration"]
return ids, configs
components = {}
r = s.get(url + f"projects/{PROJECT}/components/")
data = r.json()
count = data["count"]
for comp in data["results"]:
components[comp["slug"]] = comp
while data["next"]:
r = s.get(data["next"])
data = r.json()
for comp in data["results"]:
components[comp["slug"]] = comp
assert len(components) == count
print(f"{count} components")
phpcomponents = {slug: comp for slug, comp in components.items() if not comp["is_glossary"]}
community_components = {slug: comp for slug, comp in components.items() if "Community" in comp["name"]}
official_components = diff_dicts(phpcomponents, community_components)
core_repo_components = {slug: comp for slug, comp in components.items() if
"https://github.com/matomo-org/matomo/" == comp["repo"]}
non_core_repo_components = diff_dicts(phpcomponents, core_repo_components)
non_core_repo_components = diff_dicts(non_core_repo_components, community_components)
for slug, comp in community_components.items():
print(slug, comp["name"])
addon_ids, addon_configs = get_addons(comp["addons"])
license = comp["license"]
if license != "GPL-3.0-or-later":
print(license)
if slug in priorities:
priority = priorities[slug]
else:
priority = Priority.medium
update_setting(comp, {
"check_flags": "php-format,safe-html,ignore-optional-plural",
# "license": "proprietary",
"manage_units": False, # Manage strings
"edit_template": False,
"enforced_checks": [
"php_format"
],
"priority": priority.value,
"language_code_style": "bcp",
"new_lang": "contact",
"push_on_commit": True,
"commit_message": "Translated using Weblate ({{ language_name }})\n\nCurrently translated at {{ stats.translated_percent }}% ({{ stats.translated }} of {{ stats.all }} strings)\n\nTranslation: {{ project_name }}/{{ component_name }}\nTranslate-URL: {{ url }}\n\n[ci skip]",
"add_message": "Added translation using Weblate ({{ language_name }})\n\n[ci skip]",
"delete_message": "Deleted translation using Weblate ({{ language_name }})\n\n[ci skip]",
"merge_message": "Merge branch '{{ component_remote_branch }}' into Weblate.\n\n[ci skip]",
"addon_message": "Update translation files\n\nUpdated by \"{{ addon_name }}\" hook in Weblate.\n\nTranslation: {{ project_name }}/{{ component_name }}\nTranslate-URL: {{ url }}\n\n[ci skip]",
})
is_wordpress = "Wordpress" in comp["name"]
if "weblate.cleanup.blank" not in addon_ids.keys() and not is_wordpress:
create_addon(comp, name="weblate.cleanup.blank", configuration={})
if "weblate.cleanup.generic" not in addon_ids.keys() and not is_wordpress:
create_addon(comp, name="weblate.cleanup.generic", configuration={})
if "weblate.json.customize" not in addon_ids.keys() and not is_wordpress:
create_addon(comp, name="weblate.json.customize", configuration={
"sort_keys": True,
"indent": 4,
"style": "spaces"
})
if "weblate.git.squash" not in addon_ids.keys() and PROJECT == "matomo":
print("add addon")
create_addon(comp, name="weblate.git.squash", configuration={
"squash": "language",
"append_trailers": True,
"commit_message": ""
})
# input("done\n")
# lock_component(comp, unlock=True)
# print("locked")
# exit()