<commit_before>""" Service Walkthrough view

List out the updated bundle in a cleaner view showing what
charms and their relations will be done.
"""

from urwid import (connect_signal, Filler, WidgetWrap, Pile,
                   Text, Columns)

from conjure.app_config import app
from conjure.ui.widgets.option_widget import OptionWidget
from ubuntui.ev import EventLoop
from ubuntui.widgets.buttons import PlainButton
from ubuntui.widgets.input import IntegerEditor
from ubuntui.widgets.hr import HR
from ubuntui.utils import Color, Padding

import logging

log = logging.getLogger('conjure')


class ServiceWalkthroughView(WidgetWrap):
    def __init__(self, service, idx, n_total, metadata_controller, callback):
        self.callback = callback
        self.service = service
        self.idx = idx
        self.n_total = n_total
        self.n_remaining = n_total - idx - 1
        self.metadata_controller = metadata_controller
        w = self.build_widgets()
        super().__init__(w)
        self.get_async_info()
        self.pile.focus_position = len(self.pile.contents) - 2

    def selectable(self):
        return True

    def build_widgets(self):
        self.description_w = Text("Description Loading…")
        self.readme_w = Text("README Loading…")
        self.scale_edit = IntegerEditor(default=self.service.num_units)
        connect_signal(self.scale_edit._edit, 'change',
                       self.handle_scale_changed)
        self.continue_button = PlainButton(
            "Deploy and Configure Next Application",
            self.do_deploy)
        self.skip_rest_button = PlainButton(
            "Deploy all {} Remaining Applications with Bundle Defaults".format(
                self.n_remaining),
            self.do_skip_rest
        )
        col = Columns(
            [
                (6, Text('Units:', align='right')),
                (15,
                 Color.string_input(self.scale_edit,
                                    focus_map='string_input focus'))
            ], dividechars=1
        )
        ws = [Text("{} of {}: {}".format(self.idx+1, self.n_total,
                                         self.service.service_name.upper())),
              Padding.center(HR()),
              Padding.center(self.description_w, left=2),
              Padding.line_break(""),
              Padding.center(self.readme_w, left=2),
              Padding.center(HR()),
              Padding.left(col, left=1),
              Padding.line_break(""),
              Padding.right_50(
                  Color.button_primary(self.continue_button,
                                       focus_map='button_primary focus')),
              Padding.right_50(
                  Color.button_secondary(self.skip_rest_button,
                                         focus_map='button_secondary focus'))]

        self.pile = Pile(ws)
        return Padding.center_90(Filler(self.pile, valign="top"))

    def get_async_info(self):
        info = self.metadata_controller.get_charm_info(
            self.service.csid.as_str_without_rev(),
            self.handle_info_updated)
        if info:
            self.handle_info_updated(info)

        self.metadata_controller.get_readme(
            self.service.csid.as_seriesname(),
            self.handle_readme_updated)

    def handle_info_updated(self, new_info):
        EventLoop.loop.event_loop._loop.call_soon_threadsafe(
            self._update_info_on_main_thread,
            new_info)

    def _update_info_on_main_thread(self, new_info):
        self.description_w.set_text(
            new_info["Meta"]["charm-metadata"]["Summary"])
        self._invalidate()<commit_msg>walkthru: Show just one button for last service (#105)<commit_after>""" Service Walkthrough view

List out the updated bundle in a cleaner view showing what
charms and their relations will be done.
"""

from urwid import (connect_signal, Filler, WidgetWrap, Pile,
                   Text, Columns)

from conjure.app_config import app
from conjure.ui.widgets.option_widget import OptionWidget
from ubuntui.ev import EventLoop
from ubuntui.widgets.buttons import PlainButton
from ubuntui.widgets.input import IntegerEditor
from ubuntui.widgets.hr import HR
from ubuntui.utils import Color, Padding

import logging

log = logging.getLogger('conjure')


class ServiceWalkthroughView(WidgetWrap):
    def __init__(self, service, idx, n_total, metadata_controller, callback):
        self.callback = callback
        self.service = service
        self.idx = idx
        self.n_total = n_total
        self.n_remaining = n_total - idx - 1
        self.metadata_controller = metadata_controller
        w = self.build_widgets()
        super().__init__(w)
        self.get_async_info()
        if self.n_remaining == 0:
            self.pile.focus_position = len(self.pile.contents) - 1
        else:
            self.pile.focus_position = len(self.pile.contents) - 2

    def selectable(self):
        return True

    def build_widgets(self):
        self.description_w = Text("Description Loading…")
        self.readme_w = Text("README Loading…")
        self.scale_edit = IntegerEditor(default=self.service.num_units)
        connect_signal(self.scale_edit._edit, 'change',
                       self.handle_scale_changed)
        self.skip_rest_button = PlainButton(
            "Deploy all {} Remaining Applications with Bundle Defaults".format(
                self.n_remaining),
            self.do_skip_rest
        )
        col = Columns(
            [
                (6, Text('Units:', align='right')),
                (15,
                 Color.string_input(self.scale_edit,
                                    focus_map='string_input focus'))
            ], dividechars=1
        )

        if self.n_remaining == 0:
            buttons = [Padding.right_50(Color.button_primary(
                PlainButton("Deploy and Continue",
                            self.do_deploy),
                focus_map='button_primary focus'))]
        else:
            buttons = [
                Padding.right_50(Color.button_primary(
                    PlainButton(
                        "Deploy and Configure Next Application",
                        self.do_deploy),
                    focus_map='button_primary focus')),
                Padding.right_50(
                    Color.button_secondary(
                        self.skip_rest_button,
                        focus_map='button_secondary focus'))]

        ws = [Text("{} of {}: {}".format(self.idx+1, self.n_total,
                                         self.service.service_name.upper())),
              Padding.center(HR()),
              Padding.center(self.description_w, left=2),
              Padding.line_break(""),
              Padding.center(self.readme_w, left=2),
              Padding.center(HR()),
              Padding.left(col, left=1),
              Padding.line_break("")] + buttons

        self.pile = Pile(ws)
        return Padding.center_90(Filler(self.pile, valign="top"))

    def get_async_info(self):
        info = self.metadata_controller.get_charm_info(
            self.service.csid.as_str_without_rev(),
            self.handle_info_updated)
        if info:
            self.handle_info_updated(info)

        self.metadata_controller.get_readme(
            self.service.csid.as_seriesname(),
            self.handle_readme_updated)

    def handle_info_updated(self, new_info):
        EventLoop.loop.event_loop._loop.call_soon_threadsafe(
            self._update_info_on_main_thread,
            new_info)

    def _update_info_on_main_thread(self, new_info):
        self.description_w.set_text(
            new_info["Meta"]["charm-metadata"]["Summary"])
        self._invalidate()
{'fraction_non_alphanumeric': 0.09431041155979873, 'fraction_numerical': 0.005160624435556703, 'mean_word_length': 2.0495672698662473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2635165', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2103, 'n_words': 413}