<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sublime
from glob import glob

from ..platformio.project_recognition import ProjectRecognition
from .quick_panel import quick_panel
from .paths import getBoardsFileDataPath
from .tools import get_setting, save_setting
from .preferences_bridge import PreferencesBridge
from .serial import serial_port_list
from .I18n import I18n

_ = None

class QuickMenu(PreferencesBridge):
    def __init__(self):
        super(QuickMenu, self).__init__()
        self.index = 0
        self.quick_list = []
        self.deeper = 0

        global _
        _ = I18n().translate

    def quick_boards(self):
        """Boards Menu
        
        Shows the quick panel with the availables boards in
        deviot
        """
        boards_list = self.quick_boards_list()
        quick_panel(boards_list, self.callback_board)

    def callback_board(self, selected):
        """Board Callback
        
        The quick panel returns the index of the option selected,
        this index is used to get the id of the board and the id
        is stored in the setting file.

        last_action is used to run the compilation or upload if was
        the action before call the list of boards
        
        Arguments:
            selected {int} -- index of the selected board
        """
        if(selected == -1):
            save_setting('last_action', None)
            return

        boards_list = self.quick_boards_list()
        board_select = boards_list[selected][-1]
        board_id = board_select.split("|")[-1].strip()

        self.save_selected_board(board_id)
        self.run_last_action()

    def quick_boards_list(self):
        """Boards List
        
        PlatformIO returns a JSON list with all information of the boards,
        the quick panel requires a list with a different format. We will only
        show the name (caption), id and vendor.
        
        Returns:
            list -- boards list
        """
        from .file import File

        selected_boards = self.get_selected_boards()
        boards_path = getBoardsFileDataPath()
        boards_file = File(boards_path)
        boards = boards_file.read_json()
        boards_list = []

        for board in boards:
            id = board['id']
            vendor = board['vendor']

            if(id in selected_boards):
                start = '- '
            else:
                start = '+ '

            caption = start + board['name']
            extra = "%s | %s" % (vendor, id)
            boards_list.append([caption, extra])

        return boards_list

    def quick_environments(self):
        """Environment Panel
        
        Displays the quick panel with the available environments
        """
        environments_list = self.quick_environment_list()
        quick_panel(environments_list, self.callback_environment, index=self.index)

    def callback_environment(self, selected):
        """Environment Callback
        
        Callback to store the select environment

        last_action is used to run the compilation or upload if was
        the action before call the list of environments
        
        Arguments:
            selected {int} -- option selected (index)
        """
        if(selected == -1):
            save_setting('last_action', None)
            return

        environments_list = self.quick_environment_list()
        environment_select = environments_list[selected][1]
        environment = environment_select.split("|")[-1].strip()

        self.save_environment(environment)
        self.run_last_action()

    def quick_environment_list(self):
        """
        gets a list with all selected environments and format it
        to be shown in the quick panel
        """
        from .file import File

        environments_list = []
        boards = self.quick_boards_list()
        environments = self.get_selected_boards()
        environment = self.get_environment()

        index = 0
        total = len(environments)
        count = total

        if(environments):
            for value in boards:
                data = value[1].split("|")
                caption = value[0][2:]
                id = data[1].strip()
                vendor = data[0].strip()

                for listed in environments:
                    if(listed == id):
                        vendor = "%s | %s" % (vendor, id)
                        environments_list.append([caption, vendor])
                        count -= 1

                        if(environment == listed):
                            self.index = index

                        index += 1

                if(not count):
                    break

        return environments_list

    def quick_serial_ports(self):
        """List of Serial Ports
        
        Show the list of serial ports availables in the quick panel
        """
        port_list = self.quick_serial_list()

        quick_panel(port_list, self.callback_serial_ports)

    def callback_serial_ports(self, selected):
        """Selected Port Callback
        
        Stores the selected serial port in the preferences file
        
        Arguments:
            selected {str} -- Port selected ex. 'COM1'
        """
        if(selected <= 0):
            save_setting('last_action', None)
            return

        if(selected == 1):
            self.window.run_command('deviot_set_ip')
            return

        port_list = self.quick_serial_list()
        print(port_list)
        print(selected)
        port_selected = port_list[selected][1]
        
        save_setting('port_id', port_selected)

        self.run_last_action()

    def quick_serial_list(self):
        """Serial Port List
        
        Gets the list of serial ports and mdns services and
        return it
        
        Returns:
            list -- available serial ports/mdns services
        """

        header = _("select_port_list").upper()
        ports_list = self.get_ports_list()
        ports_list.insert(0, [header])
        ports_list.insert(1, [_("menu_add_ip")])     

        if(len(ports_list) < 2):
            ports_list = [_("menu_none_serial_mdns").upper()]

        return ports_list

    def quick_language(self):
        """Language Panel
        
        Shows the panel to selecte the language of the plugin
        """<commit_msg>Avoids to request the port list in the callback, instead, it will use the list previously obtained<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sublime
from glob import glob

