Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 22, 2024
1 parent b804b8e commit 144f416
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ def fileno(self) -> int:
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."""
Expand Down
50 changes: 50 additions & 0 deletions src/textual/config.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def render_str(self, text_content: str | Text) -> Text:
A text object.
"""
text = (
Text.from_markup(text_content)
Text.from_markup(text_content, end="")
if isinstance(text_content, str)
else text_content
)
Expand Down

0 comments on commit 144f416

Please sign in to comment.