Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Config system #4787

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
18 changes: 3 additions & 15 deletions src/textual/css/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,9 @@ def only_one(
the_one: ExpectType | QueryType = (
self.first(expect_type) if expect_type is not None else self.first()
)
try:
# Now see if we can access a subsequent item in the nodes. There
# should *not* be anything there, so we *should* get an
# IndexError. We *could* have just checked the length of the
# query, but the idea here is to do the check as cheaply as
# possible. "There can be only one!" -- Kurgan et al.
_ = self.nodes[1]
raise TooManyMatches(
"Call to only_one resulted in more than one matched node"
)
except IndexError:
# The IndexError was got, that's a good thing in this case. So
# we return what we found.
pass
return the_one
if len(self.nodes) == 1:
return the_one
raise TooManyMatches("Call to only_one resulted in more than one matched node")

if TYPE_CHECKING:

Expand Down
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
Loading