<commit_before>"""
Copyright 2021 <NAME>.  See LICENSE for details.

The Text User Interface state machine
"""

import logging
import sys

from asciimatics.exceptions import ResizeScreenError, StopApplication
from asciimatics.scene import Scene
from asciimatics.screen import Screen
import farc

from tui_ident import IdentModel, IdentView
from tui_msgs import MsgsModel, MsgsView
from tui_status import RadioStatusModel, RadioStatusView
from tui_stngs import RadioStngsModel, RadioStngsView

UI_ANIMATE_PERIOD = 0.050  # Taken from asciimatics screen.py


class TxtUiHsm(farc.Ahsm):
    def __init__(self, phy_hsm, lnk_hsm):
        """Language-specific initialization."""
        super().__init__()
        self._ident_model = IdentModel()
        self._msgs_model = MsgsModel(lnk_hsm)
        self._status_model = RadioStatusModel(phy_hsm)
        self._stngs_model = RadioStngsModel(phy_hsm)

    @farc.Hsm.state
    def _initial(self, event):
        """PseudoState: _initial

        State machine framework initialization
        """
        farc.Signal.register("_UI_KEYCODE")
        self._tmout_evt = farc.TimeEvent("_UI_TMOUT")
        return self.tran(self._running)


    @farc.Hsm.state
    def _running(self, event):
        """State: _running

        Application init and running.
        """
        sig = event.signal
        if sig == farc.Signal.ENTRY:
            logging.debug("UI._running")
            self._tmout_evt.post_in(self, 0)
            self._prev_tick = farc.Framework._event_loop.time()

            if (self._ident_model.device_cred_exists() and
                self._ident_model.personal_cert_exists()):
                start_scene = None
            else:
                start_scene = Scene([], -1, name="Identity")
            self._new_screen(start_scene)
            return self.handled(event)

        elif sig == farc.Signal._UI_TMOUT:
            try:
                self._screen.draw_next_frame()

            except ResizeScreenError as e:
                self._new_screen(e.scene)

            except StopApplication:
                self._screen.close()
                return self.tran(self._exiting)

            # Iterate immediately if this step took too long
            now = farc.Framework._event_loop.time()
            if now > self._prev_tick + UI_ANIMATE_PERIOD:
                self._tmout_evt.post_in(self, 0)
                self._prev_tick = now

            # otherwise iterate one period later
            else:
                self._prev_tick += UI_ANIMATE_PERIOD
                self._tmout_evt.post_at(self, self._prev_tick)
            return self.handled(event)

        elif sig == farc.Signal.EXIT:
            self._tmout_evt.disarm()
            return self.handled(event)

        return self.super(self.top)


    @farc.Hsm.state
    def _exiting(self, event):
        sig = event.signal
        if sig == farc.Signal.ENTRY:
            logging.debug("UI._exiting")
            sys.exit(0)
            return self.handled(event)
<commit_msg>Make UI Quit cause Framework.stop()<commit_after>"""
Copyright 2021 <NAME>.  See LICENSE for details.

The Text User Interface state machine
"""

import logging
import sys

from asciimatics.exceptions import ResizeScreenError, StopApplication
from asciimatics.scene import Scene
from asciimatics.screen import Screen
import farc

from tui_ident import IdentModel, IdentView
from tui_msgs import MsgsModel, MsgsView
from tui_status import RadioStatusModel, RadioStatusView
from tui_stngs import RadioStngsModel, RadioStngsView

UI_ANIMATE_PERIOD = 0.050  # Taken from asciimatics screen.py


class TxtUiHsm(farc.Ahsm):
    def __init__(self, phy_hsm, lnk_hsm):
        """Language-specific initialization."""
        super().__init__()
        self._ident_model = IdentModel()
        self._msgs_model = MsgsModel(lnk_hsm)
        self._status_model = RadioStatusModel(phy_hsm)
        self._stngs_model = RadioStngsModel(phy_hsm)

    @farc.Hsm.state
    def _initial(self, event):
        """PseudoState: _initial

        State machine framework initialization
        """
        farc.Signal.register("_UI_KEYCODE")
        self._tmout_evt = farc.TimeEvent("_UI_TMOUT")
        return self.tran(self._running)


    @farc.Hsm.state
    def _running(self, event):
        """State: _running

        Application init and running.
        """
        sig = event.signal
        if sig == farc.Signal.ENTRY:
            logging.debug("UI._running")
            self._tmout_evt.post_in(self, 0)
            self._prev_tick = farc.Framework._event_loop.time()

            if (self._ident_model.device_cred_exists() and
                self._ident_model.personal_cert_exists()):
                start_scene = None
            else:
                start_scene = Scene([], -1, name="Identity")
            self._new_screen(start_scene)
            return self.handled(event)

        elif sig == farc.Signal._UI_TMOUT:
            try:
                self._screen.draw_next_frame()

            except ResizeScreenError as e:
                self._new_screen(e.scene)

            except StopApplication:
                self._screen.close()
                return self.tran(self._exiting)

            # Iterate immediately if this step took too long
            now = farc.Framework._event_loop.time()
            if now > self._prev_tick + UI_ANIMATE_PERIOD:
                self._tmout_evt.post_in(self, 0)
                self._prev_tick = now

            # otherwise iterate one period later
            else:
                self._prev_tick += UI_ANIMATE_PERIOD
                self._tmout_evt.post_at(self, self._prev_tick)
            return self.handled(event)

        elif sig == farc.Signal.EXIT:
            self._tmout_evt.disarm()
            return self.handled(event)

        return self.super(self.top)


    @farc.Hsm.state
    def _exiting(self, event):
        sig = event.signal
        if sig == farc.Signal.ENTRY:
            logging.debug("UI._exiting")
            farc.Framework.stop()
            return self.handled(event)

{'fraction_non_alphanumeric': 0.08692788936450445, 'fraction_numerical': 0.0037866315442871253, 'mean_word_length': 2.1690140845070425, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1505035', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1789, 'n_words': 386}