-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathreporting.py
More file actions
435 lines (361 loc) · 16.4 KB
/
Copy pathreporting.py
File metadata and controls
435 lines (361 loc) · 16.4 KB
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
426
427
428
429
430
431
432
433
434
435
import datetime
import glob
import re
from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from . import config as app_config
from . import client as tx_client
from . import utils as tx_utils
def _get_processed_contributor_data():
"""
Fetches and processes contributor data (translations, reviews, proofreads).
Returns a list of dictionaries, each containing:
{
"user_id": str,
"username": str,
"role": str,
"translated": int,
"reviewed": int,
"proofread": int,
"total": int
}
"""
members = tx_client.get_team_members()
users_details = {}
for member in members:
if member.user:
users_details[member.user.id] = {
"username": member.user.attributes.get("username", member.user.id),
"role": member.attributes["role"],
}
else:
# Fallback if user data is somehow missing, using member.id as key
users_details[member.id] = {
"username": "Unknown User",
"role": member.attributes["role"],
}
translators = {user_id: 0 for user_id in users_details}
reviewers = {user_id: 0 for user_id in users_details}
proofreaders = {user_id: 0 for user_id in users_details}
all_resources = tx_client.get_all_resources()
for resource in all_resources:
translations = tx_client.get_resource_translations(resource)
for translation in translations:
if (
translation.relationships.get("translator")
and translation.relationships["translator"]["data"]
):
translator_id = translation.relationships["translator"]["data"]["id"]
if translator_id in translators:
translators[translator_id] += 1
if (
translation.relationships.get("reviewer")
and translation.relationships["reviewer"]["data"]
):
reviewer_id = translation.relationships["reviewer"]["data"]["id"]
if reviewer_id in reviewers:
reviewers[reviewer_id] += 1
if (
translation.relationships.get("proofreader")
and translation.relationships["proofreader"]["data"]
):
proofreader_id = translation.relationships["proofreader"]["data"]["id"]
if proofreader_id in proofreaders:
proofreaders[proofreader_id] += 1
processed_data = []
for user_id, details in users_details.items():
t = translators.get(user_id, 0)
r = reviewers.get(user_id, 0)
p = proofreaders.get(user_id, 0)
total_contributions = t + r + p
processed_data.append(
{
"user_id": user_id,
"username": details["username"],
"role": details["role"],
"translated": t,
"reviewed": r,
"proofread": p,
"total": total_contributions,
}
)
return processed_data
class ReportGenerator:
"""Base class for report generators."""
def __init__(self):
pass
def generate(self):
raise NotImplementedError("Subclasses must implement the 'generate' method.")
class TxConfigReporter(ReportGenerator):
"""Generates the Transifex client configuration file (.tx/config)."""
def generate(self) -> None:
resources = tx_client.get_all_resources()
output_path = Path(app_config.TX_CONFIG_PATH)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as fo:
fo.writelines(("[main]\n", "host = https://www.transifex.com\n"))
for resource in resources:
path_obj = tx_utils.slug_to_file_path(resource.slug)
fo.writelines(
(
f"\n[{app_config.PROJECT.id}:{resource.slug}]\n",
f"file_filter = {str(path_obj)}\n",
"type = PO\n",
"source_lang = en\n",
f"source_file = {str(path_obj)}\n",
f"trans.{app_config.LANG} = {str(path_obj)}\n",
)
)
print(f"Generated Transifex config at {output_path}")
class ResourceStatsMarkdownReporter(ReportGenerator):
"""Generates a Markdown file with resource translation statistics."""
def generate(self) -> None:
stats = tx_client.get_resource_language_stats()
output_path = Path(app_config.RESOURCE_STATS_MD_PATH)
headers_conf = app_config.REPORT_HEADERS["resource_stats"]
header_line = f"| {headers_conf['file']} | {headers_conf['translated']} | {headers_conf['reviewed']} | {headers_conf['proofread']} |\n"
alignment_line = headers_conf["alignment"]
rows_data = []
for stat in stats:
resource_id_str = stat.relationships["resource"]["data"]["id"]
resource_slug = resource_id_str.split(":")[-1]
file_name = tx_utils.slug_to_file_path(resource_slug)
total_words = stat.attributes["total_words"]
if total_words == 0:
translated_pct, reviewed_pct, proofread_pct = 0.0, 0.0, 0.0
else:
translated_pct = round(
100 * stat.attributes["translated_words"] / total_words, 1
)
reviewed_pct = round(
100 * stat.attributes["reviewed_words"] / total_words, 1
)
proofread_pct = round(
100 * stat.attributes["proofread_words"] / total_words, 1
)
rows_data.append((file_name, translated_pct, reviewed_pct, proofread_pct))
# Sort: first by reviewed_pct (desc), then by translated_pct (desc)
rows_sorted = sorted(
rows_data,
key=lambda row: (
row[2],
row[1],
), # row[2]=reviewed_pct, row[1]=translated_pct
reverse=True,
)
with open(output_path, "w", encoding="utf-8") as fo:
fo.writelines((header_line, alignment_line))
for file_name, translated_pct, reviewed_pct, proofread_pct in rows_sorted:
fo.writelines(
f"| {file_name} | {translated_pct}% | {reviewed_pct}% | {proofread_pct}% |\n"
)
print(f"Generated resource stats at {output_path}")
class TeamStatsMarkdownReporter(ReportGenerator):
"""Generates a Markdown file with team contribution statistics."""
def _parse_count_from_string(self, count_str: str) -> int:
"""Extracts the base number from a count string like '123 (+4)'."""
match = re.match(r"(\d+)", count_str.strip())
if match:
return int(match.group(1))
return 0
def _format_count_with_diff(self, new_count: int, diff: int) -> str:
"""Formats the count string to include the difference."""
if diff > 0:
return f"{new_count} (+{diff})"
return str(new_count)
def generate(self) -> None:
contributor_data = _get_processed_contributor_data()
output_path = Path(app_config.TEAM_STATS_MD_PATH)
headers_conf = app_config.REPORT_HEADERS["team_stats"]
header_line = f"| {headers_conf['user']} | {headers_conf['role']} | {headers_conf['translated_count']} | {headers_conf['reviewed_count']} | {headers_conf['proofread_count']} |\n"
alignment_line = headers_conf["alignment"]
old_stats = {}
if output_path.exists():
try:
with open(output_path, "r", encoding="utf-8") as f_old:
lines = f_old.readlines()
if (
len(lines) > 2
): # Check if there's data beyond header and alignment
for line in lines[2:]: # Skip header and alignment
parts = [part.strip() for part in line.split("|")]
if (
len(parts) >= 6 and parts[1]
): # Ensure valid row structure
username = parts[1]
old_stats[username] = {
"translated": self._parse_count_from_string(
parts[3]
),
"reviewed": self._parse_count_from_string(parts[4]),
"proofread": self._parse_count_from_string(
parts[5]
),
}
except Exception as e:
print(f"Warning: Could not read or parse existing TEAM.md: {e}")
old_stats = {} # Reset if parsing fails
rows_sorted = sorted(contributor_data, key=lambda x: x["total"], reverse=True)
with open(output_path, "w", encoding="utf-8") as fo:
fo.writelines((header_line, alignment_line))
for contributor in rows_sorted:
username = contributor["username"]
new_translated = contributor["translated"]
new_reviewed = contributor["reviewed"]
new_proofread = contributor["proofread"]
translated_str = str(new_translated)
reviewed_str = str(new_reviewed)
proofread_str = str(new_proofread)
if username in old_stats:
prev_user_stats = old_stats[username]
diff_t = new_translated - prev_user_stats.get("translated", 0)
diff_r = new_reviewed - prev_user_stats.get("reviewed", 0)
diff_p = new_proofread - prev_user_stats.get("proofread", 0)
translated_str = self._format_count_with_diff(
new_translated, diff_t
)
reviewed_str = self._format_count_with_diff(new_reviewed, diff_r)
proofread_str = self._format_count_with_diff(new_proofread, diff_p)
fo.writelines(
f"| {username} | {contributor['role']} | {translated_str} | {reviewed_str} | {proofread_str} |\n"
)
print(f"Generated team stats at {output_path}")
class ContributorChartReporter(ReportGenerator):
"""Generates a bar chart of user contributions."""
def __init__(self, top_n=10):
super().__init__()
self.top_n = top_n
def generate(self) -> None:
contributor_data = _get_processed_contributor_data()
chart_data = [c for c in contributor_data if c["total"] > 0]
sorted_contributions = sorted(
chart_data, key=lambda x: x["total"], reverse=True
)
if self.top_n and len(sorted_contributions) > self.top_n:
sorted_contributions = sorted_contributions[: self.top_n]
if not sorted_contributions:
print("No contributor data to generate chart.")
return
usernames = [item["username"] for item in sorted_contributions]
totals = [item["total"] for item in sorted_contributions]
bar_colors = [
app_config.CHART_PASTEL_COLORS[i % len(app_config.CHART_PASTEL_COLORS)]
for i in range(len(usernames))
]
plt.figure(figsize=(12, 7))
bars = plt.bar(usernames, totals, color=bar_colors)
chart_labels = app_config.REPORT_HEADERS["contributor_chart"]
chart_title = chart_labels["title_base"]
if self.top_n:
chart_title += chart_labels["title_top_n_suffix"].format(top_n=self.top_n)
plt.title(chart_title)
plt.xlabel(chart_labels["xlabel_username"])
plt.ylabel(chart_labels["ylabel_total_contributions"])
plt.xticks(rotation=45, ha="right")
if mticker:
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(integer=True))
plt.grid(axis="y", linestyle="--", alpha=0.7)
for bar in bars:
height = bar.get_height()
if height > 0:
plt.text(
bar.get_x() + bar.get_width() / 2.0,
height + 0.1,
f"{int(height)}",
ha="center",
fontweight="bold",
)
plt.tight_layout()
chart_dir = Path(app_config.CONTRIBUTOR_CHART_DIR)
chart_dir.mkdir(parents=True, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y_%m_%d")
filename = (
chart_dir / f"{app_config.CONTRIBUTOR_CHART_FILENAME_PREFIX}{timestamp}.png"
)
plt.savefig(filename)
plt.close()
print(f"Saved user contributions chart to {filename}")
class ReadmeUpdaterReporter(ReportGenerator):
"""Updates the README.md file with the latest statistics chart."""
def _find_latest_chart(self) -> str | None:
"""Finds the latest contributor chart image."""
chart_dir = Path(app_config.CONTRIBUTOR_CHART_DIR)
pattern = str(
chart_dir / f"{app_config.CONTRIBUTOR_CHART_FILENAME_PREFIX}*.png"
)
files = glob.glob(pattern)
if not files:
return None
# Sort files by name (timestamp ensures latest is last)
latest_file_path = Path(sorted(files)[-1])
# Return path relative to the project root
relative_path = latest_file_path.relative_to(app_config.PROJECT_ROOT)
return relative_path.as_posix()
def generate(self) -> None:
readme_path = app_config.README_PATH
print(f"Updating README.md at {readme_path}")
if not readme_path.exists():
print(f"Error: README.md not found at {readme_path}")
return
latest_chart_path = self._find_latest_chart()
if not latest_chart_path:
print("Warning: No contributor chart found to update README.")
return
today_display = datetime.datetime.now().strftime("%Y-%m-%d")
stats_section_content = (
f"### {app_config.README_CONTRIBUTORS_HEADER}\n"
f"\n"
f"({app_config.README_UPDATED_ON}: {today_display})"
)
full_replacement_text = (
f"{app_config.README_STATS_START_MARKER}\n"
f"{stats_section_content}\n"
f"{app_config.README_STATS_END_MARKER}"
)
with open(readme_path, "r+", encoding="utf-8") as f:
content = f.read()
# Pattern to find the section between markers
pattern = re.compile(
f"{re.escape(app_config.README_STATS_START_MARKER)}.*?{re.escape(app_config.README_STATS_END_MARKER)}",
re.DOTALL,
)
if pattern.search(content):
updated_content = pattern.sub(full_replacement_text, content)
else:
print(
f"Warning: Markers {app_config.README_STATS_START_MARKER} not found. Appending stats section."
)
updated_content = content.rstrip() + "\n\n" + full_replacement_text
f.seek(0)
f.write(updated_content)
f.truncate()
print(
f"README.md updated successfully at {readme_path} with chart {latest_chart_path}"
)
class CombinedStatsReporter(ReportGenerator):
"""Generates all reports in a single run"""
def generate(self) -> None:
print("Generating all reports in a single run...")
# Generate resource stats
resource_reporter = ResourceStatsMarkdownReporter()
resource_reporter.generate()
# Generate team stats
team_reporter = TeamStatsMarkdownReporter()
team_reporter.generate()
# Generate contributor chart
chart_reporter = ContributorChartReporter()
chart_reporter.generate()
# Update README with latest stats
readme_reporter = ReadmeUpdaterReporter()
readme_reporter.generate()
print("All reports generated successfully!")
# Mapping commands to reporter classes
REPORTERS = {
"recreate-config": TxConfigReporter,
"recreate-resource-stats": ResourceStatsMarkdownReporter,
"recreate-team-stats": TeamStatsMarkdownReporter,
"generate-contributor-chart": ContributorChartReporter,
"update-readme": ReadmeUpdaterReporter,
"generate-all-stats": CombinedStatsReporter,
}