<commit_before>import logging

from urwid import (
    BoxAdapter,
    connect_signal,
    LineBox,
    Text,
    )

from subiquitycore.models.network import WLANConfig
from subiquitycore.ui.buttons import cancel_btn, menu_btn
from subiquitycore.ui.container import (
    ListBox,
    Pile,
    WidgetWrap,
    )
from subiquitycore.ui.form import Form, PasswordField, StringField
from subiquitycore.ui.stretchy import Stretchy
from subiquitycore.ui.utils import (
    Color,
    disabled,
    Padding,
    )

log = logging.getLogger(
    'subiquitycore.network.network_configure_wlan_interface')


class NetworkList(WidgetWrap):

    def __init__(self, parent, ssids):
        self.parent = parent
        button = cancel_btn(_("Cancel"), on_press=self.do_cancel)
        ssid_list = [menu_btn(label=ssid, on_press=self.do_network)
                     for ssid in ssids if ssid]
        p = Pile([BoxAdapter(ListBox(ssid_list), height=10),
                  Padding.fixed_10(button)])
        box = LineBox(p, title=_("Select a network"))
        super().__init__(box)

    def do_network(self, sender):
        self.parent.form.ssid.value = sender.label
        self.parent.parent.remove_overlay()

    def do_cancel(self, sender):
        self.parent.parent.remove_overlay()


class WLANForm(Form):

    ok_label = _("Save")

    ssid = StringField(caption="Network Name:")
    psk = PasswordField(caption="Password:")

    def validate_psk(self):
        psk = self.psk.value
        if len(psk) == 0:
            return
        elif len(psk) < 8:
            return "Password must be at least 8 characters long if present"
        elif len(psk) > 63:
            return "Password must be less than 63 characters long"


class NetworkConfigureWLANStretchy(Stretchy):
    def __init__(self, parent, dev_info):
        self.parent = parent
        self.dev_info = dev_info
        title = _("Network interface {nic} WIFI configuration").format(
            nic=dev_info.name)

        self.form = WLANForm()

        connect_signal(self.form, 'submit', self.done)
        connect_signal(self.form, 'cancel', self.cancel)

        if self.dev_info.wlan.config.ssid:
            self.form.ssid.value = self.dev_info.wlan.config.ssid
        if self.dev_info.wlan.config.psk:
            self.form.psk.value = self.dev_info.wlan.config.psk

        self.ssid_row = self.form.ssid._table
        self.psk_row = self.form.psk._table
        self.ssid_row.bind(self.psk_row)

        self.inputs = Pile(self._build_iface_inputs())

        self.error = Text("")
        widgets = [
            self.inputs,
            Padding.center_79(Color.info_error(self.error)),
            self.form.buttons,
            ]
        super().__init__(title, widgets, 0, 0)

    def show_ssid_list(self, sender):
        self.parent.show_overlay(
            NetworkList(self, self.dev_info.wlan.visible_ssids),
            width=60)

    def start_scan(self, sender):
        fp = self.inputs.focus_position - 1
        while not self.inputs.contents[fp][0].selectable():
            fp -= 1
        self.inputs.focus_position = fp
        try:
            self.parent.controller.start_scan(self.dev_info.name)
        except RuntimeError as r:
            log.exception("start_scan failed")
            self.error.set_text("%s" % (r,))

    def _build_iface_inputs(self):
        visible_ssids = self.dev_info.wlan.visible_ssids
        if len(visible_ssids) > 0:
            networks_btn = menu_btn("Choose a visible network",
                                    on_press=self.show_ssid_list)
        else:
            networks_btn = disabled(menu_btn("No visible networks"))

        if not self.dev_info.wlan.scan_state:
            scan_btn = menu_btn("Scan for networks", on_press=self.start_scan)
        else:
            scan_btn = disabled(menu_btn("Scanning for networks"))

        warning = (
            "Only open or WPA2/PSK networks are supported at this time.")
        col = [
            Text(warning),
            Text(""),
            self.ssid_row,
            Text(""),
            Padding.fixed_32(networks_btn),
            Padding.fixed_32(scan_btn),
            Text(""),
            self.psk_row,
        ]
        return col

    def update_link(self, dev_info):
        self.dev_info = dev_info
        self.inputs.contents = [(obj, ('pack', None))
                                for obj in self._build_iface_inputs()]

    def done(self, sender):
        if self.dev_info.wlan.config.ssid is None and self.form.ssid.value:
            # Turn DHCP4 on by default when specifying an SSID for
            # the first time...
            self.parent.controller.enable_dhcp(self.dev_info.name, 4)
        if self.form.ssid.value:<commit_msg>NetworkConfigureWLANStretchy.update_link should only care about updates for its nic<commit_after>import logging

from urwid import (
    BoxAdapter,
    connect_signal,
    LineBox,
    Text,
    )

