|
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 |
@@ -295,59 +296,128 @@ def _show_collect_results(self): |
295 | 296 | ) |
296 | 297 | Color.echo(file_list) |
297 | 298 |
|
| 299 | + # ---------------- New helpers for verbose group printing ---------------- # |
| 300 | + |
| 301 | + def _normalize_array(self, maybe): |
| 302 | + """Normalize possibly-stringified arrays into a python list.""" |
| 303 | + if maybe is None: |
| 304 | + return [] |
| 305 | + if isinstance(maybe, list): |
| 306 | + return maybe |
| 307 | + if isinstance(maybe, (str, bytes)): |
| 308 | + try: |
| 309 | + parsed = json.loads(maybe) |
| 310 | + return parsed if isinstance(parsed, list) else [] |
| 311 | + except Exception: |
| 312 | + return [] |
| 313 | + return [] |
| 314 | + |
| 315 | + def _print_verbose_group(self, label, items): |
| 316 | + """Print one verbose group (Created/Updated/etc) in a readable format.""" |
| 317 | + arr = self._normalize_array(items) |
| 318 | + if not arr: |
| 319 | + return |
| 320 | + Color.echo(' [high]{label}:[end] [warn]{n}[end]'.format( |
| 321 | + label=label, n=len(arr) |
| 322 | + )) |
| 323 | + for item in arr: |
| 324 | + string = item.get('string', '') |
| 325 | + key = item.get('key', '') |
| 326 | + occurrences = item.get('occurrences', []) |
| 327 | + context = item.get('context', []) |
| 328 | + if not isinstance(occurrences, list): |
| 329 | + occurrences = [] |
| 330 | + if not isinstance(context, list): |
| 331 | + context = [] |
| 332 | + |
| 333 | + Color.echo(' └─ {}'.format(json.dumps(string))) |
| 334 | + Color.echo(' └─ key: {}'.format(json.dumps(key))) |
| 335 | + Color.echo(' └─ occurrences: {}'.format(json.dumps(occurrences))) |
| 336 | + Color.echo(' └─ context: {}'.format(json.dumps(context))) |
| 337 | + |
| 338 | + # ------------------------------------------------------------------------ # |
| 339 | + |
298 | 340 | def _show_push_results(self, status_code, response_content): |
299 | 341 | """Display results of pushing the source strings to CDS. |
300 | 342 |
|
301 | 343 | :param int status_code: the HTTP status code |
302 | 344 | :param dict response_content: the content of the response |
303 | 345 | """ |
304 | 346 | try: |
305 | | - data = response_content.get('data') |
| 347 | + data = response_content.get('data', {}) |
306 | 348 | status = data.get('status') |
307 | | - errors = data.get('errors', []) |
| 349 | + errors = data.get('errors', []) or [] |
308 | 350 | 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') |
| 351 | + details = data.get('details', {}) or {} |
| 352 | + created = details.get('created', 0) |
| 353 | + updated = details.get('updated', 0) |
| 354 | + skipped = details.get('skipped', 0) |
| 355 | + deleted = details.get('deleted', 0) |
| 356 | + failed = details.get('failed', 0) |
| 357 | + verbose = details.get('verbose', {}) or {} |
| 358 | + |
315 | 359 | Color.echo( |
316 | 360 | '[green]\nSuccessfully pushed strings to Transifex.[end]' |
317 | 361 | ) |
318 | 362 |
|
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)) |
| 363 | + if verbose: |
| 364 | + self._print_verbose_group( |
| 365 | + 'Created strings', |
| 366 | + verbose.get('created') |
| 367 | + ) |
| 368 | + self._print_verbose_group( |
| 369 | + 'Updated strings', |
| 370 | + verbose.get('updated') |
| 371 | + ) |
| 372 | + self._print_verbose_group( |
| 373 | + 'Skipped strings', |
| 374 | + verbose.get('skipped') |
| 375 | + ) |
| 376 | + self._print_verbose_group( |
| 377 | + 'Deleted strings', |
| 378 | + verbose.get('deleted') |
| 379 | + ) |
| 380 | + self._print_verbose_group( |
| 381 | + 'Failed strings', |
| 382 | + verbose.get('failed') |
| 383 | + ) |
| 384 | + else: |
| 385 | + |
| 386 | + if created > 0: |
| 387 | + Color.echo( |
| 388 | + '[high]Created strings:[end] ' |
| 389 | + '[warn]{created}[end]'.format(created=created)) |
| 390 | + |
| 391 | + if updated > 0: |
| 392 | + Color.echo( |
| 393 | + '[high]Updated strings:[end] ' |
| 394 | + '[warn]{updated}[end]'.format(updated=updated)) |
| 395 | + |
| 396 | + if skipped > 0: |
| 397 | + Color.echo( |
| 398 | + '[high]Skipped strings:[end] ' |
| 399 | + '[warn]{skipped}[end]'.format(skipped=skipped)) |
| 400 | + |
| 401 | + if deleted > 0: |
| 402 | + Color.echo( |
| 403 | + '[high]Deleted strings:[end] ' |
| 404 | + '[warn]{deleted}[end]'.format(deleted=deleted)) |
| 405 | + |
| 406 | + if failed > 0: |
| 407 | + Color.echo( |
| 408 | + '[high]Failed strings:[end] ' |
| 409 | + '[warn]{failed}[end]'.format(failed=failed)) |
| 410 | + |
343 | 411 | else: |
344 | 412 | Color.echo( |
345 | 413 | '[error]\nCould not push strings to Transifex.[end]') |
346 | 414 |
|
347 | | - if len(errors) > 0: |
| 415 | + if errors: |
348 | 416 | Color.echo( |
349 | 417 | '[high]Errors:[end] {errors}[end]\n'.format( |
350 | | - errors='\n'.join(errors) |
| 418 | + errors='\n'.join( |
| 419 | + [json.dumps(e) if not isinstance(e, str) else e for e in errors] |
| 420 | + ) |
351 | 421 | ) |
352 | 422 | ) |
353 | 423 | except Exception: |
|
0 commit comments