Exemple #1
0
async def testing(group, student_num, file_name):
    q = queue.Queue()
    t = KThread(target=__testing__, args=(group, student_num, file_name, q))
    t.start()
    await asyncio.sleep(5)
    if t.is_alive():
        t.kill()
    return q.get()
        def the_wrapper_around_the_original_function(*args, **kwargs):
            from kthread import KThread
            thread = KThread(
                target=lambda: function_to_decorate(*args, **kwargs))
            thread.start()
            thread.join(timeout=seconds)

            if thread.is_alive():
                thread.kill()

                if raise_timeout:
                    raise TimeoutError()
Exemple #3
0
class TTS:
	def __init__(self):
		# Event Handlers
		self.queue = []
		# Threading and control
		self.BUSY = False
		self.thread = KThread(target=self.__process_creator)
		self.thread.start()
		self.OS = os.name

	# Adds speech to speech queue
	def speak(self, text):
		self.queue.append(text)

	# KTHREADED runs proccess initiator for speech
	def __process_creator(self):
		while(1):
			if(self.BUSY or self.queue == []):
				sleep(0.1)
				continue

			self.BUSY = True

			if(self.OS == 'nt'):
				l = ["powershell", "python", str(Path("src\\SpeechProcess.py")), str(self.queue.pop(0))]
				sp.call(l, universal_newlines=True)
			else:
				sp.check_output(["python3", str(Path("src\\SpeechProcess.py")), str(self.queue.pop(0))], universal_newlines=True)

			self.BUSY = False

	# Kills thread on deletion
	def delete(self):
		self.thread.kill()

	# Kills thread on deletion
	def close(self):
		self.thread.kill()
Exemple #4
0
        def _(*args, **kwargs):

            result = []

            new_kwargs = {  # create new args for _new_func, because we want to get the func return val to result list
                'oldfunc': func,
                'result': result,
                'oldfunc_args': args,
                'oldfunc_kwargs': kwargs
            }

            thd = KThread(target=_new_func, args=(), kwargs=new_kwargs)

            thd.start()

            thd.join(
                seconds
            )  # join(timeout) is for this thread blocked to wait its sub-thread timeout seconds

            alive = thd.isAlive(
            )  # isAlive() to check if sub-thread timeouts after timeout seconds

            thd.kill()  # kill the child thread

            if alive:
                # raise Timeout(u'function run too long, timeout %d seconds.' % seconds)
                try:

                    raise Timeout(
                        u'function run too long, timeout %d seconds.' %
                        seconds)
                finally:
                    return u'function run too long, timeout %d seconds.' % seconds

            else:

                return result[0]
