Skip to content

Commit

Permalink
Merge pull request #1802 from ogayot/rich-mode-s390x
Browse files Browse the repository at this point in the history
ui: have a distinct state file for rich mode over serial
  • Loading branch information
ogayot authored Sep 25, 2023
2 parents 333e4d9 + c95261e commit c9f3e25
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 9 deletions.
76 changes: 76 additions & 0 deletions subiquitycore/tests/test_tui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2023 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import json
import pathlib
from unittest.mock import Mock, patch

from subiquitycore.tests import SubiTestCase
from subiquitycore.tui import TuiApplication


class TestTuiApplication(SubiTestCase):
def setUp(self):
with patch("subiquitycore.tui.Application.__init__", return_value=None):
opts = Mock()
opts.answers = None
opts.dry_run = True
self.tui = TuiApplication(opts)
# Usually, the below are assigned by Application.__init__()
self.tui.opts = opts
self.tui.state_dir = self.tmp_dir()

def test_get_initial_rich_mode_normal(self):
self.tui.opts.run_on_serial = False
self.assertTrue(self.tui.get_initial_rich_mode())

# With a state file.
with (pathlib.Path(self.tui.state_dir) / "rich-mode-tty").open("w") as fh:
fh.write(json.dumps(True))
self.assertTrue(self.tui.get_initial_rich_mode())
with (pathlib.Path(self.tui.state_dir) / "rich-mode-tty").open("w") as fh:
fh.write(json.dumps(False))
self.assertFalse(self.tui.get_initial_rich_mode())

def test_get_initial_rich_mode_serial(self):
self.tui.opts.run_on_serial = True
self.assertFalse(self.tui.get_initial_rich_mode())

# With a state file.
with (pathlib.Path(self.tui.state_dir) / "rich-mode-serial").open("w") as fh:
fh.write(json.dumps(True))
self.assertTrue(self.tui.get_initial_rich_mode())
with (pathlib.Path(self.tui.state_dir) / "rich-mode-serial").open("w") as fh:
fh.write(json.dumps(False))
self.assertFalse(self.tui.get_initial_rich_mode())

def test_get_initial_rich_mode_legacy_state_file(self):
# Make sure if an old rich-mode state file is present, it is honored -
# but the new format takes precedence.
self.tui.opts.run_on_serial = True
with (pathlib.Path(self.tui.state_dir) / "rich-mode").open("w") as fh:
fh.write(json.dumps(True))
self.assertTrue(self.tui.get_initial_rich_mode())
with (pathlib.Path(self.tui.state_dir) / "rich-mode-serial").open("w") as fh:
fh.write(json.dumps(False))
self.assertFalse(self.tui.get_initial_rich_mode())

self.tui.opts.run_on_serial = False
with (pathlib.Path(self.tui.state_dir) / "rich-mode").open("w") as fh:
fh.write(json.dumps(False))
self.assertFalse(self.tui.get_initial_rich_mode())
with (pathlib.Path(self.tui.state_dir) / "rich-mode-tty").open("w") as fh:
fh.write(json.dumps(True))
self.assertTrue(self.tui.get_initial_rich_mode())
46 changes: 37 additions & 9 deletions subiquitycore/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ def set_rich(self, rich):
urwid.util.set_encoding("ascii")
new_palette = PALETTE_MONO
self.rich_mode = False
with open(self.state_path("rich-mode"), "w") as fp:
if self.opts.run_on_serial:
rich_mode_file = "rich-mode-serial"
else:
rich_mode_file = "rich-mode-tty"
with open(self.state_path(rich_mode_file), "w") as fp:
json.dump(self.rich_mode, fp)
urwid.CanvasCache.clear()
self.urwid_loop.screen.register_palette(new_palette)
Expand All @@ -286,6 +290,37 @@ def extra_urwid_loop_args(self):
def make_screen(self, inputf=None, outputf=None):
return make_screen(self.opts.ascii, inputf, outputf)

def get_initial_rich_mode(self) -> bool:
"""Return the initial value for rich-mode, either loaded from an
exising state file or automatically determined. True means rich mode
and False means basic mode."""
if self.opts.run_on_serial:
rich_mode_file = "rich-mode-serial"
else:
rich_mode_file = "rich-mode-tty"
try:
fp = open(self.state_path(rich_mode_file))
except FileNotFoundError:
pass
else:
with fp:
return json.load(fp)

try:
# During the 23.10 development cycle, there was only one rich-mode
# state file. Let's handle the scenario where we just snap
# refresh-ed from a pre 23.10 release.
# Once mantic is EOL, let's remove this code.
fp = open(self.state_path("rich-mode"))
except FileNotFoundError:
pass
else:
with fp:
return json.load(fp)

# By default, basic on serial - rich otherwise.
return not self.opts.run_on_serial

def start_urwid(self, input=None, output=None):
# This stops the tcsetpgrp call in run_command_in_foreground from
# suspending us. See the rant there for more details.
Expand All @@ -302,14 +337,7 @@ def start_urwid(self, input=None, output=None):
**self.extra_urwid_loop_args(),
)
extend_dec_special_charmap()
try:
fp = open(self.state_path("rich-mode"))
except FileNotFoundError:
initial_rich_mode = not self.opts.run_on_serial
else:
with fp:
initial_rich_mode = json.load(fp)
self.set_rich(initial_rich_mode)
self.set_rich(self.get_initial_rich_mode())
self.urwid_loop.start()
self.select_initial_screen()

Expand Down

0 comments on commit c9f3e25

Please sign in to comment.