diff --git a/.coveragerc b/.coveragerc index e9bb799..281a4ad 100644 --- a/.coveragerc +++ b/.coveragerc @@ -8,7 +8,7 @@ exclude_lines = self.fail\(.*\) except KeyboardInterrupt: except ImportError: - pragma: no cover + skip coverage include = ./* omit = diff --git a/simulators/acu/__init__.py b/simulators/acu/__init__.py index 6e009b6..0b8abd1 100644 --- a/simulators/acu/__init__.py +++ b/simulators/acu/__init__.py @@ -76,9 +76,9 @@ def __init__(self, sampling_time=default_sampling_time): self.command_threads = Queue() self.status = Array(c_char, 813) - self.status[0:4] = bytes(start_flag, 'raw_unicode_escape') + self.status[0:4] = bytes(start_flag, 'latin-1') self.status[4:8] = utils.uint_to_bytes(813) - self.status[-4:] = bytes(end_flag, 'raw_unicode_escape') + self.status[-4:] = bytes(end_flag, 'latin-1') subsystems = [] subsystems.append(self.PS.update_status) @@ -137,7 +137,7 @@ def system_stop(self): command_thread.join() except Empty: break - return '$server_shutdown%%%%%' + return super().system_stop() def _set_default(self): """This method resets the received command string to its default value. diff --git a/simulators/acu/axis_status.py b/simulators/acu/axis_status.py index 30981c7..9fabc50 100644 --- a/simulators/acu/axis_status.py +++ b/simulators/acu/axis_status.py @@ -1550,7 +1550,7 @@ def _validate_mode_command(self, mode_id, parameter_1, parameter_2): elif mode_id in [3, 4, 5, 7, 8, 52] and axis_state != 3: received_command_answer = 4 elif mode_id == 15 and axis_state not in [0, 1]: - received_command_answer = 4 # pragma: no cover + received_command_answer = 4 # skip coverage elif mode_id == 50: if not self.stowPosOk: received_command_answer = 4 diff --git a/simulators/acu/pointing_status.py b/simulators/acu/pointing_status.py index be34b5a..abe5cc9 100644 --- a/simulators/acu/pointing_status.py +++ b/simulators/acu/pointing_status.py @@ -6,12 +6,12 @@ from datetime import datetime, timedelta try: import numpy as np -except ImportError as ex: # pragma: no cover +except ImportError as ex: # skip coverage raise ImportError('The `numpy` package, required for the simulator' + ' to run, is missing!') from ex try: from scipy import interpolate -except ImportError as ex: # pragma: no cover +except ImportError as ex: # skip coverage raise ImportError('The `scipy` package, required for the simulator' + ' to run, is missing!') from ex from simulators import utils diff --git a/simulators/gaia/__init__.py b/simulators/gaia/__init__.py index a03796d..0132d8b 100644 --- a/simulators/gaia/__init__.py +++ b/simulators/gaia/__init__.py @@ -170,10 +170,7 @@ def _execute(self, msg): def _error(self, error_code): error_string = self.errors.get(error_code) - hex_string = codecs.encode( - error_string.encode('raw_unicode_escape'), - 'hex' - ) + hex_string = codecs.encode(error_string.encode('latin-1'), 'hex') retval = f'{self.header}ERROR({error_code})[{error_string}]' retval += f'({hex_string}) {self.cmd_id}{self.tail}' return retval diff --git a/simulators/minor_servos/__init__.py b/simulators/minor_servos/__init__.py index 5393ba5..51d9372 100644 --- a/simulators/minor_servos/__init__.py +++ b/simulators/minor_servos/__init__.py @@ -4,12 +4,12 @@ import os try: from numpy import sign -except ImportError as ex: # pragma: no cover +except ImportError as ex: # skip coverage raise ImportError('The `numpy` package, required for the simulator' + ' to run, is missing!') from ex try: from scipy.interpolate import splrep, splev -except ImportError as ex: # pragma: no cover +except ImportError as ex: # skip coverage raise ImportError('The `scipy` package, required for the simulator' + ' to run, is missing!') from ex from ctypes import c_bool, c_int @@ -84,7 +84,7 @@ def __init__(self): self.control = 1 self.power = 1 self.emergency = 2 - self.gregorian_cap = Value(c_int, 0) + self.gregorian_cap = Value(c_int, 1) self.last_executed_command = 0 self.servos = { 'PFP': PFP(), @@ -118,7 +118,7 @@ def system_stop(self): self.cover_timer.join() except RuntimeError: pass - return '$server_shutdown%%%%%' + return super().system_stop() @staticmethod def _update(stop, servos): diff --git a/simulators/mscu/__init__.py b/simulators/mscu/__init__.py index c09c6e3..eefd173 100644 --- a/simulators/mscu/__init__.py +++ b/simulators/mscu/__init__.py @@ -25,7 +25,7 @@ def __del__(self): def system_stop(self): for servo in self.servos.values(): servo.stop() - return '$server_shutdown%%%%%' + return super().system_stop() def system_setpos_NAK(self): self.setpos_NAK[1].value = True # The SRP address diff --git a/simulators/server.py b/simulators/server.py index e6d4aed..3e4656f 100644 --- a/simulators/server.py +++ b/simulators/server.py @@ -57,11 +57,10 @@ def _execute_custom_command(self, msg_body): else: params = () try: - method = getattr(self.system, name) - response = method(*params) + response = getattr(self.system, name)(*params) if isinstance(response, str): self.socket.sendto( - response.encode('raw_unicode_escape'), + response.encode('latin-1'), self.client_address ) if response == '$server_shutdown%%%%%': @@ -83,7 +82,7 @@ def setup(self): greet_msg = self.system.system_greet() if greet_msg: self.socket.sendto( - greet_msg.encode('raw_unicode_escape'), + greet_msg.encode('latin-1'), self.client_address ) else: # UDP client @@ -101,16 +100,14 @@ def handle(self): scenario (i.e. some error condition).""" if not self.connection_oriented: # UDP client msg, self.socket = self.socket - msg = msg.decode('raw_unicode_escape') - msg += '\n' + msg += b'\n' self._handle(msg) else: # TCP client while True: try: - msg = self.socket.recv(1) + msg = self.socket.recv(1024) if not msg: break - msg = msg.decode('raw_unicode_escape') self._handle(msg) except IOError: break @@ -126,6 +123,7 @@ def _handle(self, msg): """ response = None for byte in msg: + byte = chr(byte) try: response = self.system.parse(byte) except ValueError as ex: @@ -136,9 +134,9 @@ def _handle(self, msg): pass elif response and isinstance(response, str): try: - response = response.encode('raw_unicode_escape') + response = response.encode('latin-1') self.socket.sendto(response, self.client_address) - except IOError: # pragma: no cover + except IOError: # skip coverage # Something went wrong while sending the response, # probably the client was stopped without closing # the connection @@ -178,14 +176,14 @@ def handle(self): while True: try: if msg: - custom_msg = msg.decode('raw_unicode_escape') + custom_msg = msg msg = None else: custom_msg = self.socket.recv(1024) - custom_msg = custom_msg.decode('raw_unicode_escape') # Check if the client is sending a custom command if not custom_msg: break + custom_msg = custom_msg.decode('latin-1') if ( custom_msg.startswith(self.custom_header) and custom_msg.endswith(self.custom_tail) @@ -387,15 +385,14 @@ def _send_stop(entry): pass sockobj.sendto(b'$system_stop%%%%%', address) response = sockobj.recv(1024) - response = response.decode('raw_unicode_escape') - if response != '$server_shutdown%%%%%': # pragma: no cover + if response != b'$server_shutdown%%%%%': # skip coverage logging.warning( '%s %s %s', 'The server did not answer with the', '$server_shutdown%%%%% string!', 'The simulator might still be running!' ) - except Exception as ex: # pragma: no cover + except Exception as ex: # skip coverage logging.debug(ex) finally: sockobj.close() diff --git a/simulators/totalpower/__init__.py b/simulators/totalpower/__init__.py index 13c4d4f..1295716 100644 --- a/simulators/totalpower/__init__.py +++ b/simulators/totalpower/__init__.py @@ -342,7 +342,7 @@ def _get_status(self, ascii_format=False): status += '111' status = binary_to_string(status) if not ascii_format: - return status.encode('raw_unicode_escape') + return status.encode('latin-1') else: return ''.join([hex(ord(c))[-2:] for c in status[::-1]]) diff --git a/simulators/utils.py b/simulators/utils.py index 056fb5f..2741011 100644 --- a/simulators/utils.py +++ b/simulators/utils.py @@ -198,7 +198,7 @@ def binary_to_string(binary_string, little_endian=True): return binary_to_bytes( binary_string, little_endian - ).decode('raw_unicode_escape') + ).decode('latin-1') def bytes_to_int(byte_string, little_endian=True): @@ -235,7 +235,7 @@ def string_to_int(string, little_endian=True): >>> string_to_int('hello', False) 448378203247 """ - return bytes_to_int(string.encode('raw_unicode_escape'), little_endian) + return bytes_to_int(string.encode('latin-1'), little_endian) def bytes_to_binary(byte_string, little_endian=True): @@ -277,7 +277,7 @@ def string_to_binary(string, little_endian=True): >>> string_to_binary('hi', little_endian=False) '0110100001101001' """ - return bytes_to_binary(string.encode('raw_unicode_escape'), little_endian) + return bytes_to_binary(string.encode('latin-1'), little_endian) def bytes_to_uint(byte_string, little_endian=True): @@ -311,7 +311,7 @@ def string_to_uint(string, little_endian=True): >>> string_to_uint('hi', little_endian=False) 26729 """ - return bytes_to_uint(string.encode('raw_unicode_escape'), little_endian) + return bytes_to_uint(string.encode('latin-1'), little_endian) def real_to_binary(num, precision=1): @@ -403,7 +403,7 @@ def real_to_string(num, precision=1, little_endian=True): binary_number = real_to_binary(num, precision) binary_number = binary_to_bytes(binary_number, little_endian=little_endian) - return binary_number.decode('raw_unicode_escape') + return binary_number.decode('latin-1') def bytes_to_real(bytes_real, precision=1, little_endian=True): @@ -459,7 +459,7 @@ def string_to_real(string_real, precision=1, little_endian=True): >>> round(string_to_real('\x40\x7A\x25\x7D\x2E\x68\x51\x5D', 2, False), 2) 418.34 """ - bytes_real = bytes(string_real, 'raw_unicode_escape') + bytes_real = bytes(string_real, 'latin-1') return bytes_to_real(bytes_real, precision, little_endian) @@ -505,7 +505,7 @@ def int_to_string(val, n_bytes=4, little_endian=True): val, n_bytes, little_endian - ).decode('raw_unicode_escape') + ).decode('latin-1') def uint_to_bytes(val, n_bytes=4, little_endian=True): @@ -558,7 +558,7 @@ def uint_to_string(val, n_bytes=4, little_endian=True): val, n_bytes, little_endian - ).decode('raw_unicode_escape') + ).decode('latin-1') def sign(number): diff --git a/tests/test_acu.py b/tests/test_acu.py index fa4166f..56c1068 100644 --- a/tests/test_acu.py +++ b/tests/test_acu.py @@ -2215,8 +2215,8 @@ class TestACUSimulator(unittest.TestCase): @classmethod def setUpClass(cls): - cls.start_flag = '\x1A\xCF\xFC\x1D' - cls.end_flag = '\xD1\xCF\xFC\xA1' + cls.start_flag = b'\x1A\xCF\xFC\x1D' + cls.end_flag = b'\xD1\xCF\xFC\xA1' cls.simulator = Simulator('acu') cls.simulator.start(daemon=True) @@ -2230,7 +2230,6 @@ def test_different_statuses(self): prev = '' for _ in range(5): status = s.recv(1024) - status = status.decode('raw_unicode_escape') self.assertEqual(len(status), 813) self.assertEqual(status[0:4], self.start_flag) self.assertEqual(status[-4:], self.end_flag) diff --git a/tests/test_minor_servos.py b/tests/test_minor_servos.py index fa8190d..bcc8bcd 100644 --- a/tests/test_minor_servos.py +++ b/tests/test_minor_servos.py @@ -33,7 +33,7 @@ def test_status(self): regex += r'CONTROL=1\|' regex += r'POWER=1\|' regex += r'EMERGENCY=2\|' - regex += r'GREGORIAN_CAP=0\|' + regex += r'GREGORIAN_CAP=1\|' regex += r'LAST_EXECUTED_COMMAND=0' regex += fr'{tail}$' for byte in cmd[:-1]: diff --git a/tests/test_server.py b/tests/test_server.py index 58bc524..5fc24fc 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -63,32 +63,32 @@ def test_wrong_greet_msg(self): with self.assertRaises(ValueError): get_response( self.address, - greet_msg='Wrong greet message!', + greet_msg=b'Wrong greet message!', ) def test_proper_request(self): response = get_response( self.address, - greet_msg='This is a greeting message!', - msg='#command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#command:a,b,c%%%%%' ) - self.assertEqual(response, 'aabbcc') + self.assertEqual(response, b'aabbcc') def test_wrong_request(self): """Wrong request but expected by the protocol""" response = get_response( self.address, - greet_msg='This is a greeting message!', - msg='#wrong_command:foo%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#wrong_command:foo%%%%%' ) - self.assertRegex(response, 'you sent a wrong command') + self.assertRegex(response, b'you sent a wrong command') def test_value_error(self): """The message of ValueError in the logfile""" get_response( self.address, - greet_msg='This is a greeting message!', - msg='#valueerror:%%%%%', + greet_msg=b'This is a greeting message!', + msg=b'#valueerror:%%%%%', response=False ) self.assertIn('unexpected value', get_logs()) @@ -96,8 +96,8 @@ def test_value_error(self): def test_unexpected_error(self): get_response( self.address, - greet_msg='This is a greeting message!', - msg='#unexpected:%%%%%', + greet_msg=b'This is a greeting message!', + msg=b'#unexpected:%%%%%', response=False ) self.assertIn('unexpected exception', get_logs()) @@ -105,8 +105,8 @@ def test_unexpected_error(self): def test_unexpected_response(self): get_response( self.address, - greet_msg='This is a greeting message!', - msg='#unexpected_response:%%%%%', + greet_msg=b'This is a greeting message!', + msg=b'#unexpected_response:%%%%%', response=False ) self.assertIn('unexpected response: 0.0', get_logs()) @@ -114,18 +114,18 @@ def test_unexpected_response(self): def test_custom_command_with_parameters(self): response = get_response( self.address, - greet_msg='This is a greeting message!', - msg='$custom_command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$custom_command:a,b,c%%%%%' ) - self.assertRegex(response, 'ok_abc') + self.assertRegex(response, b'ok_abc') def test_custom_command_without_parameters(self): response = get_response( self.address, - greet_msg='This is a greeting message!', - msg='$custom_command%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$custom_command%%%%%' ) - self.assertRegex(response, 'no_params') + self.assertRegex(response, b'no_params') class TestListeningUDPServer(unittest.TestCase): @@ -151,44 +151,44 @@ def tearDown(self): def test_proper_request(self): response = get_response( self.address, - msg='#command:a,b,c%%%%%', + msg=b'#command:a,b,c%%%%%', udp=True ) - self.assertEqual(response, 'aabbcc') + self.assertEqual(response, b'aabbcc') def test_wrong_request(self): """Wrong request but expected by the protocol""" response = get_response( - self.address, msg='#wrong_command:foo%%%%%', udp=True + self.address, msg=b'#wrong_command:foo%%%%%', udp=True ) - self.assertRegex(response, 'you sent a wrong command') + self.assertRegex(response, b'you sent a wrong command') def test_value_error(self): """The message of ValueError in the logfile""" get_response( - self.address, msg='#valueerror:%%%%%', response=False, udp=True + self.address, msg=b'#valueerror:%%%%%', response=False, udp=True ) self.assertIn('unexpected value', get_logs()) def test_unexpected_error(self): get_response( - self.address, msg='#unexpected:%%%%%', response=False, udp=True + self.address, msg=b'#unexpected:%%%%%', response=False, udp=True ) self.assertIn('unexpected exception', get_logs()) def test_custom_command_with_parameters(self): response = get_response( - self.address, msg='$custom_command:a,b,c%%%%%', udp=True + self.address, msg=b'$custom_command:a,b,c%%%%%', udp=True ) - self.assertRegex(response, 'ok_abc') + self.assertRegex(response, b'ok_abc') def test_custom_command_without_parameters(self): response = get_response( self.address, - msg='$custom_command%%%%%', + msg=b'$custom_command%%%%%', udp=True ) - self.assertRegex(response, 'no_params') + self.assertRegex(response, b'no_params') class TestSendingServer(unittest.TestCase): @@ -213,14 +213,18 @@ def tearDown(self): def test_get_message(self): response = get_response(self.address) - self.assertEqual(response, 'message') + self.assertEqual(response, b'message') def test_unknown_command(self): - get_response(self.address, msg='$unknown%%%%%', response=False) + get_response(self.address, msg=b'$unknown%%%%%', response=False) self.assertIn('command unknown not supported', get_logs()) def test_raise_exception(self): - get_response(self.address, msg='$raise_exception%%%%%', response=False) + get_response( + self.address, + msg=b'$raise_exception%%%%%', + response=False + ) self.assertIn( 'unexpected exception raised by sendingtestsystem', get_logs() ) @@ -248,12 +252,12 @@ def tearDown(self): def test_get_message(self): response = get_response(self.address, udp=True) - self.assertEqual(response, 'message') + self.assertEqual(response, b'message') def test_unknown_command(self): get_response( self.address, - msg='$unknown%%%%%', + msg=b'$unknown%%%%%', response=False, udp=True ) @@ -261,7 +265,10 @@ def test_unknown_command(self): def test_raise_exception(self): get_response( - self.address, msg='$raise_exception%%%%%', response=False, udp=True + self.address, + msg=b'$raise_exception%%%%%', + response=False, + udp=True ) self.assertIn( 'unexpected exception raised by sendingtestsystem', get_logs() @@ -293,50 +300,50 @@ def tearDown(self): def test_proper_request(self): response = get_response( self.l_address, - greet_msg='This is a greeting message!', - msg='#command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#command:a,b,c%%%%%' ) - self.assertEqual(response, 'aabbcc') + self.assertEqual(response, b'aabbcc') def test_wrong_request(self): """Wrong request but expected by the protocol""" response = get_response( self.l_address, - greet_msg='This is a greeting message!', - msg='#wrong_command:foo%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#wrong_command:foo%%%%%' ) - self.assertRegex(response, 'you sent a wrong command') + self.assertRegex(response, b'you sent a wrong command') def test_custom_command_with_parameters(self): response = get_response( self.l_address, - greet_msg='This is a greeting message!', - msg='$custom_command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$custom_command:a,b,c%%%%%' ) - self.assertRegex(response, 'ok_abc') + self.assertRegex(response, b'ok_abc') def test_custom_command_without_parameters(self): response = get_response( self.l_address, - greet_msg='This is a greeting message!', - msg='$custom_command%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$custom_command%%%%%' ) - self.assertRegex(response, 'no_params') + self.assertRegex(response, b'no_params') def test_get_message(self): response = get_response(self.s_address) - self.assertEqual(response, 'message') + self.assertEqual(response, b'message') def test_last_cmd(self): - message = '#test:1,2,3%%%%%' + message = b'#test:1,2,3%%%%%' l_response = get_response( self.l_address, - greet_msg='This is a greeting message!', + greet_msg=b'This is a greeting message!', msg=message ) - self.assertEqual(l_response, '112233') + self.assertEqual(l_response, b'112233') s_response = get_response(self.s_address) - self.assertEqual(s_response, message[1:].strip('%')) + self.assertEqual(s_response, message[1:].strip(b'%')) class TestDuplexUDPServer(unittest.TestCase): @@ -363,41 +370,41 @@ def tearDown(self): def test_proper_request(self): response = get_response( - self.l_address, msg='#command:a,b,c%%%%%', udp=True + self.l_address, msg=b'#command:a,b,c%%%%%', udp=True ) - self.assertEqual(response, 'aabbcc') + self.assertEqual(response, b'aabbcc') def test_wrong_request(self): """Wrong request but expected by the protocol""" response = get_response( - self.l_address, msg='#wrong_command:foo%%%%%', udp=True + self.l_address, msg=b'#wrong_command:foo%%%%%', udp=True ) - self.assertRegex(response, 'you sent a wrong command') + self.assertRegex(response, b'you sent a wrong command') def test_custom_command_with_parameters(self): response = get_response( self.l_address, - msg='$custom_command:a,b,c%%%%%', + msg=b'$custom_command:a,b,c%%%%%', udp=True ) - self.assertRegex(response, 'ok_abc') + self.assertRegex(response, b'ok_abc') def test_custom_command_without_parameters(self): response = get_response( - self.l_address, msg='$custom_command%%%%%', udp=True + self.l_address, msg=b'$custom_command%%%%%', udp=True ) - self.assertRegex(response, 'no_params') + self.assertRegex(response, b'no_params') def test_get_message(self): response = get_response(self.s_address, udp=True) - self.assertEqual(response, 'message') + self.assertEqual(response, b'message') def test_last_cmd(self): - message = '#test:1,2,3%%%%%' + message = b'#test:1,2,3%%%%%' l_response = get_response(self.l_address, msg=message, udp=True) - self.assertEqual(l_response, '112233') + self.assertEqual(l_response, b'112233') s_response = get_response(self.s_address, udp=True) - self.assertEqual(s_response, message[1:].strip('%')) + self.assertEqual(s_response, message[1:].strip(b'%')) class TestServerVarious(unittest.TestCase): @@ -415,10 +422,10 @@ def test_server_shutdown(self): def shutdown(self): response = get_response( address, - greet_msg='This is a greeting message!', - msg='$system_stop%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$system_stop%%%%%' ) - self.assertEqual(response, '$server_shutdown%%%%%') + self.assertEqual(response, b'$server_shutdown%%%%%') t = Timer(0.01, shutdown, args=(self,)) t.start() @@ -509,10 +516,10 @@ def test_start_and_stop_listening(self): response = get_response( address, - greet_msg='This is a greeting message!', - msg='#command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#command:a,b,c%%%%%' ) - self.assertEqual(response, 'aabbcc') + self.assertEqual(response, b'aabbcc') simulator.stop() @@ -525,7 +532,7 @@ def test_start_and_stop_sending(self): simulator.start(daemon=True) response = get_response(address) - self.assertEqual(response, 'message') + self.assertEqual(response, b'message') simulator.stop() @@ -540,12 +547,12 @@ def test_start_and_stop_duplex(self): l_response = get_response( l_addr, - greet_msg='This is a greeting message!', - msg='#command:a,b,c%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'#command:a,b,c%%%%%' ) - self.assertEqual(l_response, 'aabbcc') + self.assertEqual(l_response, b'aabbcc') s_response = get_response(s_addr) - self.assertEqual(s_response, 'command:a,b,c') + self.assertEqual(s_response, b'command:a,b,c') simulator.stop() @@ -567,12 +574,12 @@ def test_non_daemon_simulator(self): def shutdown(self): response = get_response( l_addr, - greet_msg='This is a greeting message!', - msg='$system_stop%%%%%' + greet_msg=b'This is a greeting message!', + msg=b'$system_stop%%%%%' ) - self.assertEqual(response, '$server_shutdown%%%%%') - response = get_response(s_addr, msg='$system_stop%%%%%') - self.assertEqual(response, '$server_shutdown%%%%%') + self.assertEqual(response, b'$server_shutdown%%%%%') + response = get_response(s_addr, msg=b'$system_stop%%%%%') + self.assertEqual(response, b'$server_shutdown%%%%%') t = Timer(0.01, shutdown, args=(self,)) t.start() @@ -587,11 +594,11 @@ def get_response( timeout=2.0, response=True, udp=False): - retval = '' + retval = b'' if udp: socket_type = socket.SOCK_DGRAM if not msg: - msg = '' + msg = b'' else: socket_type = socket.SOCK_STREAM with socket_context(socket.AF_INET, socket_type) as sock: @@ -599,14 +606,15 @@ def get_response( sock.connect(server_address) if greet_msg: greeting = sock.recv(len(greet_msg)) - greeting = greeting.decode('raw_unicode_escape') if greeting != greet_msg: raise ValueError - if isinstance(msg, str): - msg = msg.encode('raw_unicode_escape') - sock.sendto(msg, server_address) + if isinstance(msg, bytes): + sock.sendall(msg) if response: - retval = sock.recv(1024).decode('raw_unicode_escape') + try: + retval = sock.recv(1024) + except socket.timeout: + pass return retval @@ -615,22 +623,20 @@ def get_logs(): filename = os.path.join(os.getenv('ACSDATA', ''), 'sim-server.log') logs = [] with open(filename, mode='rb') as f: - f.seek(0, os.SEEK_END) - # read last 3 lines with current process ID buffer_string = '' - while len(logs) <= 3: + f.seek(0, os.SEEK_END) + while len(logs) < 3: try: f.seek(-2, os.SEEK_CUR) buffer_string += f.read(1).decode('utf-8') except OSError: - if buffer_string: - buffer_string += '\n' - else: - break + break if buffer_string.endswith('\n'): - buffer_string = buffer_string.strip()[::-1].split() - if str(os.getpid()) in buffer_string[2]: - logs.append(' '.join(buffer_string[3:])) + log = buffer_string[:-1][::-1] + pid = str(os.getpid()) + index = log.find(pid) + if index != -1: + logs.append(log[index + len(pid) + 1:]) buffer_string = '' return logs @@ -657,7 +663,7 @@ def parse(self, byte): if self.msg.endswith(self.tail): self.msg = self.msg.lstrip(self.header) self.msg = self.msg.rstrip(self.tail) - self.last_cmd.value = bytes(self.msg, 'raw_unicode_escape') + self.last_cmd.value = self.msg.encode('utf-8') name, params_str = self.msg.split(':') self.msg = '' if name == 'wrong_command': @@ -695,7 +701,7 @@ def __init__(self, **kwargs): # pylint: disable=unused-argument getattr(self, 'last_cmd') except AttributeError: self.last_cmd = Array(c_char, b'\x00' * 50) - self.last_cmd.value = 'message'.encode('raw_unicode_escape') + self.last_cmd.value = b'message' self.t = None def subscribe(self, q):