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

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

from sublime import platform
from threading import Thread
from time import sleep

from . import __version__ as version
from ..libraries.pyserial.tools import list_ports
from ..libraries import pyserial
from .tools import get_setting
from .messages import MessageQueue
from .I18n import I18n

def serial_port_list():
    """List of Ports
    
    Return the list of serial ports availables on the system.
    
    Returns:
        [list/list] -- list of list like [['port1 fullname', 
                       port_name]['port2 fullname', 'port_name']]
    """
    ports = list(list_ports.comports()) 
    dev_names = ['ttyACM', 'ttyUSB', 'tty.', 'cu.']
    serial_ports = []
    for port_no, description, address in ports:
        for dev_name in dev_names:
            if(address != 'n/a' and dev_name in port_no or platform() == 'windows'):
                serial_ports.append([description, port_no])
                break

    return serial_ports


serials_in_use = []
serial_monitor_dict = {}


class SerialMonitor(object):
    """
    Handle the messages sended and received from/to the serial monitor
    """

    def __init__(self, serial_port):
        self.port = serial_port
        self.serial = pyserial.Serial()
        self.serial.port = serial_port
        self.is_alive = False
        self.baudrate = get_setting('baudrate', 9600)

        type_console = 'sexec'
        serial_header = I18n().translate("serial_monitor_header{0}{1}", version, serial_port)
        output_console = get_setting('output_console', False)
        
        if(not output_console):
            type_console = serial_header.strip('\\n')
    
        message = MessageQueue(serial_header)
        message.set_console(type_console)
        message.start_print()

        self.dprint = message.put
        self.dstop = message.stop_print
        self.clean = message.clean_console

    def is_running(self):
        """Monitor Running
        
        Check if the monitorserial is running
        
        Returns:
            [bool] -- True if it's running
        """
        return self.is_alive

    def start_async(self):
        """Start serial monitor
        
        Open the serial monitor in a new thread to avoid block
        the sublime text UI
        """
        monitor_thread = Thread(target=self.start)
        monitor_thread.start()

    def start(self):
        """Start serial monitor
        
        Starts to receive data from the serial monitor in a new thread.

        """
        if(not self.is_alive):
            self.serial.baudrate = self.baudrate
            
            if is_available(self.port):
                self.serial.open()
                self.is_alive = True
                
                monitor_thread = Thread(target=self.receive)
                monitor_thread.start()
            else:
                self.stop()

    def stop(self):
        """Stop serial monitor
        
        Stops the loop who is wating for more information from the serial port
        """
        self.is_alive = False
        serials_in_use.remove(self.port)<commit_msg>bug fix to make sure the serial port is in use before to remove it<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

from sublime import platform
from threading import Thread
from time import sleep

from . import __version__ as version
from ..libraries.pyserial.tools import list_ports
from ..libraries import pyserial
from .tools import get_setting
from .messages import MessageQueue
from .I18n import I18n

def serial_port_list():
    """List of Ports
    
    Return the list of serial ports availables on the system.
    
    Returns:
        [list/list] -- list of list like [['port1 fullname', 
                       port_name]['port2 fullname', 'port_name']]
    """
    ports = list(list_ports.comports()) 
    dev_names = ['ttyACM', 'ttyUSB', 'tty.', 'cu.']
    serial_ports = []
    for port_no, description, address in ports:
        for dev_name in dev_names:
            if(address != 'n/a' and dev_name in port_no or platform() == 'windows'):
                serial_ports.append([description, port_no])
                break

    return serial_ports


serials_in_use = []
serial_monitor_dict = {}


class SerialMonitor(object):
    """
    Handle the messages sended and received from/to the serial monitor
    """

    def __init__(self, serial_port):
        self.port = serial_port
        self.serial = pyserial.Serial()
        self.serial.port = serial_port
        self.is_alive = False
        self.baudrate = get_setting('baudrate', 9600)

        type_console = 'sexec'
        serial_header = I18n().translate("serial_monitor_header{0}{1}", version, serial_port)
        output_console = get_setting('output_console', False)
        
        if(not output_console):
            type_console = serial_header.strip('\\n')
    
        message = MessageQueue(serial_header)
        message.set_console(type_console)
        message.start_print()

        self.dprint = message.put
        self.dstop = message.stop_print
        self.clean = message.clean_console

    def is_running(self):
        """Monitor Running
        
        Check if the monitorserial is running
        
        Returns:
            [bool] -- True if it's running
        """
        return self.is_alive

    def start_async(self):
        """Start serial monitor
        
        Open the serial monitor in a new thread to avoid block
        the sublime text UI
        """
        monitor_thread = Thread(target=self.start)
        monitor_thread.start()

    def start(self):
        """Start serial monitor
        
        Starts to receive data from the serial monitor in a new thread.

        """
        if(not self.is_alive):
            self.serial.baudrate = self.baudrate
            
            if is_available(self.port):
                self.serial.open()
                self.is_alive = True
                
                monitor_thread = Thread(target=self.receive)
                monitor_thread.start()
            else:
                self.stop()

    def stop(self):
        """Stop serial monitor
        
        Stops the loop who is wating for more information from the serial port
        """
        self.is_alive = False
        if(self.port in serials_in_use):
            serials_in_use.remove(self.port)
{'fraction_non_alphanumeric': 0.08556946929039952, 'fraction_numerical': 0.004472271914132379, 'mean_word_length': 2.1335824381130313, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2598626', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1824, 'n_words': 534}