Skip to content

Commit ea62136

Browse files
committed
internal: read resource according to spec
Use the `importlib.resource` api for reading resources.
1 parent 284ff14 commit ea62136

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/isd/isd.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from enum import Enum, StrEnum, auto
3030
from functools import partial
3131
from itertools import chain, repeat
32+
from importlib.resources import as_file, files, as_file
3233
from pathlib import Path
3334
from textwrap import dedent, indent
3435
from typing import (
@@ -87,6 +88,25 @@
8788

8889
from . import __version__
8990

91+
CSS_RESOURCE = files(__name__).joinpath("dom.tcss")
92+
93+
with as_file(CSS_RESOURCE) as f:
94+
_CSS_RESOURCE_PATH = f
95+
96+
if _CSS_RESOURCE_PATH.exists():
97+
# package resources are persisted on the file system
98+
CSS_RESOURCE_PATH = _CSS_RESOURCE_PATH
99+
else:
100+
# this means that the resource is dynamically extracted
101+
# and not read as-is from the unpacked wheel.
102+
# So extract it again, copy its contents
103+
_tmp_file = tempfile.NamedTemporaryFile(
104+
"w", prefix="isd_", suffix=".tcss", delete=False
105+
)
106+
_tmp_file.write(CSS_RESOURCE.read_text())
107+
CSS_RESOURCE_PATH = Path(_tmp_file.name)
108+
del _tmp_file
109+
90110
ToggleButton.BUTTON_LEFT = ""
91111
ToggleButton.BUTTON_INNER = "▐" # "▌" # "█"
92112
ToggleButton.BUTTON_RIGHT = ""
@@ -1552,9 +1572,9 @@ def update_keybindings(self) -> None:
15521572
"toggle_mode",
15531573
]
15541574
model_fields = MainKeybindings.model_fields
1555-
assert all(
1556-
[show_field in model_fields for show_field in show_fields]
1557-
), "Forgot to update show_field"
1575+
assert all([show_field in model_fields for show_field in show_fields]), (
1576+
"Forgot to update show_field"
1577+
)
15581578
for action, field in model_fields.items():
15591579
# name of keybinding == action name and
15601580
# description is used for binding description
@@ -2172,7 +2192,8 @@ class InteractiveSystemd(App, inherit_bindings=False):
21722192
"""
21732193

21742194
TITLE = "isd"
2175-
CSS_PATH = "dom.tcss"
2195+
# CSS_PATH = "dom.tcss"
2196+
CSS_PATH = CSS_RESOURCE_PATH
21762197
COMMAND_PALETTE_BINDING = ensure_reserved("ctrl+p")
21772198
NOTIFICATION_TIMEOUT = 5
21782199
# If the modal is dismissed, this App would be focused by default

0 commit comments

Comments
 (0)