Exemple #5
0
class Task:
    class WorkCancelException(Exception):
        pass

    class State(Enum):
        Stop = auto()
        Work = auto()
        Done = auto()
        Wait = auto()

    class Status(Enum):
        Pausing = auto()
        Resuming = auto()
        Cancelling = auto()
        Killing = auto()

    _ping_callback: Tuple[Callable[[], None]] = ((lambda: None), )
    _force_ping_callback: Tuple[Callable[[], None]] = ((lambda: None), )
    _workload: Tuple[Callable[[Callable[[], None], Callable[[float], None]],
                              Any]] = ((lambda p, u: (p(), u(1.0))), )
    _pause_lock: Lock
    _thread: Optional[KThread]

    status: Optional[Status]
    state: State
    progress: float

    def __init__(self) -> None:
        self._init()

    def _init(self):
        self.state = self.State.Stop
        self.status = None
        self._pause_lock = Lock()
        self._thread = None
        self.progress = 0.0

    def set_callback(self, ping: Callable[[], None],
                     force_ping: Callable[[], None]) -> None:
        self._ping_callback = (ping, )
        self._force_ping_callback = (force_ping, )

    def set_workload(
        self, workload: Callable[[Callable[[], None], Callable[[float], None]],
                                 None]
    ) -> None:
        """workload(ping: ()->Any, progress_update: (float)->None)
        If needed, catch exception WorkCancelException.
        Use ping() as often as u can.
        Dont forget progress_update with 0 .. 1.0"""
        self._workload = (workload, )

    def start(self):
        if self.state is self.State.Stop:
            self._thread = KThread(target=self._exec)
            self._thread.setDaemon(True)
            self._thread.start()

    def pause(self) -> None:
        if self.state is self.State.Work:
            if self.status is None:
                self.status = self.Status.Pausing
            self._force_ping_callback[0]()
            self._pause_lock.acquire()

    def resume(self) -> None:
        if self.state is self.State.Wait:
            if self.status is None:
                self.status = self.Status.Resuming
                self._force_ping_callback[0]()
            self._pause_lock.release()

    def cancel(self):
        if self.state in (self.State.Work, self.State.Wait):
            self.status = self.Status.Cancelling
            self._force_ping_callback[0]()
        self.resume()

    def finish(self):
        if self.state is self.State.Done:
            self.state = self.State.Stop
            self._init()

    def kill(self):
        if self.state not in (self.State.Stop, self.State.Done):
            self._thread.kill()
            self.status = self.Status.Killing
            self.resume()
            self._init()
            self._force_ping_callback[0]()

    def _exec(self):
        try:
            self.progress = 0.0
            self.state = self.State.Work
            self._workload[0](self._ping, self._update_progress)
            self.state = self.State.Done
        except self.WorkCancelException:
            self.state = self.State.Stop

        self._force_ping_callback[0]()

    def _ping(self) -> None:
        """Signalling, that task is alive an work still in progress.
        Used by workload implementation"""
        self._ping_callback[0]()
        self._check_pause()
        self._check_cancel()

    def _update_progress(self, progress: float):
        self.progress = progress

    def _check_pause(self) -> None:
        if self.status is self.Status.Cancelling:
            return

        if self._pause_lock.locked():
            assert self.status is self.Status.Pausing
            self.status = None
            self.state = self.State.Wait
            self._force_ping_callback[0]()

            self._pause_lock.acquire()
            self._pause_lock.release()

            self.state = self.State.Work
            if self.status is self.Status.Resuming:
                self.status = None
                self._force_ping_callback[0]()

    def _check_cancel(self) -> None:
        if self.status is self.Status.Cancelling:
            self.status = None
            raise self.WorkCancelException()
Exemple #6
0
class Lexer:
    def __init__(self, input_dim=20, auto_save=True, sleep_time=120):
        self.input_dim = input_dim
        self.thread_sleep = sleep_time
        self.__file_exists()
        self.auto_save = auto_save

        self.dict = self.__load()

        if (auto_save):
            self.thread = KThread(target=self.__auto_save)
            self.thread.start()

    # Main Lexer function to prepare input for Neural Network
    def transform(self, text):
        text = self.__prepare(text)
        words = text.split(" ")
        output = []
        zeros = 0

        # Trims max word count to input_dim
        if (len(words) > self.input_dim):
            words = words[:self.input_dim]
        else:
            zeros = self.input_dim - len(words)

        for w in words:
            if (w in self.dict.keys()):
                output.append(self.dict[w])
            else:
                self.dict[w] = len(self.dict.keys()) + 1
                output.append(self.dict[w])

        # Fills rest of input with NULL
        for i in range(0, zeros):
            output.append(0)

        return output

    # Removes punctuation of input text
    def __prepare(self, text):
        t = text.replace("!", "")
        t = t.replace(".", '')
        t = t.replace("?", "")
        return t

    # KTHREADED Lexer auto-save
    def __auto_save(self):
        while (1):
            for i in range(0, 60):
                sleep(self.thread_sleep / 60)

            with open(str(Path("src\\WordEmbeddings.txt")), "w") as f:
                json.dump(self.dict, f)

    # Saves Word Embeddings dictionary
    def save(self):
        try:
            with open(str(Path("src\\WordEmbeddings.txt")), "w") as f:
                json.dump(self.dict, f)
        except:
            raise IOError(str(Path("src\\WordEmbeddings.txt")))

    # Loads Word Embeddings dictionary
    def __load(self):
        try:
            with open(str(Path("src\\WordEmbeddings.txt")), "r") as f:
                data = json.load(f)

            return data
        except json.decoder.JSONDecodeError:
            data = {}
            return data
        except:
            raise IOError(str(Path("src\\WordEmbeddings.txt")))

    # Kills auto_save thread
    def delete(self):
        if (self.auto_save):
            self.thread.kill()
            self.save()

    # Check existance of Word Embeddings file
    def __file_exists(self):
        if (not os.path.isfile(str(Path("src\\WordEmbeddings.txt")))):
            file = open(str(Path("src\\WordEmbeddings.txt")), "w")
            file.close()
Exemple #7
0
class NetSocket:
    receive_threads = {}
    traffic_thread = None

    def __init__(self, t="DEALER"):
        self.type = t  # "DEALER" or "ROUTER"
        self.sock = socket(AF_INET, SOCK_STREAM)
        self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        self.port = 33000
        self.receive_buffer = []
        self.LOCK = Lock()
        self.ip_table = {}  # IP to Client Convertion

        if (self.type == "ROUTER"):
            self.ips = []
            self.clients = []

        if (t != "DEALER" and t != "ROUTER"):
            raise SocketTypeError(t)

    # Encrypt and decrypt message
    def __crypt(self, text):
        '''
		new = ""
		for e in text:
			new += chr(ord(e) ^ ord("B"))
		'''
        return text  #new

    # Turns message into bytes
    def __byt(self, text):
        return bytes(text, "UTF-8")

    # Sets connection or binding
    # Is persistent on DEALER connections
    def open_connection(self, ip):
        if (self.type == "DEALER"):
            CONNECTED = False

            # Persistent connection
            while (not CONNECTED):
                try:
                    self.sock.connect((ip, self.port))
                    CONNECTED = True
                except ConnectionRefusedError:
                    continue

            self.traffic_thread = KThread(target=self.handle_receives_dealer)
            self.traffic_thread.start()
        elif (self.type == "ROUTER"):
            self.sock.bind(("0.0.0.0", self.port))
            self.sock.listen(1)
            self.traffic_thread = KThread(target=self.accept_connections)
            self.traffic_thread.start()

    # Sends message to other connection
    def send(self, message, client=None):
        try:
            if (self.type == "DEALER"):
                self.sock.send(self.__byt(self.__crypt(message)))
            elif (self.type == "ROUTER"):
                ##### CHANGE LATER BECAUSE ROUTER CAN 'ROUTE' THINGS
                client.send(self.__byt(self.__crypt(message)))
        except:
            pass

    # Receives message from other connection
    def recv(self):
        try:
            while (1):
                if (self.type == "DEALER"):
                    if (self.receive_buffer == []):
                        self.LOCK.acquire()
                        continue

                    message = self.receive_buffer.pop(0)
                    return self.__crypt(message.decode())

                elif (self.type == "ROUTER"):

                    if (self.receive_buffer == []):
                        self.LOCK.acquire()
                        continue

                    message, client = self.receive_buffer.pop(0)
                    return (self.__crypt(message.decode()), client)
        except KeyboardInterrupt:
            self.close()

    # KTHREADED
    # Handles connection traffic
    def accept_connections(self):
        if (self.type == "ROUTER"):
            while (1):
                client, client_address = self.sock.accept()
                self.clients.append(client)
                self.ips.append(client_address)
                print(f"{client_address} Connected")
                print(len(self.clients))
                self.ip_table[client_address[0]] = client

                if (client in self.receive_threads.keys()):
                    self.receive_threads[client].kill()

                self.receive_threads[client] = KThread(
                    target=self.handle_receives_router, args=(client, ))
                self.receive_threads[client].start()

    # KTHREADED
    # Receives all data from clients and puts them on receive_buffer
    def handle_receives_dealer(self):
        try:
            while (1):
                message = self.sock.recv(4096)

                if (self.socket_control(message, None)):
                    continue

                self.receive_buffer.append(message)
                if (self.LOCK.locked()):
                    self.LOCK.release()
        except KeyboardInterrupt:
            return

    # KTHREADED
    # Receives all data from clients and puts them on receive_buffer
    def handle_receives_router(self, client):
        try:
            while (client in self.clients):
                message = client.recv(4096)
                if (self.socket_control(message, client)):
                    continue

                self.receive_buffer.append((message, client))
                if (self.LOCK.locked()):
                    self.LOCK.release()
        except ConnectionResetError:
            self.remove_connection(client)
            print(f"{client} Disconnected")

    '''
	This function handles all 'socket controlling' operations
	Add new if's to all new operations if needed
	'''

    def socket_control(self, message, client):
        if (message == "<CLOSING>" and self.type == "ROUTER"):
            self.clients.remove(client)
            if (self.LOCK.locked()):
                self.LOCK.release()
            print(f"{client} Disconnected")
            return True

        elif (message == "<CLOSING>" and self.type == "DEALER"):
            self.sock.shutdown(0)
            self.sock.close()
            return True

        return False

    # Totally severs connections
    def close(self):
        if (self.type == "DEALER"):
            self.sock.send(self.__byt(self.__crypt("<CLOSING>")))
            self.sock.shutdown(0)
            self.sock.close()
            self.traffic_thread.kill()
            if (self.LOCK.locked()):
                self.LOCK.release()

        elif (self.type == "ROUTER"):
            self.sock.shutdown(0)
            self.sock.close()
            self.ips = []
            self.clients = []
            self.traffic_thread.kill()
            for th in self.receive_threads.keys():
                self.receive_threads[th].kill()

    # Removes a connection from ROUTER sockets
    # Usually called on ConnectionResetError exception
    def remove_connection(self, client):
        if (self.type == "ROUTER"):
            self.clients.remove(client)
            self.receive_threads[client].kill()

    # Checks if a tuple of (eip, iip) exists in self.ip_table and
    # returns the correct index
    def ip_search(self, ips):
        for ip in ips:
            if (ip in self.ip_table.keys()):
                return ip
        return None
Exemple #8
0
class SpeechRecognition:
    exception_phrases = [
        "What?", "I did not understand", "What did you say?", "What was that?"
    ]

    def __init__(self, energy=2000, exception=False):
        self.client = sr.Recognizer()
        self.text = ""
        self.exception = exception

        # Adjust Noise
        #with sr.Microphone() as source:
        #	self.client.adjust_for_ambient_noise(source)

        self.client.energy_threshold = energy

        self.LOCK = Lock()
        self.EXIT = False
        self.thread = KThread(target=self.audio_thread)
        self.thread.start()

    # Blocks until audio thread gets a text
    def recv_text(self):
        try:
            while (1):
                if (self.EXIT):
                    return

                if (self.text == ""):
                    self.LOCK.acquire()
                    continue

                recognized_text = self.text
                self.text = ""
                recognized_text = self.__clean(recognized_text)

                return recognized_text
        except KeyboardInterrupt:
            self.close()
            raise SpeechRecognitionError()

    # KThreaded to recognize audio
    def audio_thread(self):
        while (1):
            try:
                with sr.Microphone() as source:
                    audio = self.client.listen(source)
                    try:
                        self.text = self.client.recognize_google(audio)
                        if (self.text == None):
                            if (self.exception):
                                self.text = self.__get_exception()
                            else:
                                continue
                    except:
                        try:
                            self.text = self.client.recognize_sphinx(audio)
                            if (self.text == None):
                                if (self.exception):
                                    self.text = self.__get_exception()
                                else:
                                    continue

                        except:
                            self.text = "Something is wrong with my Speech Recognition"

                    if (self.LOCK.locked()):
                        self.LOCK.release()
            except KeyboardInterrupt:
                self.close()
                raise SpeechRecognitionError()

    # Formats string to usable format
    def __clean(self, text):
        text = text.replace("\'s", " is")
        text = text.replace("n\'t", " not")
        return text

    # Closes all thread activity
    def close(self):
        self.thread.kill()
        self.EXIT = True

        if (self.LOCK.locked()):
            self.LOCK.release()

    # Gets a random exception phrase
    def __get_exception(self):
        return self.exception_phrases[randint(0,
                                              len(self.exception_phrases) - 1)]