from subiquitycore.models.network import WLANConfig
from subiquitycore.ui.buttons import cancel_btn, menu_btn
from subiquitycore.ui.container import (
    ListBox,
    Pile,
    WidgetWrap,
    )
from subiquitycore.ui.form import Form, PasswordField, StringField
from subiquitycore.ui.stretchy import Stretchy
from subiquitycore.ui.utils import (
    Color,
    disabled,
    Padding,
    )

log = logging.getLogger(
    'subiquitycore.network.network_configure_wlan_interface')


class NetworkList(WidgetWrap):

    def __init__(self, parent, ssids):
        self.parent = parent
        button = cancel_btn(_("Cancel"), on_press=self.do_cancel)
        ssid_list = [menu_btn(label=ssid, on_press=self.do_network)
                     for ssid in ssids if ssid]
        p = Pile([BoxAdapter(ListBox(ssid_list), height=10),
                  Padding.fixed_10(button)])
        box = LineBox(p, title=_("Select a network"))
        super().__init__(box)

    def do_network(self, sender):
        self.parent.form.ssid.value = sender.label
        self.parent.parent.remove_overlay()

    def do_cancel(self, sender):
        self.parent.parent.remove_overlay()


class WLANForm(Form):

    ok_label = _("Save")

    ssid = StringField(caption="Network Name:")
    psk = PasswordField(caption="Password:")

    def validate_psk(self):
        psk = self.psk.value
        if len(psk) == 0:
            return
        elif len(psk) < 8:
            return "Password must be at least 8 characters long if present"
        elif len(psk) > 63:
            return "Password must be less than 63 characters long"


class NetworkConfigureWLANStretchy(Stretchy):
    def __init__(self, parent, dev_info):
        self.parent = parent
        self.dev_info = dev_info
        title = _("Network interface {nic} WIFI configuration").format(
            nic=dev_info.name)

        self.form = WLANForm()

        connect_signal(self.form, 'submit', self.done)
        connect_signal(self.form, 'cancel', self.cancel)

        if self.dev_info.wlan.config.ssid:
            self.form.ssid.value = self.dev_info.wlan.config.ssid
        if self.dev_info.wlan.config.psk:
            self.form.psk.value = self.dev_info.wlan.config.psk

        self.ssid_row = self.form.ssid._table
        self.psk_row = self.form.psk._table
        self.ssid_row.bind(self.psk_row)

        self.inputs = Pile(self._build_iface_inputs())

        self.error = Text("")
        widgets = [
            self.inputs,
            Padding.center_79(Color.info_error(self.error)),
            self.form.buttons,
            ]
        super().__init__(title, widgets, 0, 0)

    def show_ssid_list(self, sender):
        self.parent.show_overlay(
            NetworkList(self, self.dev_info.wlan.visible_ssids),
            width=60)

    def start_scan(self, sender):
        fp = self.inputs.focus_position - 1
        while not self.inputs.contents[fp][0].selectable():
            fp -= 1
        self.inputs.focus_position = fp
        try:
            self.parent.controller.start_scan(self.dev_info.name)
        except RuntimeError as r:
            log.exception("start_scan failed")
            self.error.set_text("%s" % (r,))

    def _build_iface_inputs(self):
        visible_ssids = self.dev_info.wlan.visible_ssids
        if len(visible_ssids) > 0:
            networks_btn = menu_btn("Choose a visible network",
                                    on_press=self.show_ssid_list)
        else:
            networks_btn = disabled(menu_btn("No visible networks"))

        if not self.dev_info.wlan.scan_state:
            scan_btn = menu_btn("Scan for networks", on_press=self.start_scan)
        else:
            scan_btn = disabled(menu_btn("Scanning for networks"))

        warning = (
            "Only open or WPA2/PSK networks are supported at this time.")
        col = [
            Text(warning),
            Text(""),
            self.ssid_row,
            Text(""),
            Padding.fixed_32(networks_btn),
            Padding.fixed_32(scan_btn),
            Text(""),
            self.psk_row,
        ]
        return col

    def update_link(self, dev_info):
        if dev_info.name != self.dev_info.name:
            return
        self.dev_info = dev_info
        self.inputs.contents = [(obj, ('pack', None))
                                for obj in self._build_iface_inputs()]

    def done(self, sender):
        if self.dev_info.wlan.config.ssid is None and self.form.ssid.value:
            # Turn DHCP4 on by default when specifying an SSID for
            # the first time...
            self.parent.controller.enable_dhcp(self.dev_info.name, 4)
        if self.form.ssid.value:
{'fraction_non_alphanumeric': 0.10299761435535733, 'fraction_numerical': 0.005808526086505549, 'mean_word_length': 2.1821782178217823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2889591', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2921, 'n_words': 607}