from ..platformio.project_recognition import ProjectRecognition
from .quick_panel import quick_panel
from .paths import getBoardsFileDataPath
from .tools import get_setting, save_setting
from .preferences_bridge import PreferencesBridge
from .serial import serial_port_list
from .I18n import I18n

_ = None

class QuickMenu(PreferencesBridge):
    def __init__(self):
        super(QuickMenu, self).__init__()
        self.index = 0
        self.quick_list = []
        self.deeper = 0

        global _
        _ = I18n().translate

    def quick_boards(self):
        """Boards Menu
        
        Shows the quick panel with the availables boards in
        deviot
        """
        boards_list = self.quick_boards_list()
        quick_panel(boards_list, self.callback_board)

    def callback_board(self, selected):
        """Board Callback
        
        The quick panel returns the index of the option selected,
        this index is used to get the id of the board and the id
        is stored in the setting file.

        last_action is used to run the compilation or upload if was
        the action before call the list of boards
        
        Arguments:
            selected {int} -- index of the selected board
        """
        if(selected == -1):
            save_setting('last_action', None)
            return

        boards_list = self.quick_boards_list()
        board_select = boards_list[selected][-1]
        board_id = board_select.split("|")[-1].strip()

        self.save_selected_board(board_id)
        self.run_last_action()

    def quick_boards_list(self):
        """Boards List
        
        PlatformIO returns a JSON list with all information of the boards,
        the quick panel requires a list with a different format. We will only
        show the name (caption), id and vendor.
        
        Returns:
            list -- boards list
        """
        from .file import File

        selected_boards = self.get_selected_boards()
        boards_path = getBoardsFileDataPath()
        boards_file = File(boards_path)
        boards = boards_file.read_json()
        boards_list = []

        for board in boards:
            id = board['id']
            vendor = board['vendor']

            if(id in selected_boards):
                start = '- '
            else:
                start = '+ '

            caption = start + board['name']
            extra = "%s | %s" % (vendor, id)
            boards_list.append([caption, extra])

        return boards_list

    def quick_environments(self):
        """Environment Panel
        
        Displays the quick panel with the available environments
        """
        environments_list = self.quick_environment_list()
        quick_panel(environments_list, self.callback_environment, index=self.index)

    def callback_environment(self, selected):
        """Environment Callback
        
        Callback to store the select environment

        last_action is used to run the compilation or upload if was
        the action before call the list of environments
        
        Arguments:
            selected {int} -- option selected (index)
        """
        if(selected == -1):
            save_setting('last_action', None)
            return

        environments_list = self.quick_environment_list()
        environment_select = environments_list[selected][1]
        environment = environment_select.split("|")[-1].strip()

        self.save_environment(environment)
        self.run_last_action()

    def quick_environment_list(self):
        """
        gets a list with all selected environments and format it
        to be shown in the quick panel
        """
        from .file import File

        environments_list = []
        boards = self.quick_boards_list()
        environments = self.get_selected_boards()
        environment = self.get_environment()

        index = 0
        total = len(environments)
        count = total

        if(environments):
            for value in boards:
                data = value[1].split("|")
                caption = value[0][2:]
                id = data[1].strip()
                vendor = data[0].strip()

                for listed in environments:
                    if(listed == id):
                        vendor = "%s | %s" % (vendor, id)
                        environments_list.append([caption, vendor])
                        count -= 1

                        if(environment == listed):
                            self.index = index

                        index += 1

                if(not count):
                    break

        return environments_list

    def quick_serial_ports(self):
        """List of Serial Ports
        
        Show the list of serial ports availables in the quick panel
        """
        self.quick_list = self.quick_serial_list()

        quick_panel(self.quick_list, self.callback_serial_ports)

    def callback_serial_ports(self, selected):
        """Selected Port Callback
        
        Stores the selected serial port in the preferences file
        
        Arguments:
            selected {str} -- Port selected ex. 'COM1'
        """
        if(selected <= 0):
            save_setting('last_action', None)
            return

        if(selected == 1):
            self.window.run_command('deviot_set_ip')
            return

        port_selected = self.quick_list[selected][1]
        save_setting('port_id', port_selected)

        self.run_last_action()

    def quick_serial_list(self):
        """Serial Port List
        
        Gets the list of serial ports and mdns services and
        return it
        
        Returns:
            list -- available serial ports/mdns services
        """

        header = _("select_port_list").upper()
        ports_list = self.get_ports_list()
        ports_list.insert(0, [header])
        ports_list.insert(1, [_("menu_add_ip")])     

        if(len(ports_list) < 2):
            ports_list = [_("menu_none_serial_mdns").upper()]

        return ports_list

    def quick_language(self):
        """Language Panel
        
        Shows the panel to selecte the language of the plugin
        """
{'fraction_non_alphanumeric': 0.0815480844409695, 'fraction_numerical': 0.004691164972634871, 'mean_word_length': 1.8317467345583351, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '479166', 'n_tokens_mistral': 3361, 'n_tokens_neox': 3212, 'n_words': 970}