-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·426 lines (396 loc) · 16.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python3
from flask import Flask, render_template, make_response, request, jsonify, redirect
from flask_caching import Cache
import tempfile
import os
import requests
import json
import urllib.parse
# configuration
CACHE_TIMEOUT = int(os.environ.get("APP_CACHE_TIMEOUT", 60 * 60 * 24))
TRANSIFEX_PASSWORD = os.environ["TRANSIFEX_PASSWORD"] # from https://www.transifex.com/user/settings/api/
TRANSIFEX_USERNAME = os.environ.get("TRANSIFEX_USERNAME", "secret-user") # the user for the TRANSIFEX_PASSWORD
SHIELDS_API = os.environ.get("SHIELDS_API", "https://img.shields.io") # replace this, if you run shields locally
HAS_HTTPS = os.environ.get("APP_HAS_HTTPS", "false").lower() == "true"
PORT = int(os.environ.get("PORT", "5000"))
# constants
AUTH = ("api", TRANSIFEX_PASSWORD)
EXAMPLE_ENTRIES = {
"resource_slug": "07-better-to-be-hopeful-txt",
"project_slug": "12-characters-play",
"organization_slug": "12-characters",
"language_code": "de",
"color": "green",
"label": "label",
"extension": "svg",
"stat": "translated"
}
TEMPLATE_ENTRIES = {
"resource_slug": "RESOURCE",
"project_slug": "PROJECT",
"organization_slug": "ORGANIZATION",
"language_code": "LANGUAGE",
"color": "COLOR",
"label": "LABEL"
}
PARAM_MODIFICATION = "modification"
PROTOCOL = "http" + ("s" if HAS_HTTPS else "") + "://"
# globals
app = Flask(__name__, template_folder= "templates")
# Check Configuring Flask-Cache section for more details
cache = Cache(app, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': tempfile.mktemp(prefix="cache-")})
open_apis = []
def change_name(name):
"""Change the name of a function."""
def change_to(function):
function.__name__ = name
return function
return change_to
def open_api(name, role, url, documentation, modifications={}):
"""Open the call to the API for a user."""
# get the path from a url, see https://stackoverflow.com/a/7894483/1320237
path = urllib.parse.urlparse(url).path
url_template = url.replace("<", "{").replace(">", "}")
print("url_template:", url_template)
@app.route(path, methods=['GET'])
# use query string in cache, see https://stackoverflow.com/a/47181782/1320237
@cache.cached(timeout=CACHE_TIMEOUT, key_prefix="{}/%s".format(name), query_string=True)
@change_name(name)
def get(**kw):
# request query string, see http://werkzeug.pocoo.org/docs/0.14/wrappers/#werkzeug.wrappers.BaseRequest.args
request_url = url_template.format(**kw)
args = request.args.copy()
if PARAM_MODIFICATION in args:
modification_name = args.pop(PARAM_MODIFICATION)
modification = modifications[modification_name]
else:
def modification(x):
return x
print("request.path", request.path)
params = ("?" + urllib.parse.urlencode(args) if args else "")
full_request_url = request_url + params
print("curl -i -L --user api:$TRANSIFEX_PASSWORD -X GET '{}'".format(full_request_url))
response = requests.get(request_url, auth=AUTH, params=args)
result = response.json()
result = modification(result)
# make a response, see https://stackoverflow.com/a/40211663/1320237
resp = make_response(json.dumps(result, indent=2), ) #here you could use make_response(render_template(...)) too
# flask response headers, see http://werkzeug.pocoo.org/docs/0.14/datastructures/#werkzeug.datastructures.Headers
for header in response.headers:
if header.lower().startswith("x-transifex"):
resp.headers[header] = response.headers[header]
resp.headers['Access-Control-Allow-Origin'] = '*'
resp.headers['X-App-Documentation'] = documentation
resp.headers['X-App-Url'] = full_request_url
resp.headers['X-App-Url-Template'] = url
resp.mimetype = "application/json"
return resp
example_url = PROTOCOL + "{host}" + urllib.parse.urlparse(url_template.format(**EXAMPLE_ENTRIES)).path
mods = []
for modification_name in sorted(modifications):
function = modifications[modification_name]
mod_query = "?" + PARAM_MODIFICATION + "=" + modification_name
mods.append({
"name": modification_name,
"description": function.__doc__,
"example": example_url + mod_query,
"path": path + mod_query
})
open_apis.append({
"url": url,
"documentation": documentation,
"path": path,
"name": name,
"example": example_url,
"role": role,
"get": get,
"modifications": mods
})
def modify_resources_summary(data):
"""Summarize the resources into one joint statistic."""
result = {
"stats": {
"reviewed_1":{
"name": "reviewed"
},
"translated":{
"name":"translated"
},
"language_code": data[0]["stats"].get("language_code")
}
}
for attribute in ["stringcount", "wordcount", "percentage"]:
if attribute in data[0]:
result[attribute] = sum(entry[attribute] for entry in data)
for stat in ["reviewed_1", "translated"]:
value = sum(entry["stats"][stat][attribute] for entry in data)
if attribute == "percentage":
value /= len(data) * 1.0
result["stats"][stat][attribute] = value
return result
open_api(
"Projects", "Translator",
"https://www.transifex.com/api/2/projects/",
"https://docs.transifex.com/api/projects")
open_api(
"Resources", "Translator",
"https://api.transifex.com/organizations/<organization_slug>/projects/<project_slug>/resources/",
"https://docs.transifex.com/api/resources",
modifications={"summary": modify_resources_summary})
open_api(
"Translations", "Translator",
"https://www.transifex.com/api/2/project/<project_slug>/resource/<resource_slug>/translation/<language_code>/strings/",
"https://docs.transifex.com/api/translations")
open_api(
"Reports", "Translator",
"https://api.transifex.com/organizations/<organization_slug>/reports/activity/",
"https://docs.transifex.com/api/reports")
open_api(
"Statistics", "Translator",
"https://www.transifex.com/api/2/project/<project_slug>/resource/<resource_slug>/stats/",
"https://docs.transifex.com/api/statistics")
open_api(
"Language Statistics", "Translator",
"https://www.transifex.com/api/2/project/<project_slug>/resource/<resource_slug>/stats/<language_code>",
"https://docs.transifex.com/api/statistics")
open_api(
"Languages", "Project Maintainer",
"https://www.transifex.com/api/2/project/<project_slug>/languages/",
"https://docs.transifex.com/api/languages")
open_api(
"Organizations", "Translator",
"https://api.transifex.com/organizations/",
"https://docs.transifex.com/api/organizations")
dynamic_badges = []
def dynamic_badge(name, description, app_path, query):
"""Create a badge."""
def template_url(host, args=TEMPLATE_ENTRIES):
badge_query = {
"url": PROTOCOL + host + app_path.format(**args),
"label": args["label"],
"query": query.format(**args),
"colorB": args["color"]}
for attr in ["prefix", "suffix"]:
if attr in args:
badge_query[attr] = args[attr]
url = SHIELDS_API
url += "/badge/dynamic/json.svg?"
url += urllib.parse.urlencode(badge_query)
return url
def example_url(host):
url = template_url(host, EXAMPLE_ENTRIES)
return url
def show_url(host):
url = template_url(host)
for variable in TEMPLATE_ENTRIES.values():
url = url.replace(variable, "<strong>" + variable + "</strong>")
return url
dynamic_badges.append({
"name": name,
"description": description,
"app_path": app_path,
"query": query,
"example_url": example_url,
"template_url": template_url,
"show_url": show_url
})
dynamic_badge(
"Resource: overall translation",
"How much of the resource is translated.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/",
'$[?(@.slug=="{resource_slug}")].stats[?(@.name=="translated")].percentage')
dynamic_badge(
"Resource: translation of a language",
"How much of the resource is translated in one language.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?language_code={language_code}",
'$[?(@.slug=="{resource_slug}")].stats[?(@.name=="translated")].percentage')
dynamic_badge(
"Project: overall translation",
"How much of a project is translated.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?modification=summary",
'$.stats[?(@.name=="translated")].percentage')
dynamic_badge(
"Project: translation of a language",
"How much of a project is translated in a certain language.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?modification=summary&language_code={language_code}",
'$.stats[?(@.name=="translated")].percentage')
dynamic_badge(
"Resource: overall review progress",
"How much of the resource is reviewed.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/",
'$[?(@.slug=="{resource_slug}")].stats[?(@.name=="reviewed")].percentage')
dynamic_badge(
"Resource: review progress of a language",
"How much of the resource is reviewed in one language.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?language_code={language_code}",
'$[?(@.slug=="{resource_slug}")].stats[?(@.name=="reviewed")].percentage')
dynamic_badge(
"Project: overall review progress",
"How much of a project is reviewed.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?modification=summary",
'$.stats[?(@.name=="reviewed")].percentage')
dynamic_badge(
"Project: review progress of a language",
"How much of a project is reviewed in a certain language.",
"/organizations/{organization_slug}/projects/{project_slug}/resources/?modification=summary&language_code={language_code}",
'$.stats[?(@.name=="reviewed")].percentage')
def fraction_to_color(fraction):
i = int(255 * fraction)
# hex format, see https://stackoverflow.com/a/19996754/1320237
return "#{:02X}{:02X}00".format(255 - i, i)
badges = []
DEFAULT_BADGE = {
"schemaVersion": 1,
"label": "Translation"
}
PARAMETER_EXPLANATIONS = {
"organization_slug": "the organization name from the URL",
"project_slug": "the project name from the URL",
"resource_slug": "the resource name from the URL",
"language_code": "the language you want to summarize",
"stat": "either translated or reviewed",
"extension": "the extension of the file, e.g. svg or json",
}
def badge(path):
template_path = path.replace("<", "{").replace(">", "}")
parameter_description = "<ul>"
for key, explanation in PARAMETER_EXPLANATIONS.items():
if "<" + key + ">" in path:
parameter_description += "<li><strong>" + key + "</strong>" + " - " + explanation + "</li>"
parameter_description += "</ul>"
def add_endpoint(function):
example_url = PROTOCOL + "{host}" + template_path.format(**EXAMPLE_ENTRIES)
url = PROTOCOL + "{host}" + path
json_url = PROTOCOL + "{host}" + path.replace("<extension>", "json")
badges.append({
"name": function.__name__,
"path": path,
"description": function.__doc__ + parameter_description,
"example_url": example_url,
"template_url": url,
"json_url": json_url,
})
@app.route(path, methods=['GET'])
# use query string in cache, see https://stackoverflow.com/a/47181782/1320237
@cache.cached(timeout=CACHE_TIMEOUT, query_string=True)
@change_name(function.__name__ + "_" + str(len(badges)))
def get(extension, **kw):
if extension == "json":
result = DEFAULT_BADGE.copy()
badge = function(**kw)
result.update(badge)
return jsonify(result)
kw["extension"] = "json"
query = request.args.copy()
query["url"] = PROTOCOL + request.host + template_path.format(**kw)
query_string = urllib.parse.urlencode(query)
redirect_url = SHIELDS_API + "/badge/endpoint." + extension + "?" + query_string
return redirect(redirect_url)
return function
return add_endpoint
@badge("/badge/<organization_slug>/project/<project_slug>/<stat>.<extension>")
def project_progress(organization_slug, project_slug, stat):
"""Summarize the project progress."""
return project_progress_result(organization_slug, project_slug, None, stat, None, project_slug)
@badge("/badge/<organization_slug>/project/<project_slug>/language/<language_code>/<stat>.<extension>")
def project_language_progress(organization_slug, project_slug, stat, language_code):
"""Summarize the project progress of a language."""
return project_progress_result(organization_slug, project_slug, None, stat, language_code, language_code)
@badge("/badge/<organization_slug>/project/<project_slug>/resource/<resource_slug>/<stat>.<extension>")
def resource_progress(organization_slug, project_slug, resource_slug, stat):
"""Summarize the resource progress."""
return project_progress_result(organization_slug, project_slug, resource_slug, stat, None, resource_slug)
@badge("/badge/<organization_slug>/project/<project_slug>/resource/<resource_slug>/language/<language_code>/<stat>.<extension>")
def resource_language_progress(organization_slug, project_slug, resource_slug, stat, language_code):
"""Summarize the resource progress of a language."""
return project_progress_result(organization_slug, project_slug, resource_slug, stat, language_code, resource_slug + " (" + language_code + ")")
error_responses = [
{
"error_code": "not_found",
"detail": "Not found."
},
{
"error_code": "permission_denied",
"detail": "You do not have permission to perform this action."
}
]
def project_progress_result(organization_slug, project_slug, resource_slug, stat, language_code, label):
url = "https://api.transifex.com/organizations/" + organization_slug + "/projects/" + project_slug + "/resources/"
if language_code:
url += "?language_code=" + language_code
api = requests.get(url, auth=AUTH, params=request.args)
resources = api.json()
if isinstance(resources, dict):
error = resources
assert "error_code" in error, "assume an error message {}".format(error)
print(error)
if error["error_code"] == "permission_denied":
message = "invite {} as translator to use the badge.".format(TRANSIFEX_USERNAME)
else:
message = error.get("detail", error.get("language_code", "unkown error"))
return {
"label": label,
"message": message,
"color": "red",
"isError" : True,
"cacheSeconds": CACHE_TIMEOUT
}
fraction = 0
assert stat in ("reviewed", "translated")
resource_found = False
for resource in resources:
if resource_slug is not None and resource["slug"] != resource_slug:
continue
found_stat = False
for statistic in resource["stats"].values():
if isinstance(statistic, dict) and statistic["name"] == stat:
fraction += statistic["percentage"]
found_stat = True
resource_found = True
assert found_stat, "Found the stat " + stat
if resource_slug is None:
fraction /= len(resources)
elif not resource_found:
return {
"label": label,
"message": "resource not found",
"color": "red",
"isError" : True,
"cacheSeconds": CACHE_TIMEOUT
}
# provide json service interface
# see https://shields.io/#/endpoint
result = {
"label": label,
"message": str(int(round(fraction * 100))) + "%",
"color": fraction_to_color(fraction),
"cacheSeconds": CACHE_TIMEOUT
}
return result
@badge("/badge/projects.<extension>")
def open_api_count():
"""Show the number of projects providing an open Transifex API."""
url = "https://www.transifex.com/api/2/projects/"
api = requests.get(url, auth=AUTH, params=request.args)
projects = api.json()
# provide json service interface
# see https://shields.io/#/endpoint
result = {
"label": "open transifex api projects",
"message": str(len(projects)),
"color": "green"
}
return result
@app.route("/", methods=['GET'])
def index():
return render_template(
"index.html",
open_apis=open_apis,
host=request.host,
username=TRANSIFEX_USERNAME,
dynamic_badges=dynamic_badges,
badges=badges)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=PORT)