|
8 | 8 | import re |
9 | 9 | import inspect |
10 | 10 | import datetime |
| 11 | +import json |
11 | 12 |
|
12 | 13 | from typing import Any, Callable, Sequence, TypeVar |
13 | 14 | from threading import Lock |
14 | 15 | from enum import Enum |
15 | 16 | from dataclasses import dataclass |
| 17 | +from rich.text import Text |
| 18 | +from rich.console import Console |
16 | 19 |
|
17 | 20 | from . import _engine |
18 | 21 | from . import index |
@@ -451,8 +454,58 @@ def _lazy_engine_flow() -> _engine.Flow: |
451 | 454 | return engine_flow |
452 | 455 | self._lazy_engine_flow = _lazy_engine_flow |
453 | 456 |
|
| 457 | + def _format_flow(self, flow_dict: dict) -> Text: |
| 458 | + output = Text() |
| 459 | + |
| 460 | + def add_line(content, indent=0, style=None, end="\n"): |
| 461 | + output.append(" " * indent) |
| 462 | + output.append(content, style=style) |
| 463 | + output.append(end) |
| 464 | + |
| 465 | + def format_key_value(key, value, indent): |
| 466 | + if isinstance(value, (dict, list)): |
| 467 | + add_line(f"- {key}:", indent, style="green") |
| 468 | + format_data(value, indent + 2) |
| 469 | + else: |
| 470 | + add_line(f"- {key}:", indent, style="green", end="") |
| 471 | + add_line(f" {value}", style="yellow") |
| 472 | + |
| 473 | + def format_data(data, indent=0): |
| 474 | + if isinstance(data, dict): |
| 475 | + for key, value in data.items(): |
| 476 | + format_key_value(key, value, indent) |
| 477 | + elif isinstance(data, list): |
| 478 | + for i, item in enumerate(data): |
| 479 | + format_key_value(f"[{i}]", item, indent) |
| 480 | + else: |
| 481 | + add_line(str(data), indent, style="yellow") |
| 482 | + |
| 483 | + # Header |
| 484 | + flow_name = flow_dict.get("name", "Unnamed") |
| 485 | + add_line(f"Flow: {flow_name}", style="bold cyan") |
| 486 | + |
| 487 | + # Section |
| 488 | + for section_title, section_key in [ |
| 489 | + ("Sources:", "import_ops"), |
| 490 | + ("Processing:", "reactive_ops"), |
| 491 | + ("Targets:", "export_ops"), |
| 492 | + ]: |
| 493 | + add_line("") |
| 494 | + add_line(section_title, style="bold cyan") |
| 495 | + format_data(flow_dict.get(section_key, []), indent=0) |
| 496 | + |
| 497 | + return output |
| 498 | + |
| 499 | + def _render_text(self) -> Text: |
| 500 | + flow_spec_str = str(self._lazy_engine_flow()) |
| 501 | + try: |
| 502 | + flow_dict = json.loads(flow_spec_str) |
| 503 | + return self._format_flow(flow_dict) |
| 504 | + except json.JSONDecodeError: |
| 505 | + return Text(flow_spec_str) |
| 506 | + |
454 | 507 | def __str__(self): |
455 | | - return str(self._lazy_engine_flow()) |
| 508 | + return str(self._render_text()) |
456 | 509 |
|
457 | 510 | def __repr__(self): |
458 | 511 | return repr(self._lazy_engine_flow()) |
|
0 commit comments