Add weblate users
Added weblate API codes to get ready for Zanata to Weblate migration Implements: blueprint weblate-tools Change-Id: I8d1a2393cc50541a29bd9581c4ac2574b9dc9813
This commit is contained in:
parent
d5d64997ab
commit
02b0574557
159
tools/weblate/weblate_users.py
Normal file
159
tools/weblate/weblate_users.py
Normal file
@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
import yaml
|
||||
|
||||
from weblate_utils import IniConfig
|
||||
from weblate_utils import WeblateRestService
|
||||
|
||||
WEBLATE_URI = "https://openstack.weblate.cloud/api/%s"
|
||||
LOG = logging.getLogger("weblate_users")
|
||||
YAML_COMMENT = """\
|
||||
# This file is generated by tools/weblate/weblate_users.py.
|
||||
"""
|
||||
|
||||
|
||||
class WeblateUtility(object):
|
||||
"""Utilities to collect Weblate language contributors"""
|
||||
|
||||
def __init__(self, wconfig):
|
||||
accept = 'application/json'
|
||||
content_type = 'application/json'
|
||||
self.rest_service = WeblateRestService(wconfig, accept=accept,
|
||||
content_type=content_type)
|
||||
|
||||
def read_uri(self, uri):
|
||||
try:
|
||||
req = self.rest_service.query(uri)
|
||||
return req.text
|
||||
except Exception as e:
|
||||
LOG.error(
|
||||
'Error "%(error)s" while reading uri %(uri)s',
|
||||
{"error": e, "uri": uri}
|
||||
)
|
||||
raise
|
||||
|
||||
def read_json_from_uri(self, uri):
|
||||
data = self.read_uri(uri)
|
||||
try:
|
||||
return json.loads(data)
|
||||
except Exception as e:
|
||||
LOG.error(
|
||||
'Error "%(error)s" parsing json from uri %(uri)s',
|
||||
{"error": e, "uri": uri},
|
||||
)
|
||||
raise
|
||||
|
||||
def get_users(self):
|
||||
uri = WEBLATE_URI % "users/"
|
||||
LOG.debug(
|
||||
"Reading the list of all users from %s" % uri
|
||||
)
|
||||
users_data = self.read_json_from_uri(uri)
|
||||
return users_data
|
||||
|
||||
def get_languages(self):
|
||||
uri = WEBLATE_URI % "groups/"
|
||||
languages = {}
|
||||
while True:
|
||||
LOG.debug("Reading the list of languages from %s" % uri)
|
||||
groups_data = self.read_json_from_uri(uri)
|
||||
for group in groups_data["results"]:
|
||||
for language in group["languages"]:
|
||||
language_data = self.read_json_from_uri(language)
|
||||
languages[language_data["name"]] = {
|
||||
"language": language_data["code"],
|
||||
"translators": [],
|
||||
}
|
||||
if groups_data["next"] is None:
|
||||
break
|
||||
else:
|
||||
uri = groups_data["next"]
|
||||
|
||||
return languages
|
||||
|
||||
def get_user_language(self, user):
|
||||
groups = user["groups"]
|
||||
languages = []
|
||||
for group in groups:
|
||||
group_data = self.read_json_from_uri(group)
|
||||
for language in group_data["languages"]:
|
||||
language_data = self.read_json_from_uri(language)
|
||||
languages.append(language_data["name"])
|
||||
|
||||
return languages
|
||||
|
||||
|
||||
def save_to_yaml(data, output_file):
|
||||
with open(output_file, "w") as out:
|
||||
out.write(YAML_COMMENT)
|
||||
for k, v in data.items():
|
||||
yaml.safe_dump(
|
||||
{k: v},
|
||||
out,
|
||||
allow_unicode=True,
|
||||
indent=4,
|
||||
encoding="utf-8",
|
||||
default_flow_style=False,
|
||||
)
|
||||
|
||||
|
||||
def collect_weblate_groups_and_users(wc):
|
||||
weblate = WeblateUtility(wc)
|
||||
|
||||
LOG.info("Retrieving language list")
|
||||
languages = weblate.get_languages()
|
||||
users = weblate.get_users()["results"]
|
||||
|
||||
for user in users:
|
||||
LOG.info("Getting language list from user %s" % user["username"])
|
||||
user_language = weblate.get_user_language(user)
|
||||
|
||||
for language in languages.keys():
|
||||
if language in user_language:
|
||||
languages[language]["translators"].append(user["username"])
|
||||
|
||||
result = OrderedDict((k, languages[k]) for k in sorted(languages))
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Loads weblate.ini configuration file
|
||||
try:
|
||||
wc = IniConfig(os.path.expanduser('~/.config/weblate.ini'))
|
||||
except ValueError as e:
|
||||
sys.exit(e)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output-file",
|
||||
default="translation_team.yaml",
|
||||
help="Specify the output file. " "Default: translation_team.yaml",
|
||||
)
|
||||
options = parser.parse_args()
|
||||
|
||||
output_file = options.output_file
|
||||
data = collect_weblate_groups_and_users(wc)
|
||||
save_to_yaml(data, output_file)
|
||||
print("output is saved to filename: %s" % output_file)
|
Loading…
Reference in New Issue
Block a user