Base class for all cmd2 Rich consoles. This class handles the core logic for managing Rich behavior based on cmd2's global settings, such as ALLOW_STYLE and the application theme.
| 363 | |
| 364 | |
| 365 | class Cmd2BaseConsole(Console): |
| 366 | """Base class for all cmd2 Rich consoles. |
| 367 | |
| 368 | This class handles the core logic for managing Rich behavior based on |
| 369 | cmd2's global settings, such as ALLOW_STYLE and the application theme. |
| 370 | """ |
| 371 | |
| 372 | def __init__( |
| 373 | self, |
| 374 | *, |
| 375 | file: IO[str] | None = None, |
| 376 | **kwargs: Any, |
| 377 | ) -> None: |
| 378 | """Cmd2BaseConsole initializer. |
| 379 | |
| 380 | :param file: optional file object where the console should write to. |
| 381 | Defaults to sys.stdout. |
| 382 | :param kwargs: keyword arguments passed to the parent Console class. |
| 383 | :raises TypeError: if disallowed keyword argument is passed in. |
| 384 | """ |
| 385 | # These settings are controlled by the ALLOW_STYLE setting and cannot be overridden. |
| 386 | if "color_system" in kwargs: |
| 387 | raise TypeError("Passing 'color_system' is not allowed. Its behavior is controlled by the 'ALLOW_STYLE' setting.") |
| 388 | if "force_terminal" in kwargs: |
| 389 | raise TypeError( |
| 390 | "Passing 'force_terminal' is not allowed. Its behavior is controlled by the 'ALLOW_STYLE' setting." |
| 391 | ) |
| 392 | if "force_interactive" in kwargs: |
| 393 | raise TypeError( |
| 394 | "Passing 'force_interactive' is not allowed. Its behavior is controlled by the 'ALLOW_STYLE' setting." |
| 395 | ) |
| 396 | |
| 397 | # Don't allow a theme to be passed in, as it is controlled by get_theme() and set_theme(). |
| 398 | # Use cmd2.rich_utils.set_theme() to set the global theme or use a temporary |
| 399 | # theme with console.use_theme(). |
| 400 | if "theme" in kwargs: |
| 401 | raise TypeError("Passing 'theme' is not allowed. Its behavior is controlled by get_theme() and set_theme().") |
| 402 | |
| 403 | # Store the configuration key used by cmd2 to cache this console. |
| 404 | self._config_key = self._build_config_key(file=file, **kwargs) |
| 405 | |
| 406 | force_terminal: bool | None = None |
| 407 | force_interactive: bool | None = None |
| 408 | allow_style = False |
| 409 | |
| 410 | if ALLOW_STYLE == AllowStyle.ALWAYS: |
| 411 | force_terminal = True |
| 412 | allow_style = True |
| 413 | |
| 414 | # Turn off interactive mode if dest is not a terminal which supports it. |
| 415 | tmp_console = Console(file=file) |
| 416 | force_interactive = tmp_console.is_interactive |
| 417 | elif ALLOW_STYLE == AllowStyle.TERMINAL: |
| 418 | tmp_console = Console(file=file) |
| 419 | allow_style = tmp_console.is_terminal |
| 420 | elif ALLOW_STYLE == AllowStyle.NEVER: |
| 421 | force_terminal = False |
| 422 |
no outgoing calls
no test coverage detected
searching dependent graphs…