mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Logging and experimenting for Windows
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from queue import Empty, Queue
|
from queue import Empty, Queue
|
||||||
|
from threading import RLock
|
||||||
from typing import ClassVar, Iterable, Iterator
|
from typing import ClassVar, Iterable, Iterator
|
||||||
|
|
||||||
from rich.style import Style
|
from rich.style import Style
|
||||||
@@ -15,6 +16,8 @@ from ..reactive import var
|
|||||||
from ..worker import Worker, get_current_worker
|
from ..worker import Worker, get_current_worker
|
||||||
from ._tree import TOGGLE_STYLE, Tree, TreeNode
|
from ._tree import TOGGLE_STYLE, Tree, TreeNode
|
||||||
|
|
||||||
|
read_dir = RLock()
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DirEntry:
|
class DirEntry:
|
||||||
@@ -239,6 +242,9 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
"""
|
"""
|
||||||
return paths
|
return paths
|
||||||
|
|
||||||
|
def _tlog(self, message: str) -> None:
|
||||||
|
self.app.call_from_thread(self.log.debug, f"{self.id} - {message}")
|
||||||
|
|
||||||
def _populate_node(self, node: TreeNode[DirEntry], content: Iterable[Path]) -> None:
|
def _populate_node(self, node: TreeNode[DirEntry], content: Iterable[Path]) -> None:
|
||||||
"""Populate the given tree node with the given directory content.
|
"""Populate the given tree node with the given directory content.
|
||||||
|
|
||||||
@@ -268,6 +274,7 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
if worker.is_cancelled:
|
if worker.is_cancelled:
|
||||||
break
|
break
|
||||||
yield entry
|
yield entry
|
||||||
|
self._tlog(f"Loaded entry {entry} from {location}")
|
||||||
|
|
||||||
def _load_directory(self, node: TreeNode[DirEntry], worker: Worker) -> None:
|
def _load_directory(self, node: TreeNode[DirEntry], worker: Worker) -> None:
|
||||||
"""Load the directory contents for a given node.
|
"""Load the directory contents for a given node.
|
||||||
@@ -277,14 +284,15 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
"""
|
"""
|
||||||
assert node.data is not None
|
assert node.data is not None
|
||||||
node.data.loaded = True
|
node.data.loaded = True
|
||||||
self.app.call_from_thread(
|
with read_dir:
|
||||||
self._populate_node,
|
self.app.call_from_thread(
|
||||||
node,
|
self._populate_node,
|
||||||
sorted(
|
node,
|
||||||
self.filter_paths(self._directory_content(node.data.path, worker)),
|
sorted(
|
||||||
key=lambda path: (not path.is_dir(), path.name.lower()),
|
self.filter_paths(self._directory_content(node.data.path, worker)),
|
||||||
),
|
key=lambda path: (not path.is_dir(), path.name.lower()),
|
||||||
)
|
),
|
||||||
|
)
|
||||||
|
|
||||||
_LOADER_INTERVAL: Final[float] = 0.2
|
_LOADER_INTERVAL: Final[float] = 0.2
|
||||||
"""How long the loader should block while waiting for queue content."""
|
"""How long the loader should block while waiting for queue content."""
|
||||||
@@ -292,12 +300,14 @@ class DirectoryTree(Tree[DirEntry]):
|
|||||||
@work(exclusive=True)
|
@work(exclusive=True)
|
||||||
def _loader(self) -> None:
|
def _loader(self) -> None:
|
||||||
"""Background loading queue processor."""
|
"""Background loading queue processor."""
|
||||||
|
self._tlog("_loader started")
|
||||||
worker = get_current_worker()
|
worker = get_current_worker()
|
||||||
while not worker.is_cancelled:
|
while not worker.is_cancelled:
|
||||||
try:
|
try:
|
||||||
self._load_directory(
|
next_node = self._to_load.get(timeout=self._LOADER_INTERVAL)
|
||||||
self._to_load.get(timeout=self._LOADER_INTERVAL), worker
|
self._tlog(f"Received {next_node} for loading")
|
||||||
)
|
self._load_directory(next_node, worker)
|
||||||
|
self._tlog(f"Loaded {next_node}")
|
||||||
except Empty:
|
except Empty:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user