From fdc43496f59e4be862df05e315d3a0cff961c5a7 Mon Sep 17 00:00:00 2001 From: Carlos Nihelton Date: Fri, 19 Aug 2022 15:38:13 -0300 Subject: [PATCH] Implements client and server ... ... for WSL Setup Options Both controllers and UI --- .../client/controllers/wslsetupoptions.py | 45 +++++++++++ .../server/controllers/wslsetupoptions.py | 79 +++++++++++++++++++ system_setup/ui/views/wslsetupoptions.py | 76 ++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 system_setup/client/controllers/wslsetupoptions.py create mode 100644 system_setup/server/controllers/wslsetupoptions.py create mode 100644 system_setup/ui/views/wslsetupoptions.py diff --git a/system_setup/client/controllers/wslsetupoptions.py b/system_setup/client/controllers/wslsetupoptions.py new file mode 100644 index 00000000..1937cb6d --- /dev/null +++ b/system_setup/client/controllers/wslsetupoptions.py @@ -0,0 +1,45 @@ +# Copyright 2022 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 . + +import logging + +from subiquity.client.controller import SubiquityTuiController +from subiquity.common.types import WSLSetupOptions +from system_setup.ui.views.wslsetupoptions import WSLSetupOptionsView + +log = logging.getLogger('system_setup.client.controllers.wslsetupoptions') + + +class WSLSetupOptionsController(SubiquityTuiController): + endpoint_name = 'wslsetupoptions' + + async def make_ui(self): + data = await self.endpoint.GET() + return WSLSetupOptionsView(self, data) + + def run_answers(self): + if all(elem in self.answers for elem in + ['install_language_support_packages']): + configuration = WSLSetupOptions(**self.answers) + self.done(configuration) + + def done(self, configuration_data): + log.debug( + "WSLSetupOptionsController.done next_screen user_spec=%s", + configuration_data) + self.app.next_screen(self.endpoint.POST(configuration_data)) + + def cancel(self): + self.app.prev_screen() diff --git a/system_setup/server/controllers/wslsetupoptions.py b/system_setup/server/controllers/wslsetupoptions.py new file mode 100644 index 00000000..b5614c7e --- /dev/null +++ b/system_setup/server/controllers/wslsetupoptions.py @@ -0,0 +1,79 @@ +# Copyright 2022 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 . + +import logging +import attr + +from subiquitycore.context import with_context + +from subiquity.common.apidef import API +from subiquity.common.types import WSLSetupOptions +from subiquity.server.controller import SubiquityController + +from system_setup.common.wsl_conf import default_loader +from system_setup.common.wsl_utils import convert_if_bool + +log = logging.getLogger('system_setup.server.controllers.wslsetupoptions') + + +class WSLSetupOptionsController(SubiquityController): + + endpoint = API.wslsetupoptions + + autoinstall_key = model_name = "wslsetupoptions" + autoinstall_schema = { + 'type': 'object', + 'properties': { + 'install_language_support_packages': {'type': 'boolean'}, + }, + 'additionalProperties': False, + } + + def __init__(self, app): + super().__init__(app) + + # load the config file + data = default_loader() + + if data: + proc_data = \ + {key: convert_if_bool(value) for (key, value) in data.items()} + conf_data = WSLSetupOptions(**proc_data) + self.model.apply_settings(conf_data) + + def load_autoinstall_data(self, data): + if data is not None: + identity_data = WSLSetupOptions(**data) + self.model.apply_settings(identity_data) + + @with_context() + async def apply_autoinstall_config(self, context=None): + pass + + def make_autoinstall(self): + r = attr.asdict(self.model.wslconfbase) + return r + + async def GET(self) -> WSLSetupOptions: + data = WSLSetupOptions() + if self.model.wslsetupoptions is not None: + data.install_language_support_packages = \ + self.model.wslsetupoptions.install_language_support_packages + return data + + async def POST(self, data: WSLSetupOptions): + self.model.apply_settings(data) + await self.configured() + diff --git a/system_setup/ui/views/wslsetupoptions.py b/system_setup/ui/views/wslsetupoptions.py new file mode 100644 index 00000000..e7f98b05 --- /dev/null +++ b/system_setup/ui/views/wslsetupoptions.py @@ -0,0 +1,76 @@ +# Copyright 2022 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 . + +""" WSLSetupOptions + +WSLSetupOptions provides user with options to customize the setup experience. +""" + +from gettext import install +from urwid import ( + connect_signal, +) + +from subiquitycore.ui.form import ( + Form, + BooleanField, +) +from subiquitycore.ui.utils import screen +from subiquitycore.view import BaseView +from subiquity.common.types import WSLSetupOptions + + +class WSLSetupOptionsForm(Form): + def __init__(self, initial): + super().__init__(initial=initial) + connect_signal(self.install_language_support_packages.widget, "change", + self.toggle_help) + + +======= +class WSLSetupOptionsForm(Form): +>>>>>>> 18290ac4 (Fixed help strings) + install_language_support_packages = \ + BooleanField(_("Install packages for better language support"), + help=_("Not recommended for slow internet connections.")) + + + +class WSLSetupOptionsView(BaseView): + title = _("Enhance your experience") + excerpt = _("Adjust the following options for a more complete experience.") + + def __init__(self, controller, configuration_data): + self.controller = controller + + initial = { + 'install_language_support_packages': + configuration_data.install_language_support_packages, + } + self.form = WSLSetupOptionsForm(initial=initial) + + connect_signal(self.form, 'submit', self.done) + super().__init__( + screen( + self.form.as_rows(), + [self.form.done_btn], + focus_buttons=True, + excerpt=self.excerpt, + ) + ) + + def done(self, result): + self.controller.done(WSLSetupOptions(**self.form.as_data())) +