move "last-screen" handling out of generic code

This commit is contained in:
Michael Hudson-Doyle 2020-10-09 10:55:58 +13:00
parent 7d22e299bd
commit 2bf981ebb8
4 changed files with 45 additions and 36 deletions

View File

@ -13,7 +13,9 @@
# 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 logging
import os
import jsonschema
@ -79,9 +81,18 @@ class SubiquityController(BaseController):
def configured(self):
"""Let the world know that this controller's model is now configured.
"""
with open(self.app.state_path('states', self.name), 'w') as fp:
json.dump(self.serialize(), fp)
if self.model_name is not None:
self.app.base_model.configured(self.model_name)
def load_state(self):
state_path = self.app.state_path('states', self.name)
if not os.path.exists(state_path):
return
with open(state_path) as fp:
self.deserialize(json.load(fp))
def deserialize(self, state):
pass
@ -122,3 +133,6 @@ class RepeatedController(RepeatedController):
def make_autoinstall(self):
return {}
def load_state(self):
pass

View File

@ -326,8 +326,13 @@ class Subiquity(TuiApplication):
print()
break
def load_serialized_state(self):
for controller in self.controllers.instances:
controller.load_state()
async def start(self):
self.controllers.load_all()
self.load_serialized_state()
await self.connect()
if self.opts.autoinstall is not None:
await self.load_autoinstall_config()
@ -347,6 +352,15 @@ class Subiquity(TuiApplication):
return
super()._exception_handler(loop, context)
def _remove_last_screen(self):
last_screen = self.state_path('last-screen')
if os.path.exists(last_screen):
os.unlink(last_screen)
def exit(self):
self._remove_last_screen()
super().exit()
def extra_urwid_loop_args(self):
return dict(input_filter=self.input_filter.filter)
@ -411,6 +425,20 @@ class Subiquity(TuiApplication):
if isinstance(self.ui.body, BaseView):
self.ui.body.remove_overlay(overlay)
def initial_controller_index(self):
if not self.updated:
return 0
state_path = self.state_path('last-screen')
if not os.path.exists(state_path):
return 0
with open(state_path) as fp:
last_screen = fp.read().strip()
controller_index = 0
for i, controller in enumerate(self.controllers.instances):
if controller.name == last_screen:
controller_index = i
return controller_index
def select_initial_screen(self, index):
self.error_reporter.load_reports()
for report in self.error_reporter.reports:
@ -454,6 +482,8 @@ class Subiquity(TuiApplication):
view = await super().make_view_for_controller(new)
if new.answers:
self.aio_loop.call_soon(new.run_answers)
with open(self.state_path('last-screen'), 'w') as fp:
fp.write(new.name)
return view
else:
if self.autoinstall_config and not new.autoinstall_applied:

View File

@ -122,17 +122,8 @@ class Application:
controller.start()
log.debug("controllers started")
def load_serialized_state(self):
for controller in self.controllers.instances:
state_path = self.state_path('states', controller.name)
if not os.path.exists(state_path):
continue
with open(state_path) as fp:
controller.deserialize(json.load(fp))
async def start(self):
self.controllers.load_all()
self.load_serialized_state()
self._connect_base_signals()
self.start_controllers()

View File

@ -16,7 +16,6 @@
import asyncio
import inspect
import logging
import os
import yaml
import urwid
@ -80,15 +79,6 @@ class TuiApplication(Application):
self.urwid_loop = None
self.cur_screen = None
def _remove_last_screen(self):
last_screen = self.state_path('last-screen')
if os.path.exists(last_screen):
os.unlink(last_screen)
def exit(self):
self._remove_last_screen()
super().exit()
def run_command_in_foreground(self, cmd, before_hook=None, after_hook=None,
**kw):
screen = self.urwid_loop.screen
@ -122,8 +112,6 @@ class TuiApplication(Application):
raise
else:
self.cur_screen = new
with open(self.state_path('last-screen'), 'w') as fp:
fp.write(new.name)
return view
async def _wait_with_indication(self, awaitable, show, hide=None):
@ -190,7 +178,6 @@ class TuiApplication(Application):
async def _move_screen(self, increment, coro):
if coro is not None:
await coro
self.save_state()
old, self.cur_screen = self.cur_screen, None
if old is not None:
old.context.exit("completed")
@ -232,8 +219,6 @@ class TuiApplication(Application):
for controller in self.controllers.instances[:controller_index]:
controller.configured()
self.controllers.index = controller_index - 1
for controller in self.controllers.instances[:controller_index]:
controller.configured()
self.next_screen()
def run_scripts(self, scripts):
@ -343,18 +328,7 @@ class TuiApplication(Application):
self.select_initial_screen(self.initial_controller_index())
def initial_controller_index(self):
if not self.updated:
return 0
state_path = self.state_path('last-screen')
if not os.path.exists(state_path):
return 0
with open(state_path) as fp:
last_screen = fp.read().strip()
controller_index = 0
for i, controller in enumerate(self.controllers.instances):
if controller.name == last_screen:
controller_index = i
return controller_index
async def start(self, start_urwid=True):
await super().start()