refactor: read offline from the screen state

The rows asked state.offline while the plan, the estimate and the
result read the environment, kept in step only by the session loop.
tui_offline_only(state) is the one question; the variable only carries
the choice to the runs and the wording.
This commit is contained in:
Abdessamad Derraz committed 2026-11-22 00:58:06 +01:00
1 parent af34c81d71
commit 9f3fc2e0c1
10 files changed
+63 -47

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@
# and Invoke-WebRequest throws on a cut transfer before the hash is read:
# the site's build rewrites this line there (scripts/sitebootstrap.py).
$mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
$defaultInstallSha256 = "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c"
$defaultInstallSha256 = "1c6286c90deb064affd0506cfa7b0c0c4ff19ad1555518f256ac98af86e188e2"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
$maximumInstallerBytes = 8MB
+15 -13
View File
@@ -14196,6 +14196,13 @@ class TuiState:
return self.stack[-1]
def tui_offline_only(state: TuiState) -> bool:
"""Without the internet and without a pack: what is not here cannot
come. The screens ask state.offline, never the environment, which only
carries the choice to the runs."""
return state.offline and LOCAL.pack is None
# --- installer/setups.py -----------------------------------------------------
def _card_word(system: tuple[str, Path, str]) -> str:
@@ -24536,7 +24543,7 @@ def _tui_estimating(state: TuiState) -> bool:
check itself."""
if not (_choice_line(state) and state.systems and state.check is not None):
return False
if _offline_only():
if tui_offline_only(state):
# Offline the estimate is of the ticks alone: a folder to copy from
# is counted by the check it starts.
def ticked(flags: list) -> list:
@@ -24867,7 +24874,7 @@ def _ticked_estimate(state: TuiState) -> tuple[int, int]:
data files in them, or offline what the folders copied from hold: the
plan's headline and its Install row both say it. One system ticked,
the headline said 82.2 MB and Install 6.6 GB, the whole list's size."""
if _offline_only():
if tui_offline_only(state):
held = _ticked_held(state)
return len(held), sum(event["size"] for event in held)
chosen = _ticked_files(state)
@@ -25108,11 +25115,6 @@ def _core_screen_rows(state: TuiState) -> list[TuiRow]:
return rows + _CHECKLIST_END
def _tui_offline_only(state: TuiState) -> bool:
"""Without the internet and without a pack: what is not here cannot come."""
return state.offline and LOCAL.pack is None
def _confirm_screen_rows(state: TuiState) -> list[TuiRow]:
"""The rows of the confirm screen."""
if state.running or state.dry is None:
@@ -25121,7 +25123,7 @@ def _confirm_screen_rows(state: TuiState) -> list[TuiRow]:
# Counted again, every file is in place: Install wrote nothing
# and said Done.
return [TuiRow("close", "Close"), TuiRow("back", "Back")]
if _tui_offline_only(state) and not _dry_copies(state):
if tui_offline_only(state) and not _dry_copies(state):
# The plan's rule: Install would only fail every file.
return [TuiRow("back", "Back")]
return [TuiRow("install", "Install"), TuiRow("back", "Back")]
@@ -25196,7 +25198,7 @@ def _plan_rows(state: TuiState) -> list[TuiRow]:
label = (f"{TUI_INSTALL_ROW} {format_size(size)}" if size >= CONFIRM_FROM_BYTES
else TUI_INSTALL_ROW)
rows = [TuiRow("install", label)]
if _tui_offline_only(state) and not _plan_copies(state, check):
if tui_offline_only(state) and not _plan_copies(state, check):
# Nothing here to copy, and the network refused: Install would
# only fail every file, in this folder or another.
rows = []
@@ -26584,7 +26586,7 @@ def _plan_figures(check: dict, state: TuiState) -> _PlanFigures:
fetch_bytes = fetched - (check.get("data_bytes", 0) if data else 0)
elsewhere = (_tui_found_elsewhere(state, set(copied) if dry else None)
if check is state.check or dry else {})
offline = _offline_only()
offline = tui_offline_only(state)
# The data files are files to fetch like the others: one count and one
# size, the kinds apart on the line under the folder. "8358 files and
# 2636 data files" read as two kinds of one thing.
@@ -26906,7 +26908,7 @@ def _estimated_head(state: TuiState, check: dict, lines: list, data: int) -> lis
count, size = _ticked_estimate(state)
lines[0] = _styled(f"About {_plural(count, 'file')}, {format_size(size)}"
f", for {len(state.ticked)} of {len(state.systems)} systems", "title")
if _offline_only():
if tui_offline_only(state):
# Offline, what the folders copied from hold of the systems
# ticked: the plan counted the whole list beside "For your 1 of
# 11 systems".
@@ -26991,7 +26993,7 @@ def _plan_kinds(state: TuiState, check: dict, estimating: bool, data: int,
# above it: inserted second, it pushed the folder off a 40x12 plan.
kinds_of = (_ticked_files(state) if estimating else
[event for event in state.files if event["state"] != "present"])
found = (_tui_found_elsewhere(state) if not estimating and _offline_only()
found = (_tui_found_elsewhere(state) if not estimating and tui_offline_only(state)
and state.check is check else {})
if found:
# Some were found in a folder: the kinds split those the headline
@@ -27312,7 +27314,7 @@ def _outcome_counts(state: TuiState) -> _OutcomeCounts:
offline_failed = [failure for failure in failed if _offline_failure(failure["reason"])]
failed = [failure for failure in failed if not _offline_failure(failure["reason"])]
left = len(offline_failed) + (max(0, summary.get("pending", 0) - len(failed))
if _offline_only() and not offline_failed else 0)
if tui_offline_only(state) and not offline_failed else 0)
up_to_date = summary.get("up_to_date", 0)
total = (state.download or {}).get("files", 0)
space = "" if summary else state.full_drive
+1 -1
View File
@@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins
# curl or wget stop on a cut transfer before the hash is read: the site's
# build rewrites this line there (scripts/sitebootstrap.py).
MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
DEFAULT_INSTALL_SHA256="fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c"
DEFAULT_INSTALL_SHA256="1c6286c90deb064affd0506cfa7b0c0c4ff19ad1555518f256ac98af86e188e2"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
MAX_INSTALLER_BYTES=8388608
+8 -8
View File
@@ -27,8 +27,8 @@
{
"dest": "roms/ports/retrobios/install.py",
"repo_path": "install.py",
"size": 1471771,
"sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c",
"size": 1471921,
"sha256": "1c6286c90deb064affd0506cfa7b0c0c4ff19ad1555518f256ac98af86e188e2",
"executable": false
},
{
@@ -71,8 +71,8 @@
"muos": {
"archive": {
"dest": "ARCHIVE/RetroBIOS.muxapp",
"size": 1525396,
"sha256": "13a0aa4bbf7c17d7dc171568b060722a095b0010cfe6ee4ddd827c838b0d7bc2"
"size": 1525546,
"sha256": "e1ae1df236e8dac0607ab7ee20c8ece56536b03fae23b9a25a05f65ae5de0f31"
},
"members": [
{
@@ -92,8 +92,8 @@
{
"name": "RetroBIOS/install.py",
"repo_path": "install.py",
"size": 1471771,
"sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c",
"size": 1471921,
"sha256": "1c6286c90deb064affd0506cfa7b0c0c4ff19ad1555518f256ac98af86e188e2",
"executable": false
},
{
@@ -166,8 +166,8 @@
{
"dest": ".config/modules/retrobios/install.py",
"repo_path": "install.py",
"size": 1471771,
"sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c",
"size": 1471921,
"sha256": "1c6286c90deb064affd0506cfa7b0c0c4ff19ad1555518f256ac98af86e188e2",
"executable": false
},
{
+4 -3
View File
@@ -15,7 +15,7 @@ from installer.frontend_folders import save_directory_for, save_where_words
from installer.games import GAMES_REASONS
from installer.manifest import OTHER_SYSTEMS, entry_copies
from installer.narrowing import narrow
from installer.offline import LOCAL, _offline_only
from installer.offline import LOCAL
from installer.pack_source import pack_holds_trees, pack_lacks
from installer.paths import SavePlace, spelled
from installer.plan_words import NO_CORE_FILES, _core_sized, emptied_words
@@ -26,6 +26,7 @@ from installer.tally import tally
from installer.text import _names_and, _plural, agree
from installer.tui_commands import _core_base, _tui_system_flags, _tui_systems_chosen, tui_flags, tui_run_files
from installer.tui_paint import _detail, _short_path
from installer.tui_state import tui_offline_only
if TYPE_CHECKING:
from installer.tally import Tally
@@ -219,7 +220,7 @@ def _tui_estimating(state: TuiState) -> bool:
check itself."""
if not (_choice_line(state) and state.systems and state.check is not None):
return False
if _offline_only():
if tui_offline_only(state):
# Offline the estimate is of the ticks alone: a folder to copy from
# is counted by the check it starts.
def ticked(flags: list) -> list:
@@ -550,7 +551,7 @@ def _ticked_estimate(state: TuiState) -> tuple[int, int]:
data files in them, or offline what the folders copied from hold: the
plan's headline and its Install row both say it. One system ticked,
the headline said 82.2 MB and Install 6.6 GB, the whole list's size."""
if _offline_only():
if tui_offline_only(state):
held = _ticked_held(state)
return len(held), sum(event["size"] for event in held)
chosen = _ticked_files(state)
+3 -2
View File
@@ -11,7 +11,7 @@ from installer.from_folders import sources_line
from installer.handheld_app import NO_PYTHON_CARDS, app_lines, no_python_words
from installer.host_fs import NOT_WRITABLE
from installer.invocation import Invocation
from installer.offline import LOCAL, _offline_only, offline_named
from installer.offline import LOCAL, offline_named
from installer.outcome import (
EVERYTHING_IN_PLACE,
NEXT_ON_CARD,
@@ -33,6 +33,7 @@ from installer.tui_commands import _removing_misplaced, _tui_card, _tui_installe
from installer.tui_paint import _FOLDER_LABEL, _command_line, _detail, _details, _short_path, _styled
from installer.tui_plan_view import _commands, _saves_lines, _unavailable_brief, _unavailable_lines, _unplaced_lines
from installer.tui_routes import TUI_PAD_ROUTES
from installer.tui_state import tui_offline_only
if TYPE_CHECKING:
from installer.tui_state import TuiState
@@ -97,7 +98,7 @@ def _outcome_counts(state: TuiState) -> _OutcomeCounts:
offline_failed = [failure for failure in failed if _offline_failure(failure["reason"])]
failed = [failure for failure in failed if not _offline_failure(failure["reason"])]
left = len(offline_failed) + (max(0, summary.get("pending", 0) - len(failed))
if _offline_only() and not offline_failed else 0)
if tui_offline_only(state) and not offline_failed else 0)
up_to_date = summary.get("up_to_date", 0)
total = (state.download or {}).get("files", 0)
space = "" if summary else state.full_drive
+5 -4
View File
@@ -9,7 +9,7 @@ from installer.from_folders import MAX_SCANNED_FILES
from installer.frontend_folders import save_where_words
from installer.install_record import record_changes, record_says
from installer.manifest import GAME_DATA, OTHER_SYSTEMS
from installer.offline import LOCAL, _offline_only, offline_named, offline_undo
from installer.offline import LOCAL, offline_named, offline_undo
from installer.outcome import EVERYTHING_IN_PLACE, _offline_remedy, write_remedy
from installer.pack_source import pack_lacks_words
from installer.paths import shown_path
@@ -57,6 +57,7 @@ from installer.tui_frame import _compose, _text_lines
from installer.tui_paint import _FOLDER_LABEL, _command_line, _detail, _details, _is_detail, _short_path, _styled
from installer.tui_routes import TUI_PAD_ROUTES
from installer.tui_rows import _plan_rows, tui_rows
from installer.tui_state import tui_offline_only
if TYPE_CHECKING:
from installer.tui_paint import TuiLine
@@ -116,7 +117,7 @@ def _plan_figures(check: dict, state: TuiState) -> _PlanFigures:
fetch_bytes = fetched - (check.get("data_bytes", 0) if data else 0)
elsewhere = (_tui_found_elsewhere(state, set(copied) if dry else None)
if check is state.check or dry else {})
offline = _offline_only()
offline = tui_offline_only(state)
# The data files are files to fetch like the others: one count and one
# size, the kinds apart on the line under the folder. "8358 files and
# 2636 data files" read as two kinds of one thing.
@@ -438,7 +439,7 @@ def _estimated_head(state: TuiState, check: dict, lines: list, data: int) -> lis
count, size = _ticked_estimate(state)
lines[0] = _styled(f"About {_plural(count, 'file')}, {format_size(size)}"
f", for {len(state.ticked)} of {len(state.systems)} systems", "title")
if _offline_only():
if tui_offline_only(state):
# Offline, what the folders copied from hold of the systems
# ticked: the plan counted the whole list beside "For your 1 of
# 11 systems".
@@ -523,7 +524,7 @@ def _plan_kinds(state: TuiState, check: dict, estimating: bool, data: int,
# above it: inserted second, it pushed the folder off a 40x12 plan.
kinds_of = (_ticked_files(state) if estimating else
[event for event in state.files if event["state"] != "present"])
found = (_tui_found_elsewhere(state) if not estimating and _offline_only()
found = (_tui_found_elsewhere(state) if not estimating and tui_offline_only(state)
and state.check is check else {})
if found:
# Some were found in a folder: the kinds split those the headline
+3 -8
View File
@@ -74,7 +74,7 @@ from installer.tui_commands import (
)
from installer.tui_folder_scan import FolderScan
from installer.tui_paint import _short_path
from installer.tui_state import TUI_MAX_WIDTH, TuiRow, _matches
from installer.tui_state import TUI_MAX_WIDTH, TuiRow, _matches, tui_offline_only
if TYPE_CHECKING:
from installer.tui_state import TuiState
@@ -242,11 +242,6 @@ def _core_screen_rows(state: TuiState) -> list[TuiRow]:
return rows + _CHECKLIST_END
def _tui_offline_only(state: TuiState) -> bool:
"""Without the internet and without a pack: what is not here cannot come."""
return state.offline and LOCAL.pack is None
def _confirm_screen_rows(state: TuiState) -> list[TuiRow]:
"""The rows of the confirm screen."""
if state.running or state.dry is None:
@@ -255,7 +250,7 @@ def _confirm_screen_rows(state: TuiState) -> list[TuiRow]:
# Counted again, every file is in place: Install wrote nothing
# and said Done.
return [TuiRow("close", "Close"), TuiRow("back", "Back")]
if _tui_offline_only(state) and not _dry_copies(state):
if tui_offline_only(state) and not _dry_copies(state):
# The plan's rule: Install would only fail every file.
return [TuiRow("back", "Back")]
return [TuiRow("install", "Install"), TuiRow("back", "Back")]
@@ -330,7 +325,7 @@ def _plan_rows(state: TuiState) -> list[TuiRow]:
label = (f"{TUI_INSTALL_ROW} {format_size(size)}" if size >= CONFIRM_FROM_BYTES
else TUI_INSTALL_ROW)
rows = [TuiRow("install", label)]
if _tui_offline_only(state) and not _plan_copies(state, check):
if tui_offline_only(state) and not _plan_copies(state, check):
# Nothing here to copy, and the network refused: Install would
# only fail every file, in this folder or another.
rows = []
+8
View File
@@ -7,6 +7,7 @@ from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple
from installer.invocation import offline_requested
from installer.offline import LOCAL
if TYPE_CHECKING:
from installer.handheld_app import CardApp
@@ -374,3 +375,10 @@ class TuiState:
@property
def screen(self) -> str:
return self.stack[-1]
def tui_offline_only(state: TuiState) -> bool:
"""Without the internet and without a pack: what is not here cannot
come. The screens ask state.offline, never the environment, which only
carries the choice to the runs."""
return state.offline and LOCAL.pack is None
+15 -7
View File
@@ -2988,8 +2988,7 @@ class CardsSpaceAndConfirm(unittest.TestCase):
only fail every file."""
state = checked_state({"free": 1024, "writable": False, "cores": []})
state.offline = True
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
labels = [row.label for row in install.tui_rows(state)]
labels = [row.label for row in install.tui_rows(state)]
self.assertEqual(labels[0], "Choose another folder")
self.assertNotIn("Install anyway", labels)
self.assertFalse(any(label.startswith("Install") for label in labels), labels)
@@ -7062,6 +7061,7 @@ class ConfirmRefusalsAndCardRows(unittest.TestCase):
""""3 downloaded" under "3 need the internet";
the data files read as if on hand."""
state = checked_state()
state.offline = True
state.found = [{"event": "found", "platform": "retroarch", "dest": FILES[0]["dest"],
"folder": "/media/old", "path": "/media/old/x", "member": "",
"reason": ""}]
@@ -7489,6 +7489,7 @@ class WhatEachScreenOffers(unittest.TestCase):
@in_full
def test_offline_plan_names_the_folder_already_copied_from(self):
state = checked_state()
state.offline = True
state.from_folders = [self.root / "from1"]
state.found = [found("gba_bios.bin", str(self.root / "from1"))]
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
@@ -9684,8 +9685,7 @@ class PacksWordingAndDocs(unittest.TestCase):
state.stack.append("confirm")
state.dry = dict(CHECK)
state.offline = True
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
self.assertIn("Nothing to install", install.tui_view(state, 80, 24)[0])
self.assertIn("Nothing to install", install.tui_view(state, 80, 24)[0])
def test_another_folder_for_a_card_setup_asks_for_its_card(self):
"""A full Onion card offered another folder for
@@ -9990,6 +9990,7 @@ class CardFoldersAndNarrowBars(unittest.TestCase):
old = self.root / "USBSTICK"
old.mkdir()
state = checked_state()
state.offline = True
state.from_folders = [old]
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: install.OFFLINE_FLAG_VALUE}):
lines = [str(line) for line in install._plan_lines(state.check, state.dest,
@@ -10769,9 +10770,9 @@ class CardNamesAndHelp(_CardFixtures, unittest.TestCase):
state = _installed_state(Path("/x/system"))
state.summary.update(installed=6, pending=16, failed=[])
state.from_folders = [Path("/media/me/OLD")]
with mock.patch.object(install, "_offline_only", return_value=True), \
mock.patch.object(install, "_tui_sources",
return_value="6 files copied from OLD, 16 still missing."):
state.offline = True
with mock.patch.object(install, "_tui_sources",
return_value="6 files copied from OLD, 16 still missing."):
_title, lines = install.tui_outcome(state)
self.assertEqual(lines[0], "6 files copied from OLD, 16 still missing.")
self.assertIn("They are not in", lines[1])
@@ -11998,6 +11999,7 @@ class TapsGameDataAndExitCodes(_CardFixtures, unittest.TestCase):
old.finished = [("scph5500.bin", True, ""), ("gba_bios.bin", True, "")]
old.local_dests = {"scph5500.bin": ("/media/OLD", ""), "gba_bios.bin": ("/media/OLD", "")}
old.summary = {**old.summary, "installed": 2, "pending": 3}
old.offline = True
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
_title, lines = install.tui_outcome(old)
self.assertIn("Installed: 2 BIOS files.", lines)
@@ -12523,6 +12525,7 @@ class OneChoicePerRun(unittest.TestCase):
def test_the_old_card_result_splits_what_is_missing(self):
""""16 still missing" where 6 were game data."""
state = _game_state()
state.offline = True
state.from_folders = [Path("/media/OLD")]
state.local_dests = {"gba_bios.bin": ("/media/OLD", "")}
state.finished = [("gba_bios.bin", True, "")]
@@ -13201,6 +13204,7 @@ class ChoiceStateInBothModes(unittest.TestCase):
"""Offline, one system ticked, the plan counted
every file of the list beside "For your 1 of 3 systems"."""
state = checked_state()
state.offline = True
folder = str(self.root / "from1")
state.from_folders = [self.root / "from1"]
state.found = [found("gba_bios.bin", folder)]
@@ -14195,6 +14199,7 @@ class TheFromPlanSaysItsKinds(unittest.TestCase):
def test_the_kinds_and_the_definition_stay(self):
audit = OneChoicePerRun()
state = audit.game_plan()
state.offline = True
folder = "/media/moi/packx"
state.from_folders = [Path(folder)]
state.found = [found(event["dest"], folder) for event in state.files
@@ -14461,6 +14466,7 @@ class ProvenCardsAndCounts(unittest.TestCase):
self.assertEqual(install._plural(1, "wrong copy"), "1 wrong copy")
self.assertEqual(install._plural(2, "day"), "2 days")
state = checked_state()
state.offline = True
folder = "/media/OLD"
state.from_folders = [Path(folder)]
state.found = [found(dest, folder) for dest in ("scph5500.bin", "pcsx/extra.bin",
@@ -15205,6 +15211,7 @@ class CoresListAndFolderFailures(unittest.TestCase):
"""Offline, the plan said a wrong copy no folder
holds "is replaced"."""
state = checked_state()
state.offline = True
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
text = self.text(state, 100, 30)
self.assertIn("1 wrong copy", text)
@@ -15268,6 +15275,7 @@ class CoresListAndFolderFailures(unittest.TestCase):
def test_offline_choice_of_systems_is_never_about_0_files(self):
""""About 0 files, 0 B, for 10 of 11 systems"."""
state = checked_state()
state.offline = True
state.ticked = {"sony-playstation"}
with mock.patch.dict(os.environ, {install.OFFLINE_VARIABLE: "1"}):
text = self.text(state, 100, 30)