Files
dssc/vendor/textual/_import_app.py
T
lucioandClaude Opus 5.5 60532a4043 Rebuild the UI on vendored Textual; make the checkout self-contained
The internal servers have no access to outside package repositories and
run Python 3.8/3.9, so the UI moves from hand-drawn curses to Textual
6.2.1 (the last release supporting 3.8.1), vendored with its pure-Python
dependencies in vendor/. The launcher puts vendor/ first on sys.path, so a
plain checkout runs with no pip and no network.

- dsflow/: the single script split into a package (pure moves of the
  backend sections); action rules moved to ops.py, free of Textual
- dsflow/ui/: tabs, detail pane, live sanity-run panel, log viewer,
  modal dialogs, filter, themes, mouse support
- tests/test_ui.py: headless UI tests through Textual's pilot
- tools/vendor.py (refresh vendor/), tools/screenshots.py (README images)
- README, CLAUDE.md, TODO and the feature doc updated for the new layout,
  the Python 3.8.1 floor and the vendoring rules

Verified: 11 tests green on Python 3.12 and 3.8.20; the shipped file set
runs under `python -I -S` on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-24 03:24:21 -07:00

127 lines
3.5 KiB
Python
Vendored

from __future__ import annotations
import os
import runpy
import shlex
import sys
from pathlib import Path
from typing import TYPE_CHECKING, cast
if TYPE_CHECKING:
from textual.app import App
class AppFail(Exception):
pass
def shebang_python(candidate: Path) -> bool:
"""Does the given file look like it's run with Python?
Args:
candidate: The candidate file to check.
Returns:
``True`` if it looks to #! python, ``False`` if not.
"""
try:
with candidate.open("rb") as source:
first_line = source.readline()
except IOError:
return False
return first_line.startswith(b"#!") and b"python" in first_line
def import_app(import_name: str) -> App:
"""Import an app from a path or import name.
Args:
import_name: A name to import, such as `foo.bar`, or a path ending with .py.
Raises:
AppFail: If the app could not be found for any reason.
Returns:
A Textual application
"""
import importlib
import inspect
from textual.app import WINDOWS, App
import_name, *argv = shlex.split(import_name, posix=not WINDOWS)
drive, import_name = os.path.splitdrive(import_name)
lib, _colon, name = import_name.partition(":")
if drive:
lib = os.path.join(drive, os.sep, lib)
if lib.endswith(".py") or shebang_python(Path(lib)):
path = os.path.abspath(lib)
sys.path.append(str(Path(path).parent))
try:
global_vars = runpy.run_path(path, {})
except Exception as error:
raise AppFail(str(error))
sys.argv[:] = [path, *argv]
if name:
# User has given a name, use that
try:
app = global_vars[name]
except KeyError:
raise AppFail(f"App {name!r} not found in {lib!r}")
else:
# User has not given a name
if "app" in global_vars:
# App exists, lets use that
try:
app = global_vars["app"]
except KeyError:
raise AppFail(f"App {name!r} not found in {lib!r}")
else:
# Find an App class or instance that is *not* the base class
apps = [
value
for value in global_vars.values()
if (
isinstance(value, App)
or (inspect.isclass(value) and issubclass(value, App))
and value is not App
)
]
if not apps:
raise AppFail(
f'Unable to find app in {lib!r}, try specifying app with "foo.py:app"'
)
if len(apps) > 1:
raise AppFail(
f'Multiple apps found {lib!r}, try specifying app with "foo.py:app"'
)
app = apps[0]
app._BASE_PATH = path
else:
# Assuming the user wants to import the file
sys.path.append("")
try:
module = importlib.import_module(lib)
except ImportError as error:
raise AppFail(str(error))
find_app = name or "app"
try:
app = getattr(module, find_app or "app")
except AttributeError:
raise AppFail(f"Unable to find {find_app!r} in {module!r}")
sys.argv[:] = [import_name, *argv]
if inspect.isclass(app) and issubclass(app, App):
app = app()
return cast(App, app)