_output.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Copyright 2026 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Output framework for the `hf` CLI."""
  15. import dataclasses
  16. import datetime
  17. import json
  18. import re
  19. import sys
  20. from collections.abc import Sequence
  21. from enum import Enum
  22. from typing import Any
  23. import typer
  24. from huggingface_hub.errors import ConfirmationError
  25. from huggingface_hub.utils import ANSI, is_agent, tabulate
  26. # TODO: remove OutputFormat in _cli_utils.py once all commands are migrated to OutputFormatWithAuto.
  27. class OutputFormatWithAuto(str, Enum):
  28. """Output format for CLI commands with auto detection of agent/human mode."""
  29. agent = "agent"
  30. auto = "auto"
  31. human = "human"
  32. json = "json"
  33. quiet = "quiet"
  34. class Output:
  35. """Output sink for the `hf` CLI.
  36. Mode is resolved once at init time based on `is_agent()` auto-detection
  37. and can be overridden per-command via `set_mode()`.
  38. """
  39. mode: OutputFormatWithAuto
  40. def __init__(self) -> None:
  41. self.set_mode()
  42. def set_mode(self, mode: OutputFormatWithAuto = OutputFormatWithAuto.auto) -> None:
  43. """Override the output mode (called by commands that receive ``--format``)."""
  44. if mode == OutputFormatWithAuto.auto:
  45. mode = OutputFormatWithAuto.agent if is_agent() else OutputFormatWithAuto.human
  46. self.mode = mode
  47. def text(self, msg: str | None = None, *, human: str | None = None, agent: str | None = None) -> None:
  48. """Print a free-form text message to stdout."""
  49. if msg is not None:
  50. if human is not None or agent is not None:
  51. raise ValueError("Cannot mix 'msg' with 'human'/'agent'.")
  52. human = msg
  53. agent = _strip_ansi(msg)
  54. match self.mode:
  55. case OutputFormatWithAuto.human:
  56. if human is not None:
  57. print(human)
  58. case OutputFormatWithAuto.agent:
  59. if agent is not None:
  60. print(agent)
  61. # json/quiet: no-op
  62. def table(
  63. self,
  64. items: Sequence[dict[str, Any]],
  65. *,
  66. headers: list[str] | None = None,
  67. id_key: str | None = None,
  68. alignments: dict[str, str] | None = None,
  69. ) -> None:
  70. """Print tabular data to stdout.
  71. Args:
  72. items: List of dicts. Headers are auto-detected from keys if not provided.
  73. headers: Explicit column names. If None, derived from dict keys (all-None columns filtered).
  74. id_key: Key to print in quiet mode. If None, uses the first header.
  75. alignments: Optional mapping of header name to "left" or "right". Defaults to "left".
  76. """
  77. if not items:
  78. match self.mode:
  79. case OutputFormatWithAuto.agent | OutputFormatWithAuto.human:
  80. print("No results found.")
  81. case OutputFormatWithAuto.json:
  82. print("[]")
  83. return
  84. if headers is None:
  85. all_columns = list(items[0].keys())
  86. headers = [col for col in all_columns if any(item.get(col) is not None for item in items)]
  87. rows = [[item.get(h) for h in headers] for item in items]
  88. match self.mode:
  89. case OutputFormatWithAuto.human: # padded table, truncated cells, SCREAMING_SNAKE headers
  90. formatted_rows: list[list[str | int]] = [[_format_table_cell_human(v) for v in row] for row in rows]
  91. screaming_headers = [_to_header(h) for h in headers]
  92. screaming_alignments = {_to_header(k): v for k, v in (alignments or {}).items()}
  93. print(tabulate(formatted_rows, headers=screaming_headers, alignments=screaming_alignments))
  94. case OutputFormatWithAuto.agent: # TSV, no truncation, full timestamps
  95. print("\t".join(headers))
  96. for row in rows:
  97. print("\t".join(_format_table_cell_agent(v) for v in row))
  98. case OutputFormatWithAuto.json: # compact JSON array
  99. print(json.dumps(list(items), default=str))
  100. case OutputFormatWithAuto.quiet: # id_key column (or first column), one per line
  101. quiet_key = id_key or headers[0]
  102. for item in items:
  103. print(item.get(quiet_key, ""))
  104. def dict(self, data: Any) -> None:
  105. """Print structured data as JSON in all modes (indented for human, compact otherwise).
  106. Accepts a dict or a dataclass.
  107. """
  108. if dataclasses.is_dataclass(data) and not isinstance(data, type):
  109. data = _dataclass_to_dict(data)
  110. indent = 2 if self.mode == OutputFormatWithAuto.human else None
  111. print(json.dumps(data, indent=indent, default=str))
  112. def result(self, message: str, **data: Any) -> None:
  113. """Print a success summary to stdout."""
  114. match self.mode:
  115. case OutputFormatWithAuto.human: # ✓ message + key: value lines
  116. parts = [ANSI.green(f"✓ {message}")]
  117. for k, v in data.items():
  118. if v is not None:
  119. parts.append(f" {k}: {v}")
  120. print("\n".join(parts))
  121. case OutputFormatWithAuto.agent: # key=val pairs, space-separated
  122. parts = [f"{k}={v}" for k, v in data.items() if v is not None]
  123. print(" ".join(parts) if parts else message)
  124. case OutputFormatWithAuto.json: # json.dumps(data), message ignored
  125. print(json.dumps(data, default=str) if data else "")
  126. case OutputFormatWithAuto.quiet: # first value only
  127. values = list(data.values())
  128. if values:
  129. print(values[0])
  130. def confirm(self, message: str, *, default: bool = False, yes: bool = False) -> None:
  131. """
  132. Ask for confirmation. Raises `ConfirmationError` in non-human modes.
  133. """
  134. if yes:
  135. return
  136. if self.mode != OutputFormatWithAuto.human:
  137. raise ConfirmationError(f"{message} Use --yes to skip confirmation.")
  138. typer.confirm(message, default=default, abort=True)
  139. def warning(self, message: str) -> None:
  140. """Print a non-fatal warning to stderr (all modes)."""
  141. if self.mode == OutputFormatWithAuto.human:
  142. print(ANSI.yellow(f"Warning: {message}"), file=sys.stderr)
  143. else:
  144. print(f"Warning: {message}", file=sys.stderr)
  145. def error(self, message: str) -> None:
  146. """Print an error to stderr (all modes)."""
  147. if self.mode == OutputFormatWithAuto.human:
  148. print(ANSI.red(f"Error: {message}"), file=sys.stderr)
  149. else:
  150. print(f"Error: {message}", file=sys.stderr)
  151. def hint(self, message: str) -> None:
  152. """Print a helpful hint to stderr (human: gray, agent/json: plain text)."""
  153. if self.mode == OutputFormatWithAuto.human:
  154. print(ANSI.gray(f"Hint: {message}"), file=sys.stderr)
  155. else:
  156. print(f"Hint: {message}", file=sys.stderr)
  157. # HELPERS
  158. def _serialize_value(v: object) -> object:
  159. """Recursively serialize a value to be JSON-compatible."""
  160. if isinstance(v, datetime.datetime):
  161. return v.isoformat()
  162. elif isinstance(v, dict):
  163. return {key: _serialize_value(val) for key, val in v.items() if val is not None}
  164. elif isinstance(v, list):
  165. return [_serialize_value(item) for item in v]
  166. return v
  167. def _dataclass_to_dict(info: Any) -> dict[str, Any]:
  168. """Convert a dataclass to a json-serializable dict."""
  169. return {k: _serialize_value(v) for k, v in dataclasses.asdict(info).items() if v is not None}
  170. _ANSI_RE = re.compile(r"\033\[[0-9;]*m")
  171. _MAX_CELL_LENGTH = 35
  172. def _strip_ansi(text: str) -> str:
  173. return _ANSI_RE.sub("", text)
  174. def _to_header(name: str) -> str:
  175. """Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE."""
  176. s = re.sub(r"([a-z])([A-Z])", r"\1_\2", name)
  177. return s.upper()
  178. def _format_table_value_human(value: Any) -> str:
  179. """Convert a value to string for terminal display."""
  180. if value is None:
  181. return ""
  182. if isinstance(value, bool):
  183. return "✔" if value else ""
  184. if isinstance(value, datetime.datetime):
  185. return value.strftime("%Y-%m-%d")
  186. if isinstance(value, str) and re.match(r"^\d{4}-\d{2}-\d{2}T", value):
  187. return value[:10]
  188. if isinstance(value, list):
  189. return ", ".join(_format_table_value_human(v) for v in value)
  190. elif isinstance(value, dict):
  191. if "name" in value: # Likely to be a user or org => print name
  192. return str(value["name"])
  193. return json.dumps(value)
  194. return str(value)
  195. def _format_table_cell_human(value: Any, max_len: int = _MAX_CELL_LENGTH) -> str:
  196. """Format a value + truncate it for table display."""
  197. cell = _format_table_value_human(value)
  198. if len(cell) > max_len:
  199. cell = cell[: max_len - 3] + "..."
  200. return cell
  201. def _format_table_cell_agent(value: Any) -> str:
  202. """Format a cell value for agent TSV output (ISO timestamps, tabs escaped)."""
  203. if isinstance(value, datetime.datetime):
  204. return value.isoformat()
  205. return str(value).replace("\t", " ")
  206. out = Output()