def test_iter(self): inp = Input() inp.send = Mock() inp.send.return_value = None for i, e in zip(range(3), inp): self.assertEqual(e, None) self.assertEqual(inp.send.call_count, 3)
def test_event_trigger(self): inp = Input() f = inp.event_trigger(CustomEvent) self.assertEqual(inp.send(0), None) f() self.assertEqual(type(inp.send(0)), CustomEvent) self.assertEqual(inp.send(0), None)
def test_use_in_thread_with_sigint_event(self): inp = Input(sigint_event=True) def use(): with inp: pass t = threading.Thread(target=use) t.start() t.join()
def test_threadsafe_event_trigger(self): inp = Input() f = inp.threadsafe_event_trigger(CustomEvent) def check_event(): self.assertEqual(type(inp.send(1)), CustomEvent) self.assertEqual(inp.send(0), None) t = threading.Thread(target=check_event) t.start() f() t.join()
def test_schedule_event_trigger(self): inp = Input() f = inp.scheduled_event_trigger(CustomScheduledEvent) self.assertEqual(inp.send(0), None) f(when=time.time()) self.assertEqual(type(inp.send(0)), CustomScheduledEvent) self.assertEqual(inp.send(0), None) f(when=time.time()+0.01) self.assertEqual(inp.send(0), None) time.sleep(0.01) self.assertEqual(type(inp.send(0)), CustomScheduledEvent) self.assertEqual(inp.send(0), None)
def test_interrupting_sigint(self): inp = Input(sigint_event=True) def send_sigint(): os.kill(os.getpid(), signal.SIGINT) with inp: t = threading.Thread(target=send_sigint) t.start() self.assertEqual(type(inp.send(1)), events.SigIntEvent) self.assertEqual(inp.send(0), None) t.join()
def test_send_paste(self): inp = Input() inp.unprocessed_bytes = [] inp._wait_for_read_ready_or_timeout = Mock() inp._wait_for_read_ready_or_timeout.return_value = (True, None) inp._nonblocking_read = Mock() n = inp.paste_threshold + 1 first_time = [True] def side_effect(): if first_time: inp.unprocessed_bytes.extend([b'a']*n) first_time.pop() return n else: return None inp._nonblocking_read.side_effect = side_effect r = inp.send(0) self.assertEqual(type(r), events.PasteEvent) self.assertEqual(r.events, [u'a'] * n)
def test_schedule_event_trigger_blocking(self): inp = Input() f = inp.scheduled_event_trigger(CustomScheduledEvent) f(when=time.time()+0.05) self.assertEqual(type(next(inp)), CustomScheduledEvent)
def test_nonblocking_read(self): inp = Input() self.assertEqual(inp._nonblocking_read(), 0)
def test_send_nonblocking_no_event(self): inp = Input() inp.unprocessed_bytes = [] self.assertEqual(inp.send(0), None)
def test_send(self): inp = Input() inp.unprocessed_bytes = [b'a'] self.assertEqual(inp.send('nonsensical value'), u'a')
def test_create(self): Input()
def test_send(self): inp = Input() inp.unprocessed_bytes = [b"a"] self.assertEqual(inp.send("nonsensical value"), "a")
def create(): inp = Input(sigint_event=True)
def managing_thread_main(): import shutit_global from shutit_global import SessionPaneLine shutit_global.shutit_global_object.global_thread_lock.acquire() shutit_module_paths = gather_module_paths() shutit_global.shutit_global_object.global_thread_lock.release() shutit_global.shutit_global_object.stacktrace_lines_arr = [SessionPaneLine('',time.time(),'log'),] last_code = [] draw_type = 'default' zoom_state = None while True: # We have acquired the lock, so read in input with Input() as input_generator: input_char = input_generator.send(0.001) if input_char == 'r': # Rotate sessions at the bottom shutit_global.shutit_global_object.lower_pane_rotate_count += 1 elif input_char == '1': if zoom_state == 1: draw_type = 'default' zoom_state = None else: draw_type = 'zoomed1' zoom_state = 1 elif input_char == '2': if zoom_state == 2: draw_type = 'default' zoom_state = None else: draw_type = 'zoomed2' zoom_state = 2 elif input_char == '3': if zoom_state == 3: draw_type = 'default' zoom_state = None else: draw_type = 'zoomed3' zoom_state = 3 elif input_char == '4': if zoom_state == 4: draw_type = 'default' zoom_state = None else: draw_type = 'zoomed4' zoom_state = 4 elif input_char == 'q': draw_type = 'clearscreen' shutit_global.shutit_global_object.pane_manager.draw_screen(draw_type=draw_type) os.system('reset') os._exit(1) # Acquire lock to write screen. Prevents nasty race conditions. # Different depending PY2/3 if shutit_global.shutit_global_object.ispy3: if not shutit_global.shutit_global_object.global_thread_lock.acquire(blocking=False): time.sleep(0.01) continue else: if not shutit_global.shutit_global_object.global_thread_lock.acquire(False): time.sleep(0.01) continue code = [] for thread_id, stack in sys._current_frames().items(): # ignore own thread: if thread_id == threading.current_thread().ident: continue for filename, lineno, name, line in traceback.extract_stack(stack): # if the file is in the same folder or subfolder as a folder in: self.host['shutit_module_path'] # then show that context for shutit_module_path in shutit_module_paths: if filename.find(shutit_module_path) == 0: if len(shutit_global.shutit_global_object.stacktrace_lines_arr) == 0 or shutit_global.shutit_global_object.stacktrace_lines_arr[-1] != line: linearrow = '===> ' + str(line) code.append('_' * 80) code.append('=> %s:%d:%s' % (filename, lineno, name)) code.append('%s' % (linearrow,)) from_lineno = lineno - 5 if from_lineno < 0: from_lineno = 0 to_lineno = 10 else: to_lineno = lineno + 5 lineno_count = from_lineno with open(filename, "r") as f: for line in itertools.islice(f, from_lineno, to_lineno): line = line.replace('\t',' ') lineno_count += 1 if lineno_count == lineno: code.append('***' + str(lineno_count) + '> ' + line.rstrip()) else: code.append('===' + str(lineno_count) + '> ' + line.rstrip()) code.append('_' * 80) if code != last_code: for line in code: shutit_global.shutit_global_object.stacktrace_lines_arr.append(SessionPaneLine(line,time.time(),'log')) last_code = code shutit_global.shutit_global_object.pane_manager.draw_screen(draw_type=draw_type) shutit_global.shutit_global_object.global_thread_lock.release()