<commit_before>from threading import Thread, Event
from Queue import Queue, Empty
from multiprocessing import Process
from packet_controller import PacketController
from time import sleep
from .. import config as c

class PacketForwarder(Thread):

    def __init__(self, reset_callback=None):
        super(PacketForwarder, self).__init__()
        self._stop = Event()
        self.packets_received = 0 # float cuz this can get big
        self.sending_queue = Queue()
        self.callback_dict = dict()
        self.reset_callback = reset_callback
        self.pc = PacketController()
        self.pipe = self.pc.pipe_outside

    def stop(self):
        self._stop.set()

    def empty_queue(self):
        while not self.sending_queue.empty():
            try: self.sending_queue.get(False)
            except Empty: pass

    def forward_requests(self):
        try:
            packet, callback = self.sending_queue.get(block=False)
            self.callback_dict[packet[:2]] = callback
            self.pipe.send(packet)
            self.sending_queue.task_done()
        except Empty:
            pass

    def callback_responses(self):
        while self.pipe.poll():
            request, response = self.pipe.recv()
            self.packets_received += 1
            if response == c.host.reset_msg: 
                self.empty_queue()
                if self.reset_callback: self.reset_callback()
            elif len(response) == 1 and response in c.responses:
                if response == 'G': continue
                print ("Firmware responded with an error: {} "
                       "for the request {}").format(
                            c.responses[response].msg, request)
            elif request in self.callback_dict:
                self.callback_dict[request](request, response)

    def run(self):
        self.pc.start()
        while not self._stop.isSet():
            self.forward_requests()
            self.callback_responses()
        # Kill the pc process if thread stopped
        self.pc.stop()<commit_msg>Prevent crash on KeyboardInterrupt<commit_after>from threading import Thread, Event
from Queue import Queue, Empty
from multiprocessing import Process
from packet_controller import PacketController
from time import sleep
from .. import config as c

class PacketForwarder(Thread):

    def __init__(self, reset_callback=None):
        super(PacketForwarder, self).__init__()
        self.__stop = Event()
        self.packets_received = 0 # float cuz this can get big
        self.sending_queue = Queue()
        self.callback_dict = dict()
        self.reset_callback = reset_callback
        self.pc = PacketController()
        self.pipe = self.pc.pipe_outside

    def stop(self):
        self.__stop.set()

    def empty_queue(self):
        while not self.sending_queue.empty():
            try: self.sending_queue.get(False)
            except Empty: pass

    def forward_requests(self):
        try:
            packet, callback = self.sending_queue.get(block=False)
            self.callback_dict[packet[:2]] = callback
            self.pipe.send(packet)
            self.sending_queue.task_done()
        except Empty:
            pass

    def callback_responses(self):
        while self.pipe.poll():
            request, response = self.pipe.recv()
            self.packets_received += 1
            if response == c.host.reset_msg: 
                self.empty_queue()
                if self.reset_callback: self.reset_callback()
            elif len(response) == 1 and response in c.responses:
                if response == 'G': continue
                print ("Firmware responded with an error: {} "
                       "for the request {}").format(
                            c.responses[response].msg, request)
            elif request in self.callback_dict:
                self.callback_dict[request](request, response)

    def run(self):
        self.pc.start()
        while not self.__stop.isSet():
            self.forward_requests()
            self.callback_responses()
        # Kill the pc process if thread stopped
        self.pc.stop()
{'fraction_non_alphanumeric': 0.08980582524271845, 'fraction_numerical': 0.001941747572815534, 'mean_word_length': 2.0435745937961594, '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': '7466587', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1071, 'n_words': 284}