Skip to content

Commit

Permalink
Threaded delivery when not on web
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Aug 20, 2024
1 parent bd7113c commit 3dba6b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/textual/driver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import asyncio
import shutil
import threading
from abc import ABC, abstractmethod
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -229,10 +229,25 @@ def deliver_binary(
set the `Content-Type` header in the HTTP response.
"""
if isinstance(binary, BinaryIO):
with open(save_path, "wb") as destination_file:
shutil.copyfileobj(binary, destination_file)
else: # isinstance(binary, TextIO):
with open(save_path, "w", encoding=encoding) as destination_file:
shutil.copyfileobj(binary, destination_file)

binary.close()
mode = "wb"
else:
mode = "w"

def save_file_thread():
with open(save_path, mode) as destination_file:
read = binary.read
write = destination_file.write
while True:
data = read(1024)
if not data:
break
write(data)
binary.close()
self._app.call_from_thread(self._delivery_complete, save_path=save_path)

thread = threading.Thread(target=save_file_thread)
thread.start()

def _delivery_complete(self, save_path: Path) -> None:
"""Called when a file has been delivered."""
self._app.post_message(events.DeliveryComplete(save_path=save_path))
9 changes: 9 additions & 0 deletions src/textual/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Type, TypeVar

import rich.repr
Expand Down Expand Up @@ -732,3 +733,11 @@ def __init__(self, text: str, stderr: bool = False) -> None:
def __rich_repr__(self) -> rich.repr.Result:
yield self.text
yield self.stderr


@dataclass
class DeliveryComplete(Event, bubble=False):
"""Sent to App when a file has been delivered."""

save_path: Path
"""The save_path to the file that was delivered."""

0 comments on commit 3dba6b9

Please sign in to comment.