def apply(self): try: self.new_user_settings.file_delimiter = self.Set_delimiter_edit.text( ) self.new_user_settings.max_num_graphs = self.Set_max_graphs_edit.text( ) self.new_user_settings.dfactor_file_head = self.Set_df_file_head_cbox.isChecked( ) self.new_user_settings.language = self.Set_language_box.currentText( ) self.new_settings.divided_n = self.Set_divided_n_cbox.isChecked() self.new_settings.braking_dist = self.Set_breaking_dist_edit.text() self.new_settings.threshold = self.Set_threshold_edit.text() self.new_settings.check_period = self.Set_check_period_edit.text() self.new_settings.max_step = self.Set_max_step_edit.text() check_s, info_s = self.new_settings.check_settings() check_us, info_us = self.new_user_settings.check_settings() if check_s and check_us: self.new_user_settings.write_user_settings() self.parent_w.settings = Settings(self.new_settings) self.parent_w.user_settings = UserSettings( self.parent_w.deep_settings, self.new_user_settings) self.parent_w.full_update() self.set_text() self.update_btn_box() self.fill_boxes() return True else: message = "" for i in info_s: message += self.parent_w.get_message_text(i) + "\n" for i in info_us: message += self.parent_w.get_message_text(i) + "\n" title = "Settings_error" title = self.parent_w.lang_parser.get( self.parent_w.user_settings.language.upper(), title, fallback=title) msg_window = Message(message, title, self.parent_w) msg_window.exec_() return False except Exception as e: msg = traceback.format_exc() logger.error("Unknown error", exc_info=True) emsg = ErrorMessage(message=msg, parent_w=self.parent_w) emsg.exec_() raise SystemExit(1)
def _send(self): ip = self.server.client_info.ip recipient, text = self._get_recipient() content = self.server.client_info.name + ': ' + text message = Message(ip, content, Mode.Normal, recipient) self.server.add_new_message(message) self._input.setText('')
def _read_file_content(self, filename: str) -> Message: user_message = None file_object = File(filename=filename, charset=self._charset, chunk_size=self._chunk_size) file_extension = file_object.get_file_extension() if self._ignored_extensions is not None and file_extension is not None and file_extension in self._ignored_extensions: # The user wants to ignore this type of files! # So we will ignore the rest of the code. return if self._encryption: str_aux = 'Do you want to store a message inside the {} encrypted file?'.format( filename) + self._formatter.orange_foreground(' [Yes, No]:') insert_message_inside = self._io.read_ask_answear(str_aux) if insert_message_inside: user_message = self._io.stdin( "Insert the message to store inside: ") message = file_object.read() del file_object return Message(content=message, user_message=user_message, file_path=filename)
def send(self, message): data = Message.to_bytes(message) data = self.server.cryptographer.encrypt(data, message.to) if not data: return self.socket.write(bytes(str(len(data)) + '\n', encoding='utf-8')) self.socket.write(data)
def _remove_dead_connection(self, client): self.connections.pop(client.ip) if client.ip in self.online: self.chat_window.say_he_is_offline(self.online[client.ip].name) self.online.pop(client.ip) message = Message(self.get_ip(), client.ip, Mode.Offline) self.send_all(message) self.change_connections_cnt.emit() self.chat_window.refresh_online_and_connections(self.online, self.connections)
def setUp(self): # arrange self.ip = '127.0.0.1' self.port = 666 with patch('source.server.Server') as mock: self.server = Server('dude', 666, mock) self.client = Client(self.ip, self.port, QTcpSocket(), self.server) self.server.peer_manager.last_client = self.client self.online = {self.ip: self.server.client_info} self.message = Message(self.ip, self.online, Mode.Neighb)
def _send_file(self): filename = self._select_file() if not filename: return file = File(filename) ip = self.server.client_info.ip recipient, text = self._get_recipient() message = Message(ip, file, Mode.File, recipient) self.server.add_new_message(message) self._input.setText('')
def add_next_client(self): connection = self.nextPendingConnection() ip = connection.peerAddress().toString()[7:] port = connection.peerPort() client = Client(ip, port, connection, self) if ip in self.connections: connection.disconnectFromHost() return client.send(Message(self.get_ip(), self.online, Mode.Neighb)) self.add_client(client)
def _share_data(self): self.last_client.socket.connected.disconnect(self._share_data) if len(self.server.connections) >= OPTIMAL_CONNECT_NUMBER: self.last_client.socket.disconnectFromHost() return if self.last_client.ip in self.server.connections: self.last_client.socket.disconnectFromHost() return message = Message(self.server.get_ip(), self.server.online, Mode.Neighb) self.last_client.send(message)
def try_get_message(self, bytes): keys = ['all', self.server.client_info.name] for key in keys: decrypted = self.server.cryptographer.decrypt(bytes, key) try: return Message.from_bytes(decrypted) except OverflowError: continue except PickleError: continue return None
def test_add_message_new_will_be_added(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) message = Message('127.0.0.1', 'good news', Mode.Normal) # act server.add_new_message(message) # assert self.assertTrue(MessageInfo(message) in server.stored_messages)
def test_update_online_when_new_ip(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) ip = '127.0.0.1' message = Message(ip, server.client_info, Mode.Online) # act server.update_online(message) # assert self.assertEqual(server.online[ip], server.client_info)
def test_remove_from_online(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) ip = '127.0.0.1' server.online[ip] = server.client_info message = Message(ip, ip, Mode.Offline) # act server.remove_from_online(message) # assert self.assertFalse(ip in server.online)
def test_merge_online_when_new_ip(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) ip = '127.0.0.1' online = {ip: server.client_info} message = Message(ip, online, Mode.Neighb) # act server.merge_online(message) # assert self.assertTrue(ip in server.online)
def test_update_online_when_actual_info(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) ip = '127.0.0.1' old_client_info = ClientInfo('dude', ip, len(server.connections)) time.sleep(1) new_client_info = ClientInfo('dude', ip, len(server.connections)) server.online[ip] = old_client_info message = Message(ip, new_client_info, Mode.Online) # act server.update_online(message) # assert self.assertNotEqual(server.online[ip], old_client_info)
def test_merge_online_when_actual_info(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) ip = server.client_info.ip old_client_info = ClientInfo('dude', ip, len(server.connections)) time.sleep(1) new_client_info = ClientInfo('dude', ip, len(server.connections)) server.online[ip] = old_client_info online = {server.client_info.ip: new_client_info} message = Message(ip, online, Mode.Neighb) # act server.merge_online(message) # assert self.assertEqual(server.online[ip], new_client_info)
def _read_text(self) -> Message: message = None user_message = None if self._encryption: message = self._io.stdin_to_bytes(' Insert the message: \t', self._charset) str_ask = ' Do you want to store a message inside the encrypted file?' + self._formatter.orange_foreground( ' [Yes, No]:') insert_message_inside = self._io.read_ask_answear(str_ask) if insert_message_inside: user_message = self._io.stdin( " Insert the message to store inside: ") else: message = self._io.stdin_to_bytes( ' Insert the encrypted message: \t', self._charset) return Message(content=message, user_message=user_message)
def test_add_message_old_wont_call_has_new_message(self): # arrange with patch('source.server.Server') as mock: server = Server('dude', 666, mock) message = Message('127.0.0.1', 'good news', Mode.Normal) server.stored_messages.add(MessageInfo(message)) flag = False def change_flag(): nonlocal flag flag = not flag server.has_new_message.connect(change_flag) # act server.add_new_message(message) # assert self.assertFalse(flag)
def update_client_info(self): self.client_info.update_incidents_cnt(len(self.connections)) ip = self.get_ip() message = Message(ip, self.client_info, Mode.Online) self.send_all(message) self.chat_window.refresh_online_and_connections(self.online, self.connections)