Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/textual_dev/tools/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
import subprocess
import sys
from string import Template
from typing import NoReturn, Sequence
from typing import Sequence

WINDOWS = platform.system() == "Windows"

EXEC_SCRIPT = Template(
"""\
from textual.app import App
try:
from $MODULE import $APP as app;
from $MODULE import $APP as app
except ImportError:
raise SystemExit("Unable to import '$APP' from module '$MODULE'") from None
import sys, traceback
traceback.print_exc()
print(file=sys.stderr)
msg = "Unable to import '$APP' from module '$MODULE' (see traceback above)"
raise SystemExit(msg) from None

if isinstance(app, App):
# If we imported an app, run it
Expand All @@ -36,10 +40,6 @@
"""A template script to import and run an app."""


class ExecImportError(Exception):
"""Raised if a Python import is invalid."""


def run_app(
import_name: str, args: Sequence[str], environment: dict[str, str] | None = None
) -> None:
Expand All @@ -60,13 +60,9 @@ def run_app(
if _is_python_path(import_name):
# If it is a Python path we can exec it now
exec_python([import_name, *args], app_environment)

else:
# Otherwise it is assumed to be a Python import
try:
exec_import(import_name, args, app_environment)
except (SyntaxError, ExecImportError):
print(f"Unable to import Textual app from {import_name!r}")
exec_import(import_name, args, app_environment)


def _is_python_path(path: str) -> bool:
Expand Down