|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import time |
| 6 | +import json |
6 | 7 |
|
7 | 8 | from django import VERSION as DJANGO_VERSION |
8 | 9 | from django.conf import settings |
@@ -230,7 +231,7 @@ def _extract_strings(self, translatable_file): |
230 | 231 |
|
231 | 232 | Supports both Python files and Django template files. |
232 | 233 |
|
233 | | - :param TranslatableFile translatable_file: the file to search |
| 234 | + :: TranslatableFile translatable_file: the file to search |
234 | 235 | :return: a list of SourceString objects |
235 | 236 | :rtype: list |
236 | 237 | """ |
@@ -295,59 +296,116 @@ def _show_collect_results(self): |
295 | 296 | ) |
296 | 297 | Color.echo(file_list) |
297 | 298 |
|
| 299 | + |
| 300 | + def _print_verbose_group(self, label, items): |
| 301 | + """Print one verbose group (Created/Updated/etc) in a readable format. |
| 302 | +
|
| 303 | + :param label: The label of the verbose group. |
| 304 | + :param items: The items to print like objects (with .string, .key, |
| 305 | + .context, .occurrences). |
| 306 | + """ |
| 307 | + if not items: |
| 308 | + return |
| 309 | + |
| 310 | + Color.echo(' [high]{label}:[end] [warn]{n}[end]'.format( |
| 311 | + label=label, n=len(items) |
| 312 | + )) |
| 313 | + |
| 314 | + for i, item in enumerate(items, 1): |
| 315 | + s = item.get('string', '') |
| 316 | + key = item.get('key', '') |
| 317 | + ctx = ', '.join(item.get('context', []) or []) |
| 318 | + occ = ', '.join(item.get('occurrences', []) or []) |
| 319 | + |
| 320 | + Color.echo(f' [pink]{i}.[end] [green]"{s}"[end]') |
| 321 | + if key: |
| 322 | + Color.echo(f' [high]key:[end] "[yel]{key}[end]"') |
| 323 | + if ctx: |
| 324 | + Color.echo(f' [high]context:[end] {ctx}') |
| 325 | + if occ: |
| 326 | + Color.echo(f' [high]occurrences:[end] [file]{occ}[end]') |
| 327 | + |
298 | 328 | def _show_push_results(self, status_code, response_content): |
299 | 329 | """Display results of pushing the source strings to CDS. |
300 | 330 |
|
301 | | - :param int status_code: the HTTP status code |
302 | | - :param dict response_content: the content of the response |
| 331 | + :: int status_code: the HTTP status code |
| 332 | + :: dict response_content: the content of the response |
303 | 333 | """ |
304 | 334 | try: |
305 | | - data = response_content.get('data') |
| 335 | + data = response_content.get('data', {}) |
306 | 336 | status = data.get('status') |
307 | | - errors = data.get('errors', []) |
| 337 | + errors = data.get('errors', []) or [] |
308 | 338 | if status == 'completed': |
309 | | - details = data.get('details') |
310 | | - created = details.get('created') |
311 | | - updated = details.get('updated') |
312 | | - skipped = details.get('skipped') |
313 | | - deleted = details.get('deleted') |
314 | | - failed = details.get('failed') |
| 339 | + details = data.get('details', {}) or {} |
| 340 | + created = details.get('created', 0) |
| 341 | + updated = details.get('updated', 0) |
| 342 | + skipped = details.get('skipped', 0) |
| 343 | + deleted = details.get('deleted', 0) |
| 344 | + failed = details.get('failed', 0) |
| 345 | + verbose = details.get('verbose', {}) or {} |
| 346 | + |
315 | 347 | Color.echo( |
316 | 348 | '[green]\nSuccessfully pushed strings to Transifex.[end]' |
317 | 349 | ) |
318 | 350 |
|
319 | | - if created > 0: |
320 | | - Color.echo( |
321 | | - '[high]Created strings:[end] ' |
322 | | - '[warn]{created}[end]'.format(created=created)) |
323 | | - |
324 | | - if updated > 0: |
325 | | - Color.echo( |
326 | | - '[high]Updated strings:[end] ' |
327 | | - '[warn]{updated}[end]'.format(updated=updated)) |
328 | | - |
329 | | - if skipped > 0: |
330 | | - Color.echo( |
331 | | - '[high]Skipped strings:[end] ' |
332 | | - '[warn]{skipped}[end]'.format(skipped=skipped)) |
333 | | - |
334 | | - if deleted > 0: |
335 | | - Color.echo( |
336 | | - '[high]Deleted strings:[end] ' |
337 | | - '[warn]{deleted}[end]'.format(deleted=deleted)) |
338 | | - |
339 | | - if failed > 0: |
340 | | - Color.echo( |
341 | | - '[high]Failed strings:[end] ' |
342 | | - '[warn]{failed}[end]'.format(failed=failed)) |
| 351 | + if verbose and self.verbose_output: |
| 352 | + self._print_verbose_group( |
| 353 | + 'Created strings', |
| 354 | + verbose.get('created') |
| 355 | + ) |
| 356 | + self._print_verbose_group( |
| 357 | + 'Updated strings', |
| 358 | + verbose.get('updated') |
| 359 | + ) |
| 360 | + self._print_verbose_group( |
| 361 | + 'Deleted strings', |
| 362 | + verbose.get('deleted') |
| 363 | + ) |
| 364 | + self._print_verbose_group( |
| 365 | + 'Skipped strings', |
| 366 | + verbose.get('skipped') |
| 367 | + ) |
| 368 | + self._print_verbose_group( |
| 369 | + 'Failed strings', |
| 370 | + verbose.get('failed') |
| 371 | + ) |
| 372 | + else: |
| 373 | + |
| 374 | + if created > 0: |
| 375 | + Color.echo( |
| 376 | + '[high]Created strings:[end] ' |
| 377 | + '[warn]{created}[end]'.format(created=created)) |
| 378 | + |
| 379 | + if updated > 0: |
| 380 | + Color.echo( |
| 381 | + '[high]Updated strings:[end] ' |
| 382 | + '[warn]{updated}[end]'.format(updated=updated)) |
| 383 | + |
| 384 | + if deleted > 0: |
| 385 | + Color.echo( |
| 386 | + '[high]Deleted strings:[end] ' |
| 387 | + '[warn]{deleted}[end]'.format(deleted=deleted)) |
| 388 | + |
| 389 | + if skipped > 0: |
| 390 | + Color.echo( |
| 391 | + '[high]Skipped strings:[end] ' |
| 392 | + '[warn]{skipped}[end]'.format(skipped=skipped)) |
| 393 | + |
| 394 | + if failed > 0: |
| 395 | + Color.echo( |
| 396 | + '[high]Failed strings:[end] ' |
| 397 | + '[warn]{failed}[end]'.format(failed=failed)) |
| 398 | + |
343 | 399 | else: |
344 | 400 | Color.echo( |
345 | 401 | '[error]\nCould not push strings to Transifex.[end]') |
346 | 402 |
|
347 | | - if len(errors) > 0: |
| 403 | + if errors: |
348 | 404 | Color.echo( |
349 | 405 | '[high]Errors:[end] {errors}[end]\n'.format( |
350 | | - errors='\n'.join(errors) |
| 406 | + errors='\n'.join( |
| 407 | + [json.dumps(e) if not isinstance(e, str) else e for e in errors] |
| 408 | + ) |
351 | 409 | ) |
352 | 410 | ) |
353 | 411 | except Exception: |
|
0 commit comments