Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

70 linhas
1.9 KiB

  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING, Any, Dict, List, Optional
  3. from rich.console import Console, RenderableType
  4. from rich.live import Live
  5. from rich.text import Text
  6. from typing_extensions import Literal
  7. from .element import Element
  8. if TYPE_CHECKING:
  9. from .styles.base import BaseStyle
  10. class ProgressLine(Element):
  11. def __init__(self, text: str | Text, parent: Progress):
  12. self.text = text
  13. self.parent = parent
  14. class Progress(Live, Element):
  15. current_message: str | Text
  16. def __init__(
  17. self,
  18. title: str,
  19. style: Optional[BaseStyle] = None,
  20. console: Optional[Console] = None,
  21. transient: bool = False,
  22. transient_on_error: bool = False,
  23. inline_logs: bool = False,
  24. lines_to_show: int = -1,
  25. **metadata: Dict[Any, Any],
  26. ) -> None:
  27. self.title = title
  28. self.current_message = title
  29. self.is_error = False
  30. self._transient_on_error = transient_on_error
  31. self._inline_logs = inline_logs
  32. self.lines_to_show = lines_to_show
  33. self.logs: List[ProgressLine] = []
  34. self.metadata = metadata
  35. self._cancelled = False
  36. Element.__init__(self, style=style)
  37. super().__init__(console=console, refresh_per_second=8, transient=transient)
  38. # TODO: remove this once rich uses "Self"
  39. def __enter__(self) -> "Progress":
  40. self.start(refresh=self._renderable is not None)
  41. return self
  42. def get_renderable(self) -> RenderableType:
  43. return self.style.render_element(self, done=not self._started)
  44. def log(self, text: str | Text) -> None:
  45. if self._inline_logs:
  46. self.logs.append(ProgressLine(text, self))
  47. else:
  48. self.current_message = text
  49. def set_error(self, text: str) -> None:
  50. self.current_message = text
  51. self.is_error = True
  52. self.transient = self._transient_on_error