class MainWindow(QMainWindow): def __init__(self, settings, listener): super().__init__() self.settings = settings self.listener = listener self.setWindowTitle('Eule.py') self.table_widget = TableWidget(self) self.setCentralWidget(self.table_widget) self.locate_to_center() self.status_thread = KThread(target=self.set_status) self.status_thread.start() def locate_to_center(self): qtRectangle = self.frameGeometry() centerPoint = QDesktopWidget().availableGeometry().center() qtRectangle.moveCenter(centerPoint) self.move(qtRectangle.topLeft()) def set_status(self): while True: self.table_widget.main_page_tab.diablo_hooked.setChecked( win32gui.FindWindow('D3 Main Window Class', 'Diablo III')) sleep(1) def closeEvent(self, event): self.settings.save() self.listener.thread.terminate() self.listener.image_recognition_thread.terminate() self.status_thread.terminate() sys.stdout = sys.__stdout__ super().closeEvent(event)
def start(self): total = len(self.data) all_thread_finished = True while self.data or not all_thread_finished: print('[{0}/{1}]'.format(total - len(self.data), total)) all_thread_finished = True for i in range(self.a): d, t, s = self.td[i] if t and t.isAlive(): if self.kc(d, s): t.terminate() print('thread killed!!!') self.data.append(d) else: all_thread_finished = False continue elif t: t.join() self.os(d) if not self.data: continue # else new thread nd = self.data.pop(0) nt = KThread(target=self.dp, args=(nd, )) ns = time() self.td[i] = (nd, nt, ns) nt.start() sleep(2)
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()
class DefaultKThreadTestCase(unittest.TestCase): def setUp(self): self.kthread = KThread(name = "DefaultKThreadTestThread", target = infinite_loop_func) def runTest(self): self.kthread.start() self.assertTrue(self.kthread.isAlive(), "KThread failed to start") # let it run for some time time.sleep(5) self.kthread.terminate() # give it time to process exception time.sleep(1) self.assertFalse(self.kthread.isAlive(), "KThread failed to stop") def tearDown(self): self.kthread.join() self.kthread = None
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()
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]
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()
class Listener: def __init__(self, settings): self.settings = settings self.thread = KThread(target=lambda: keyboard.wait('a+r+b+i+t+r+a+r+y')) self.thread.start() self.image_recognition_thread = KThread(target=self.watch_screen) self.image_recognition_thread.start() self.start() def start(self): self.paused = False hotkeys = self.settings.hotkeys special = self.settings.special abbrevations = self.settings.abbrevations if hotkeys['right_click']: keyboard.add_hotkey( hotkeys['right_click'], macros.right_click, args=(hotkeys['right_click'],), suppress=True, ) if hotkeys['left_click']: keyboard.add_hotkey( hotkeys['left_click'], macros.left_click, args=(hotkeys['left_click'],), suppress=True, ) if hotkeys['lower_difficulty']: keyboard.add_hotkey( hotkeys['lower_difficulty'], macros.lower_difficulty, suppress=True ) if hotkeys['swap_armor']: keyboard.add_hotkey( hotkeys['swap_armor'], macros.swap_armor, args=(special['armor_swap_amount'],), suppress=True, ) if hotkeys['pause']: keyboard.add_hotkey(hotkeys['pause'], self.pause, suppress=True) if hotkeys['port_a1']: keyboard.add_hotkey( hotkeys['port_a1'], macros.port_town, args=(1,), suppress=True ) if hotkeys['port_a2']: keyboard.add_hotkey( hotkeys['port_a2'], macros.port_town, args=(2,), suppress=True ) if hotkeys['port_a3']: keyboard.add_hotkey( hotkeys['port_a3'], macros.port_town, args=(3,), suppress=True ) if hotkeys['port_a4']: keyboard.add_hotkey( hotkeys['port_a4'], macros.port_town, args=(4,), suppress=True ) if hotkeys['port_a5']: keyboard.add_hotkey( hotkeys['port_a5'], macros.port_town, args=(5,), suppress=True ) if hotkeys['port_pool']: keyboard.add_hotkey( hotkeys['port_pool'], macros.port_pool, args=(PoolSpotList(self.settings.poolspots),), suppress=True, ) if hotkeys['open_gr']: keyboard.add_hotkey(hotkeys['open_gr'], macros.open_gr, suppress=True) if hotkeys['upgrade_gem']: keyboard.add_hotkey( hotkeys['upgrade_gem'], macros.upgrade_gem, args=(special['empowered'], special['choose_gem']), ) if hotkeys['leave_game']: keyboard.add_hotkey(hotkeys['leave_game'], macros.leave_game, suppress=True) if hotkeys['salvage']: keyboard.add_hotkey( hotkeys['salvage'], macros.salvage, args=(special['spare_columns'],), suppress=True, ) if hotkeys['drop_inventory']: keyboard.add_hotkey( hotkeys['drop_inventory'], macros.drop_inventory, args=(special['spare_columns'],), suppress=True, ) if hotkeys['gamble']: keyboard.add_hotkey( hotkeys['gamble'], macros.gamble, args=(special['gamble_item'],), suppress=True, ) if hotkeys['cube_conv_sm']: keyboard.add_hotkey( hotkeys['cube_conv_sm'], macros.cube_conv_sm, args=(special['cube_conv_speed'],), ) if hotkeys['cube_conv_lg']: keyboard.add_hotkey( hotkeys['cube_conv_lg'], macros.cube_conv_lg, args=(special['cube_conv_speed'],), ) if hotkeys['reforge']: keyboard.add_hotkey(hotkeys['reforge'], macros.reforge, suppress=True) if hotkeys['skill_macro']: active_macro = self.settings.skill_macro['profiles'][ self.settings.skill_macro['active'] ] print(active_macro['name']) keyboard.add_hotkey( hotkeys['skill_macro'], macros.skill_macro, args=(active_macro['hotkeys'], active_macro['delays']), suppress=True, ) if self.settings.special['abbrevations_enabled']: for abbrevation, msg in abbrevations.items(): keyboard.add_abbreviation(abbrevation, msg) def stop(self): keyboard.unhook_all() def pause(self): if self.paused: keyboard.unhook_all() self.start() self.gui_paused.setChecked(False) p = os.path.join(wd, './Compiled/active.png').replace('\\', '/') self.status_image.setStyleSheet( "background-image: url(" + p + ");" + "background-repeat: no-repeat;" + "background-position: center;" ) else: self.paused = True self.stop() keyboard.add_hotkey( self.settings.hotkeys['pause'], self.pause, suppress=True ) self.gui_paused.setChecked(True) p = os.path.join(wd, './Compiled/inactive.png').replace('\\', '/') self.status_image.setStyleSheet( f"background-image: url(" + p + ");" + "background-repeat: no-repeat;" + "background-position: center;" ) [t.stop() for t in macros.timers] macros.timers = [] macros.is_running = False def watch_screen(self): while True: handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III') if ( handle and win32gui.GetForegroundWindow() == handle and any( ( self.settings.special['auto_start'], self.settings.special['auto_open'], self.settings.special['auto_gamble'], self.settings.special['auto_upgrade_gem'], self.settings.special['auto_accept_gr'], ) ) and not self.paused ): try: screenshot = get_image(handle) if self.settings.special['auto_start']: Thread( target=screen_search.start_game, args=(screenshot, handle) ).start() if self.settings.special['auto_open']: Thread( target=screen_search.open_rift, args=( screenshot, handle, self.settings.special['auto_open_option'], ), ).start() if self.settings.special['auto_gamble']: Thread( target=screen_search.gamble, args=( screenshot, handle, self.settings.special['gamble_item'], ), ).start() if self.settings.special['auto_accept_gr']: Thread( target=screen_search.accept_gr, args=(screenshot, handle) ).start() if self.settings.special['auto_upgrade_gem']: Thread( target=screen_search.upgrade_gem, args=(screenshot, handle) ).start() except: print('Handle not found or sth') sleep(0.3)
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()
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
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)]