This commit is contained in:
Will McGugan
2024-07-22 14:42:58 +01:00
parent b804b8e851
commit 144f4160a5
3 changed files with 57 additions and 1 deletions

View File

@@ -278,6 +278,12 @@ class _PrintCapture:
class App(Generic[ReturnType], DOMNode):
"""The base class for Textual Applications."""
CONFIG_PATH: str | None = None
"""Path to app config, or `None` for no app config."""
APP_ID: str | None = None
"""A unique identifier used in the config system, or `None` to use the name of the App sub-class."""
CSS: ClassVar[str] = ""
"""Inline CSS, useful for quick scripts. This is loaded after CSS_PATH,
and therefore takes priority in the event of a specificity clash."""

50
src/textual/config.py Normal file
View File

@@ -0,0 +1,50 @@
from __future__ import annotations
import tomllib
from os import PathLike
class Config:
def __init__(self, *paths: PathLike) -> None:
self.paths = paths
self._data: dict | None = None
self._attempted_read = False
def _read(self) -> dict:
configs: list[dict] = []
for path in self.paths:
try:
with open(path, "rb") as config_file:
configs.append(tomllib.load(config_file))
except IOError:
pass
config = configs[0]
for overlay_config in configs[1:]:
if isinstance(overlay_config, dict):
for key, value in overlay_config.items():
config[key] = value
return config
@property
def data(self) -> dict:
if not self._attempted_read:
self._attempted_read = True
self._data = self._read()
if self._data is None:
return {}
return self._data
def get(self, *keys: str, default: object = None) -> object:
data = self.data
for key in keys:
if key not in data:
return default
data = data[key]
return data
if __name__ == "__main__":
config = Config("config.toml")
from rich import print
print(config.data)

View File

@@ -834,7 +834,7 @@ class Widget(DOMNode):
A text object.
"""
text = (
Text.from_markup(text_content)
Text.from_markup(text_content, end="")
if isinstance(text_content, str)
else text_content
)