cd44236152
Change-Id: I53f840e6c5e5d6656255c76f14fb5b5bf08e57f3
137 lines
4.9 KiB
Python
Executable File
137 lines
4.9 KiB
Python
Executable File
#!/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
|
|
from collections import OrderedDict
|
|
import json
|
|
import logging
|
|
import random
|
|
|
|
import requests
|
|
import yaml
|
|
|
|
ZANATA_URI = 'https://translate.openstack.org/rest/%s'
|
|
LOG = logging.getLogger('zanata_users')
|
|
YAML_COMMENT = """\
|
|
# This file is generated by tools/zanata/zanata_users.py.
|
|
# For more information, see
|
|
# https://docs.openstack.org/i18n/latest/tools.html#sync-the-translator-list-with-zanata
|
|
"""
|
|
|
|
|
|
class ZanataUtility(object):
|
|
"""Utilities to collect Zanata language contributors"""
|
|
user_agents = [
|
|
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64) Gecko/20100101 Firefox/32.0',
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_6) AppleWebKit/537.78.2',
|
|
'Mozilla/5.0 (Windows NT 6.3; WOW64) Gecko/20100101 Firefox/32.0',
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X) Chrome/37.0.2062.120',
|
|
'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
|
|
]
|
|
|
|
def read_uri(self, uri, headers):
|
|
try:
|
|
headers['User-Agent'] = random.choice(ZanataUtility.user_agents)
|
|
req = requests.get(uri, headers=headers)
|
|
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, {'Accept': 'application/json'})
|
|
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_locale_members(self, language_id):
|
|
uri = ZANATA_URI % ('locales/locale/%s/members' % language_id)
|
|
LOG.debug("Reading the list of locale members in %s from %s"
|
|
% (language_id, uri))
|
|
locale_members_data = self.read_json_from_uri(uri)
|
|
return locale_members_data
|
|
|
|
def get_locales(self):
|
|
# The default value of sizePerPage is 10, which needs to be increased
|
|
# http://zanata.org/zanata-platform/rest-api-docs/resource_LocalesResource.html
|
|
uri = ZANATA_URI % 'locales?sizePerPage=255'
|
|
LOG.debug("Reading the list of locales from %s" % uri)
|
|
locales_data = self.read_json_from_uri(uri)
|
|
languages = {}
|
|
for locale in locales_data['results']:
|
|
|
|
# No need to store language information with no members
|
|
if locale['memberCount'] == 0:
|
|
continue
|
|
|
|
languages[locale['id']] = {
|
|
'language': locale['localeDetails']['displayName'],
|
|
'coordinators': [],
|
|
'reviewers': [],
|
|
'translators': [],
|
|
}
|
|
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_zanata_language_and_members():
|
|
zanata = ZanataUtility()
|
|
|
|
LOG.info("Retrieving language list")
|
|
languages = zanata.get_locales()
|
|
|
|
for language in languages.keys():
|
|
LOG.info("Getting member list from language %s" % language)
|
|
|
|
for member in zanata.get_locale_members(language):
|
|
if member['isTranslator']:
|
|
languages[language]['translators'].append(member['username'])
|
|
if member['isReviewer']:
|
|
languages[language]['reviewers'].append(member['username'])
|
|
if member['isCoordinator']:
|
|
languages[language]['coordinators'].append(member['username'])
|
|
|
|
# Sort each member list alphabetically
|
|
for role in ['translators', 'reviewers', 'coordinators']:
|
|
languages[language][role].sort()
|
|
|
|
result = OrderedDict((k, languages[k]) for k in sorted(languages))
|
|
return result
|
|
|
|
|
|
if __name__ == '__main__':
|
|
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_zanata_language_and_members()
|
|
save_to_yaml(data, output_file)
|
|
print("output is saved to filename: %s" % output_file)
|