GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — esptool/esptool/logger.py at master · espressif/esptool Expand file tree/ Copy pathlogger.pyMore file actions 334 lines (270 loc) · 12.2 KB / Copy pathlogger.pyFile metadata and controls334 lines (270 loc) · 12.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 # SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD # # SPDX-License-Identifier: GPL-2.0-or-later """Esptool logger. `EsptoolLogger` is a thin subclass over `esp_pylib.logger.EspLog` with esptool-specific collapsible-stage gating (``_smart_features``, ``--trace``, ``compact`` / ``auto`` verbosity). Collapsible stages themselves are implemented in esp-pylib; this module only overrides `_stage_can_collapse` and `print` where esptool behaviour diverges from the shared default. Internal code should call `warn` / `err` (esp-pylib canonical API, including optional IDE ``suggestion``). `warning` / `error` remain as backward-compatible aliases on `EsptoolLogger`. `TemplateLogger` is a **standalone** ABC (it does not subclass `EspLogBase`) so the documented scripting recipe keeps a stable, seven-method contract even as esp-pylib evolves. `set_logger` wraps legacy instances in `_LegacyLoggerAdapter`, which implements `EspLogBase` and delegates to the template while inheriting esp-pylib helpers such as `die` and `progress`. ``log`` is re-exported from `esp_pylib.logger` (same proxy object that reads ``EspLog.instance``) after installing `EsptoolLogger`. """ from abc import ABC, abstractmethod from typing import Any from esp_pylib.logger import EspLog, EspLogBase, Verbosity, log __all__ = [ "EsptoolLogger", "TemplateLogger", "log", ] class TemplateLogger(ABC): """Legacy abstract interface for custom loggers used with ``log.set_logger``. Preserved for the documented "Custom Logger" scripting recipe (and third-party integrations following it). Intentionally **not** a subclass of `esp_pylib.logger.EspLogBase` so esp-pylib 1.x interface growth (``hint``, ``progress``, signature tweaks) does not break subclasses that only implement the seven historical methods. New integrations should subclass `EspLogBase` and call ``log.set_logger(...)`` with that instance directly. Default `warn` / `err` forward to ``warning`` / ``error``; ``debug`` is a no-op so esptool can call the esp-pylib names on installed legacy loggers without requiring extra methods on the subclass. """ @abstractmethod def print(self, *args, **kwargs): """ Log a plain message. """ pass @abstractmethod def note(self, message: str): """ Log a Note: message. """ pass @abstractmethod def warning(self, message: str): """ Log a Warning: message. """ pass @abstractmethod def error(self, message: str): """ Log an error message. """ pass @abstractmethod def stage(self, finish: bool = False): """ Start or finish a new collapsible stage. """ pass @abstractmethod def progress_bar( self, cur_iter: int, total_iters: int, prefix: str = "", suffix: str = "", bar_length: int = 30, ): """ Print a progress bar. """ pass @abstractmethod def set_verbosity(self, verbosity: str): """ Set the verbosity level. """ pass def warn(self, message: str, suggestion: str | None = None) -> None: """Forward to `warning` for legacy custom loggers. ``suggestion`` is accepted for API compatibility with esp-pylib but is not passed through — legacy integrators only implement `warning(message)`. """ self.warning(message) def err(self, message: str, suggestion: str | None = None) -> None: """Forward to `error` for legacy custom loggers.""" self.error(message) def debug(self, *args: Any) -> None: """Fallback to `print` for legacy custom loggers.""" self.print("".join(str(a) for a in args)) class _LegacyLoggerAdapter(EspLogBase): """`EspLogBase` facade installed when ``set_logger`` receives a `TemplateLogger`. Delegates the seven legacy hooks to the wrapped logger. Inherits esp-pylib's default ``die``, ``progress``, and ``counter`` so internal callers can use the full pylib API without extending the public `TemplateLogger` contract. Exposes ``warning`` / ``error`` aliases (matching `EsptoolLogger`) so esptool can keep calling those names after a legacy logger is installed. ``hint`` is a no-op because legacy loggers have no corresponding hook. """ __slots__ = ("_legacy",) def __init__(self, legacy: TemplateLogger) -> None: self._legacy = legacy def print(self, *args, **kwargs) -> None: self._legacy.print(*args, **kwargs) def note(self, message: str) -> None: self._legacy.note(message) def warn(self, *args: Any, suggestion: str | None = None) -> None: self._legacy.warn(" ".join(str(a) for a in args), suggestion=suggestion) def err(self, *args: Any, suggestion: str | None = None) -> None: self._legacy.err(" ".join(str(a) for a in args), suggestion=suggestion) def warning(self, message: str, suggestion: str | None = None) -> None: """Backward-compatible alias used by esptool and scripting docs.""" self.warn(message, suggestion=suggestion) def error(self, message: str, suggestion: str | None = None) -> None: """Backward-compatible alias used by esptool and scripting docs.""" self.err(message, suggestion=suggestion) def debug(self, *args: Any) -> None: self._legacy.debug("".join(str(a) for a in args)) def hint(self, message: str) -> None: self.print(message) def stage(self, finish: bool = False) -> None: self._legacy.stage(finish=finish) def progress_bar( self, cur_iter: int, total_iters: int, prefix: str = "", suffix: str = "", bar_length: int = 30, ) -> None: self._legacy.progress_bar( cur_iter, total_iters, prefix=prefix, suffix=suffix, bar_length=bar_length, ) def set_verbosity(self, mode: int | str) -> None: self._legacy.set_verbosity(mode) # type: ignore[arg-type] # Legacy CLI / scripting strings mapped onto `Verbosity`. ``verbose`` # uses `Verbosity.VERBOSE` so ``--verbose`` matches esp-pylib (``debug()`` # output, non-collapsing progress bars). Stage collapsing is disabled for # VERBOSE via `EsptoolLogger._stage_can_collapse` (and for ``--trace`` # via ``_smart_features`` in `esptool.__init__`). _VERBOSITY_LEVEL_MAP = { "auto": Verbosity.NORMAL, "verbose": Verbosity.VERBOSE, "silent": Verbosity.SILENT, "compact": Verbosity.NORMAL, } class EsptoolLogger(EspLog): """Esptool's default logger. Subclasses `esp_pylib.logger.EspLog` so Rich rendering, IDE WebSocket forwarding, collapsible stages, and ``--silent`` gating are shared with the rest of the Espressif tools, while keeping esptool-specific behaviour: * `_stage_can_collapse` honours ``_smart_features``: ``auto`` defers to esp-pylib's TTY detection, ``compact`` forces collapsing on, and ``--trace`` forces it off. * `print` keeps historical builtin-``print`` semantics (``flush``, ``soft_wrap``, lazy ``sys.stdout``). * `warn` / `err` are inherited from `EspLog` (canonical). * `warning` / `error` forward to them for backward compatibility. """ instance: EspLogBase | None = None # Tri-state compatibility override for collapsible stages, consumed by # `_stage_can_collapse`: # * ``None`` — auto: fallback to esp-pylib's detection # * ``True`` — force collapsing on (``compact`` mode). # * ``False`` — force collapsing off, set by the CLI in trace mode _smart_features: bool | None = None def _stage_can_collapse(self) -> bool: """Whether `stage` may rewind transient output. In ``auto`` mode (``_smart_features is None``) this defers to esp-pylib's default (normal verbosity + interactive stdout). ``compact`` (``True``) and ``--trace`` (``False``) force the decision instead. """ if self._smart_features is None: return super()._stage_can_collapse() # type: ignore[no-any-return] return self._smart_features and self._verbosity == Verbosity.NORMAL # ------------------------------------------------------------------ # Backward-compatible aliases # ------------------------------------------------------------------ def warning(self, message: str, suggestion: str | None = None) -> None: """Backward-compatible alias for `warn`.""" self.warn(message, suggestion=suggestion) def error(self, message: str, suggestion: str | None = None) -> None: """Backward-compatible alias for `err`.""" self.err(message, suggestion=suggestion) # ------------------------------------------------------------------ # Verbosity + custom logger swap # ------------------------------------------------------------------ def set_verbosity(self, verbosity): # type: ignore[override] """Accept esptool's CLI verbosity strings + esp-pylib levels. Historical values (``auto``, ``verbose``, ``silent``, ``compact``) map onto `Verbosity`. ``verbose`` is :data:`Verbosity.VERBOSE` so it enables `debug` and full progress-bar lines like other esp-pylib tools. ``compact`` / ``auto`` only adjust stage collapsing (``_smart_features``); ``silent`` is :data:`Verbosity.SILENT`. """ if isinstance(verbosity, str): key = verbosity.lower() if key in _VERBOSITY_LEVEL_MAP: super().set_verbosity(_VERBOSITY_LEVEL_MAP[key]) if key == "compact": self._smart_features = True elif key == "auto": # Defer to esp-pylib's TTY-based detection. self._smart_features = None return super().set_verbosity(verbosity) def set_logger(self, new_logger): # type: ignore[override] """Install a custom logger instance as the active singleton. Accepts a `TemplateLogger` (wrapped for esp-pylib) or any `EspLogBase` implementation. Assignment goes to ``EspLog.instance``, which the shared ``log`` proxy reads. """ if isinstance(new_logger, EspLogBase): instance: EspLogBase = new_logger elif isinstance(new_logger, TemplateLogger): instance = _LegacyLoggerAdapter(new_logger) else: raise TypeError( f"New logger must implement the TemplateLogger interface, " f"got {type(new_logger).__name__!r}" ) EspLog.instance = instance # ------------------------------------------------------------------ # Print — esptool-specific stdout semantics only # ------------------------------------------------------------------ def print(self, *args, **kwargs) -> None: # type: ignore[override] """Plain output with esptool's historical ``print`` semantics. ``soft_wrap=True`` is the default so Rich does not break long lines at the console width. A ``flush=True`` kwarg some callers pass is stripped here: Rich's ``Console.print`` already flushes the underlying stream once per call (the consoles are never used in a buffering context) and rejects an explicit ``flush`` argument. Flush is kept for compatibility with legacy code, to avoid breaking changes, it should not be used in new code. Following a reassigned ``sys.stdout`` (e.g. ``redirect_stdout``) and stage newline accounting are both handled by `EspLog.print`. """ kwargs.pop("flush", None) kwargs.setdefault("soft_wrap", True) super().print(*args, **kwargs) # Wire esptool's subclass into the shared singleton before anything imports # ``log`` (re-exported from esp-pylib; it delegates to ``EspLog.instance``). # # Only install when no logger has been set yet, so that importing esptool as a # library does not hijack a consumer tool's already-installed logger. When esptool # is the top-level application, nothing has touched ``log`` yet, so ``EspLog.instance`` # is ``None`` here and esptool gets its own logger as before. if EspLog.instance is None: EspLog.instance = EsptoolLogger() |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |