use a cleaner way of attaching controllers to contexts
This commit is contained in:
parent
12f01405ae
commit
88165ce5fd
|
@ -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
|
|
@ -35,7 +35,7 @@ class SubiquityController(BaseController):
|
|||
def __init__(self, app):
|
||||
super().__init__(app)
|
||||
self.autoinstall_applied = False
|
||||
self.context.controller = self
|
||||
self.context.set('controller', self)
|
||||
self.setup_autoinstall()
|
||||
|
||||
def setup_autoinstall(self):
|
||||
|
|
|
@ -37,7 +37,6 @@ from subiquitycore.controller import Skip
|
|||
from subiquitycore.core import Application
|
||||
from subiquitycore.utils import run_command
|
||||
|
||||
from subiquity.context import SubiquityContext
|
||||
from subiquity.controllers.error import (
|
||||
ErrorReportKind,
|
||||
)
|
||||
|
@ -87,8 +86,6 @@ class Subiquity(Application):
|
|||
|
||||
project = "subiquity"
|
||||
|
||||
context_cls = SubiquityContext
|
||||
|
||||
def make_model(self):
|
||||
root = '/'
|
||||
if self.opts.dry_run:
|
||||
|
@ -256,6 +253,17 @@ class Subiquity(Application):
|
|||
traceback.print_exc()
|
||||
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):
|
||||
# report_start_event gets called when the Reporting controller
|
||||
# is being loaded...
|
||||
|
@ -263,25 +271,21 @@ class Subiquity(Application):
|
|||
if Reporting is not None:
|
||||
Reporting.report_start_event(
|
||||
context.full_name(), description, context.level)
|
||||
InstallProgress = getattr(self.controllers, "InstallProgress", None)
|
||||
if InstallProgress is not None and context.controller is not None:
|
||||
if self.interactive() and not context.controller.interactive():
|
||||
msg = context.full_name()
|
||||
if description:
|
||||
msg += ': ' + description
|
||||
self.controllers.InstallProgress.progress_view.event_start(
|
||||
context, msg)
|
||||
if self._push_to_progress(context):
|
||||
msg = context.full_name()
|
||||
if description:
|
||||
msg += ': ' + description
|
||||
self.controllers.InstallProgress.progress_view.event_start(
|
||||
context, msg)
|
||||
|
||||
def report_finish_event(self, context, description, status):
|
||||
Reporting = getattr(self.controllers, "Reporting", None)
|
||||
if Reporting is not None:
|
||||
Reporting.report_finish_event(
|
||||
context.full_name(), description, status, context.level)
|
||||
InstallProgress = getattr(self.controllers, "InstallProgress", None)
|
||||
if InstallProgress is not None and context.controller is not None:
|
||||
if self.interactive() and not context.controller.interactive():
|
||||
self.controllers.InstallProgress.progress_view.event_finish(
|
||||
context)
|
||||
if self._push_to_progress(context):
|
||||
self.controllers.InstallProgress.progress_view.event_finish(
|
||||
context)
|
||||
|
||||
def confirm_install(self):
|
||||
self.install_confirmed = True
|
||||
|
|
|
@ -54,6 +54,7 @@ class Context:
|
|||
if childlevel is None:
|
||||
childlevel = level
|
||||
self.childlevel = childlevel
|
||||
self.data = {}
|
||||
|
||||
@classmethod
|
||||
def new(cls, app):
|
||||
|
@ -97,3 +98,14 @@ class Context:
|
|||
result = Status.SUCCESS
|
||||
description = None
|
||||
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
|
||||
|
|
|
@ -315,7 +315,6 @@ class Application:
|
|||
# instance.
|
||||
|
||||
make_ui = SubiquityCoreUI
|
||||
context_cls = Context
|
||||
|
||||
def __init__(self, opts):
|
||||
self.debug_flags = ()
|
||||
|
@ -367,7 +366,7 @@ class Application:
|
|||
self.new_event_loop()
|
||||
self.urwid_loop = None
|
||||
self.controllers = ControllerSet(self, self.controllers)
|
||||
self.context = self.context_cls.new(self)
|
||||
self.context = Context.new(self)
|
||||
|
||||
def new_event_loop(self):
|
||||
new_loop = asyncio.new_event_loop()
|
||||
|
|
Loading…
Reference in New Issue