use a cleaner way of attaching controllers to contexts

This commit is contained in:
Michael Hudson-Doyle 2020-04-24 14:45:20 +12:00
parent 12f01405ae
commit 88165ce5fd
5 changed files with 34 additions and 45 deletions

View File

@ -1,26 +0,0 @@
# Copyright 2020 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/>.
from subiquitycore.context import Context
class SubiquityContext(Context):
controller = None
def __init__(self, app, name, description, parent, level, childlevel=None):
super().__init__(app, name, description, parent, level, childlevel)
if parent is not None:
self.controller = parent.controller

View File

@ -35,7 +35,7 @@ class SubiquityController(BaseController):
def __init__(self, app): def __init__(self, app):
super().__init__(app) super().__init__(app)
self.autoinstall_applied = False self.autoinstall_applied = False
self.context.controller = self self.context.set('controller', self)
self.setup_autoinstall() self.setup_autoinstall()
def setup_autoinstall(self): def setup_autoinstall(self):

View File

@ -37,7 +37,6 @@ from subiquitycore.controller import Skip
from subiquitycore.core import Application from subiquitycore.core import Application
from subiquitycore.utils import run_command from subiquitycore.utils import run_command
from subiquity.context import SubiquityContext
from subiquity.controllers.error import ( from subiquity.controllers.error import (
ErrorReportKind, ErrorReportKind,
) )
@ -87,8 +86,6 @@ class Subiquity(Application):
project = "subiquity" project = "subiquity"
context_cls = SubiquityContext
def make_model(self): def make_model(self):
root = '/' root = '/'
if self.opts.dry_run: if self.opts.dry_run:
@ -256,6 +253,17 @@ class Subiquity(Application):
traceback.print_exc() traceback.print_exc()
signal.pause() signal.pause()
def _push_to_progress(self, context):
if not self.interactive():
return False
InstallProgress = getattr(self.controllers, "InstallProgress", None)
if InstallProgress is None:
return False
controller = context.get('controller')
if controller is None or controller.interactive():
return False
return True
def report_start_event(self, context, description): def report_start_event(self, context, description):
# report_start_event gets called when the Reporting controller # report_start_event gets called when the Reporting controller
# is being loaded... # is being loaded...
@ -263,25 +271,21 @@ class Subiquity(Application):
if Reporting is not None: if Reporting is not None:
Reporting.report_start_event( Reporting.report_start_event(
context.full_name(), description, context.level) context.full_name(), description, context.level)
InstallProgress = getattr(self.controllers, "InstallProgress", None) if self._push_to_progress(context):
if InstallProgress is not None and context.controller is not None: msg = context.full_name()
if self.interactive() and not context.controller.interactive(): if description:
msg = context.full_name() msg += ': ' + description
if description: self.controllers.InstallProgress.progress_view.event_start(
msg += ': ' + description context, msg)
self.controllers.InstallProgress.progress_view.event_start(
context, msg)
def report_finish_event(self, context, description, status): def report_finish_event(self, context, description, status):
Reporting = getattr(self.controllers, "Reporting", None) Reporting = getattr(self.controllers, "Reporting", None)
if Reporting is not None: if Reporting is not None:
Reporting.report_finish_event( Reporting.report_finish_event(
context.full_name(), description, status, context.level) context.full_name(), description, status, context.level)
InstallProgress = getattr(self.controllers, "InstallProgress", None) if self._push_to_progress(context):
if InstallProgress is not None and context.controller is not None: self.controllers.InstallProgress.progress_view.event_finish(
if self.interactive() and not context.controller.interactive(): context)
self.controllers.InstallProgress.progress_view.event_finish(
context)
def confirm_install(self): def confirm_install(self):
self.install_confirmed = True self.install_confirmed = True

View File

@ -54,6 +54,7 @@ class Context:
if childlevel is None: if childlevel is None:
childlevel = level childlevel = level
self.childlevel = childlevel self.childlevel = childlevel
self.data = {}
@classmethod @classmethod
def new(cls, app): def new(cls, app):
@ -97,3 +98,14 @@ class Context:
result = Status.SUCCESS result = Status.SUCCESS
description = None description = None
self.exit(description, result) self.exit(description, result)
def set(self, key, value):
self.data[key] = value
def get(self, key, default=None):
c = self
while c is not None:
if key in c.data:
return c.data[key]
c = c.parent
return default

View File

@ -315,7 +315,6 @@ class Application:
# instance. # instance.
make_ui = SubiquityCoreUI make_ui = SubiquityCoreUI
context_cls = Context
def __init__(self, opts): def __init__(self, opts):
self.debug_flags = () self.debug_flags = ()
@ -367,7 +366,7 @@ class Application:
self.new_event_loop() self.new_event_loop()
self.urwid_loop = None self.urwid_loop = None
self.controllers = ControllerSet(self, self.controllers) self.controllers = ControllerSet(self, self.controllers)
self.context = self.context_cls.new(self) self.context = Context.new(self)
def new_event_loop(self): def new_event_loop(self):
new_loop = asyncio.new_event_loop() new_loop = asyncio.new_event_loop()