mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Relative string CSS paths
This commit is contained in:
@@ -44,5 +44,5 @@ class JustABox(App):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = JustABox(css_path="just_a_box.css", watch_css=True)
|
||||
app = JustABox(css_path="../darren/just_a_box.css", watch_css=True)
|
||||
app.run()
|
||||
|
||||
28
src/textual/_path.py
Normal file
28
src/textual/_path.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import inspect
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _make_path_object_relative(path: str, obj: object) -> Path:
|
||||
"""Given a path as a string convert it, if possible, to a Path that is relative to a given Python object.
|
||||
If the path string is already absolute, it will simply be converted to a Path object.
|
||||
Used, for example, to return the path of a CSS file relative to a Textual App instance.
|
||||
|
||||
Args:
|
||||
path (str): A path, as a string.
|
||||
obj (object): A Python object.
|
||||
|
||||
Returns:
|
||||
Path: A resolved Path object, relative to obj
|
||||
"""
|
||||
path = Path(path)
|
||||
|
||||
# If the path supplied by the user is absolute, we can use it directly
|
||||
if path.is_absolute():
|
||||
return path
|
||||
|
||||
# Otherwise (relative path), resolve it relative to obj...
|
||||
subclass_module = sys.modules[obj.__module__]
|
||||
subclass_path = Path(inspect.getfile(subclass_module))
|
||||
resolved_path = (subclass_path.parent / path).resolve()
|
||||
return resolved_path
|
||||
@@ -9,8 +9,9 @@ import sys
|
||||
import warnings
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
from datetime import datetime
|
||||
from pathlib import PurePath
|
||||
from pathlib import PurePath, Path
|
||||
from time import perf_counter
|
||||
from types import ModuleType
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
@@ -25,6 +26,7 @@ from typing import (
|
||||
from weakref import WeakSet, WeakValueDictionary
|
||||
|
||||
from ._ansi_sequences import SYNC_END, SYNC_START
|
||||
from ._path import _make_path_object_relative
|
||||
|
||||
if sys.version_info >= (3, 8):
|
||||
from typing import Literal
|
||||
@@ -147,7 +149,7 @@ class App(Generic[ReturnType], DOMNode):
|
||||
CSS = """
|
||||
App {
|
||||
background: $background;
|
||||
color: $text-background;
|
||||
color: $text-background;
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -225,7 +227,12 @@ class App(Generic[ReturnType], DOMNode):
|
||||
|
||||
self.stylesheet = Stylesheet(variables=self.get_css_variables())
|
||||
self._require_stylesheet_update: set[DOMNode] = set()
|
||||
self.css_path = css_path or self.CSS_PATH
|
||||
|
||||
css_path = css_path or self.CSS_PATH
|
||||
if isinstance(css_path, str):
|
||||
css_path = _make_path_object_relative(css_path, self) if css_path else None
|
||||
|
||||
self.css_path = css_path
|
||||
|
||||
self._registry: WeakSet[DOMNode] = WeakSet()
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from __future__ import annotations
|
||||
import os
|
||||
from pathlib import PurePath
|
||||
from typing import Callable
|
||||
@@ -12,7 +11,7 @@ from ._callback import invoke
|
||||
class FileMonitor:
|
||||
"""Monitors a file for changes and invokes a callback when it does."""
|
||||
|
||||
def __init__(self, path: str | PurePath, callback: Callable) -> None:
|
||||
def __init__(self, path: PurePath, callback: Callable) -> None:
|
||||
self.path = path
|
||||
self.callback = callback
|
||||
self._modified = self._get_modified()
|
||||
|
||||
Reference in New Issue
Block a user