docstrings

This commit is contained in:
Will McGugan
2022-08-24 16:25:51 +01:00
parent a50ffe896a
commit 7df1c123e9
4 changed files with 56 additions and 30 deletions

View File

@@ -132,7 +132,17 @@ class _NullFile:
@rich.repr.auto
class App(Generic[ReturnType], DOMNode):
"""The base class for Textual Applications"""
"""The base class for Textual Applications.
Args:
driver_class (Type[Driver] | None, optional): Driver class or ``None`` to auto-detect. Defaults to None.
log_path (str | PurePath, optional): Path to log file, or "" to disable. Defaults to "".
log_verbosity (int, optional): Log verbosity from 0-3. Defaults to 1.
title (str, optional): Default title of the application. Defaults to "Textual Application".
css_path (str | PurePath | None, optional): Path to CSS or ``None`` for no CSS file. Defaults to None.
watch_css (bool, optional): Watch CSS for changes. Defaults to False.
"""
CSS = """
App {
@@ -157,16 +167,6 @@ class App(Generic[ReturnType], DOMNode):
css_path: str | PurePath | None = None,
watch_css: bool = False,
):
"""Textual application base class
Args:
driver_class (Type[Driver] | None, optional): Driver class or ``None`` to auto-detect. Defaults to None.
log_path (str | PurePath, optional): Path to log file, or "" to disable. Defaults to "".
log_verbosity (int, optional): Log verbosity from 0-3. Defaults to 1.
title (str, optional): Default title of the application. Defaults to "Textual Application".
css_path (str | PurePath | None, optional): Path to CSS or ``None`` for no CSS file. Defaults to None.
watch_css (bool, optional): Watch CSS for changes. Defaults to False.
"""
# N.B. This must be done *before* we call the parent constructor, because MessagePump's
# constructor instantiates a `asyncio.PriorityQueue` and in Python versions older than 3.10
# this will create some first references to an asyncio loop.
@@ -255,22 +255,42 @@ class App(Generic[ReturnType], DOMNode):
@property
def devtools_enabled(self) -> bool:
"""Check if devtools are enabled."""
"""Check if devtools are enabled.
Returns:
bool: True if devtools are enabled.
"""
return "devtools" in self.features
@property
def debug(self) -> bool:
"""Check if debug mode is enabled."""
"""Check if debug mode is enabled.
Returns:
bool: True if debug mode is enabled.
"""
return "debug" in self.features
@property
def is_headless(self) -> bool:
"""Check if the app is running in 'headless' mode."""
"""Check if the app is running in 'headless' mode.
Returns:
bool: True if the app is in headless mode.
"""
return "headless" in self.features
@property
def screen_stack(self) -> list[Screen]:
"""Get a *copy* of the screen stack."""
"""Get a *copy* of the screen stack.
Returns:
list[Screen]: List of screens.
"""
return self._screen_stack.copy()
def exit(self, result: ReturnType | None = None) -> None:
@@ -284,7 +304,12 @@ class App(Generic[ReturnType], DOMNode):
@property
def focus_chain(self) -> list[Widget]:
"""Get widgets that may receive focus, in focus order."""
"""Get widgets that may receive focus, in focus order.
Returns:
list[Widget]: List of Widgets in focus order.
"""
widgets: list[Widget] = []
add_widget = widgets.append
root = self.screen

View File

@@ -10,15 +10,19 @@ from ._callback import invoke
@rich.repr.auto
class FileMonitor:
"""Monitors a file for changes and invokes a callback when it does."""
def __init__(self, path: str | PurePath, callback: Callable) -> None:
self.path = path
self.callback = callback
self._modified = self._get_modified()
def _get_modified(self) -> float:
"""Get the modified time for a file being watched."""
return os.stat(self.path).st_mtime
def check(self) -> bool:
"""Check the monitored file. Return True if it was changed."""
modified = self._get_modified()
changed = modified != self._modified
self._modified = modified

View File

@@ -11,7 +11,12 @@ from ._types import MessageTarget
@rich.repr.auto
class Message:
"""Base class for a message."""
"""Base class for a message.
Args:
sender (MessageTarget): The sender of the message / event.
"""
__slots__ = [
"sender",
@@ -30,12 +35,6 @@ class Message:
namespace: ClassVar[str] = "" # Namespace to disambiguate messages
def __init__(self, sender: MessageTarget) -> None:
"""
Args:
sender (MessageTarget): The sender of the message / event.
"""
self.sender = sender
self.name = camel_to_snake(self.__class__.__name__.replace("Message", ""))
self.time = _clock.get_time_no_wait()

View File

@@ -2,24 +2,22 @@ from __future__ import annotations
import sys
from rich.console import RenderableType
import rich.repr
from rich.console import RenderableType
from rich.style import Style
from . import events, messages, errors
from . import errors, events, messages
from ._callback import invoke
from .geometry import Offset, Region, Size
from ._compositor import Compositor, MapGeometry
from ._timer import Timer
from ._types import CallbackType
from .geometry import Offset, Region, Size
from .reactive import Reactive
from .renderables.blank import Blank
from ._timer import Timer
from .widget import Widget
if sys.version_info >= (3, 8):
from typing import Final, Callable, Awaitable
from typing import Final
else:
from typing_extensions import Final