Skip to content

Commit 7185e29

Browse files
authored
Driver class environ (#2036)
* Driver class environment variable * comments
1 parent 2a63687 commit 7185e29

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3535
- Added `TabbedContent` widget https://github.com/Textualize/textual/pull/2059
3636
- Added `get_child_by_type` method to widgets / app https://github.com/Textualize/textual/pull/2059
3737
- Added `Widget.render_str` method https://github.com/Textualize/textual/pull/2059
38+
- Added TEXTUAL_DRIVER environment variable
3839

3940

4041
## [0.15.1] - 2023-03-14

src/textual/app.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import importlib
45
import inspect
56
import io
67
import os
@@ -47,7 +48,7 @@
4748
from rich.segment import Segment, Segments
4849
from rich.traceback import Traceback
4950

50-
from . import Logger, LogGroup, LogVerbosity, actions, events, log, messages
51+
from . import Logger, LogGroup, LogVerbosity, actions, constants, events, log, messages
5152
from ._animator import DEFAULT_EASING, Animatable, Animator, EasingFunction
5253
from ._ansi_sequences import SYNC_END, SYNC_START
5354
from ._asyncio import create_task
@@ -590,7 +591,24 @@ def get_driver_class(self) -> Type[Driver]:
590591
Returns:
591592
A Driver class which manages input and display.
592593
"""
594+
593595
driver_class: Type[Driver]
596+
597+
driver_import = constants.DRIVER
598+
if driver_import is not None:
599+
# The driver class is set from the environment
600+
# Syntax should be foo.bar.baz:MyDriver
601+
module_import, colon, driver_symbol = driver_import.partition(":")
602+
driver_module = importlib.import_module(module_import)
603+
driver_class = getattr(driver_module, driver_symbol)
604+
if not inspect.isclass(driver_class) or not issubclass(
605+
driver_class, Driver
606+
):
607+
raise RuntimeError(
608+
f"Unable to import {driver_import!r}; {driver_class!r} is not a Driver class "
609+
)
610+
return driver_class
611+
594612
if WINDOWS:
595613
from .drivers.windows_driver import WindowsDriver
596614

src/textual/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
"""
55

6+
from __future__ import annotations
7+
68
import os
79

810
from typing_extensions import Final
@@ -11,6 +13,8 @@
1113

1214
__all__ = ["BORDERS"]
1315

16+
get_environ = os.environ.get
17+
1418

1519
def get_environ_bool(name: str) -> bool:
1620
"""Check an environment variable switch.
@@ -28,3 +32,5 @@ def get_environ_bool(name: str) -> bool:
2832
BORDERS = list(BORDER_CHARS)
2933

3034
DEBUG: Final[bool] = get_environ_bool("TEXTUAL_DEBUG")
35+
36+
DRIVER: Final[str | None] = get_environ("TEXTUAL_DRIVER", None)

0 commit comments

Comments
 (0)