Skip to content

Commit

Permalink
add decode strict arg in run method (#98)
Browse files Browse the repository at this point in the history
* add decode strict arg in run method

* adapt client.py unit tests with new run strict arg

* add ignore_decoding_error arg in APRSClient.run()
  • Loading branch information
lemoidului committed May 31, 2021
1 parent a5337c8 commit 5291dde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
9 changes: 6 additions & 3 deletions ogn/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def connect(self, retries=1, wait_period=15):

login = create_aprs_login(self.aprs_user, -1, self.settings.APRS_APP_NAME, self.settings.APRS_APP_VER, self.aprs_filter)
self.sock.send(login.encode())
self.sock_file = self.sock.makefile('rw')
self.sock_file = self.sock.makefile('rb')

self._kill = False

Expand All @@ -67,7 +67,8 @@ def disconnect(self):

self._kill = True

def run(self, callback, timed_callback=lambda client: None, autoreconnect=False, **kwargs):
def run(self, callback, timed_callback=lambda client: None, autoreconnect=False, ignore_decoding_error=True,
**kwargs):
while not self._kill:
try:
keepalive_time = time()
Expand All @@ -79,7 +80,8 @@ def run(self, callback, timed_callback=lambda client: None, autoreconnect=False,
keepalive_time = time()

# Read packet string from socket
packet_str = self.sock_file.readline().strip()
packet_b = self.sock_file.readline().strip()
packet_str = packet_b.decode(errors="replace") if ignore_decoding_error else packet_b.decode()

# A zero length line should not be return if keepalives are being sent
# A zero length line will only be returned after ~30m if keepalives are not sent
Expand All @@ -94,6 +96,7 @@ def run(self, callback, timed_callback=lambda client: None, autoreconnect=False,
self.logger.error('OSError')
except UnicodeDecodeError:
self.logger.error('UnicodeDecodeError')
self.logger.debug(packet_b)

if autoreconnect and not self._kill:
self.connect(retries=100)
Expand Down
22 changes: 11 additions & 11 deletions tests/client/test_AprsClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_connect_full_feed(self, mock_socket):
client.connect()
client.sock.send.assert_called_once_with('user testuser pass -1 vers {} {}\n'.format(
APRS_APP_NAME, APRS_APP_VER).encode('ascii'))
client.sock.makefile.assert_called_once_with('rw')
client.sock.makefile.assert_called_once_with('rb')

@mock.patch('ogn.client.client.socket')
def test_connect_client_defined_filter(self, mock_socket):
client = AprsClient(aprs_user='testuser', aprs_filter='r/50.4976/9.9495/100')
client.connect()
client.sock.send.assert_called_once_with('user testuser pass -1 vers {} {} filter r/50.4976/9.9495/100\n'.format(
APRS_APP_NAME, APRS_APP_VER).encode('ascii'))
client.sock.makefile.assert_called_once_with('rw')
client.sock.makefile.assert_called_once_with('rb')

@mock.patch('ogn.client.client.socket')
def test_disconnect(self, mock_socket):
Expand All @@ -53,18 +53,18 @@ def test_run(self, mock_socket):
client.connect()

client.sock_file.readline = mock.MagicMock()
client.sock_file.readline.side_effect = ['Normal text blabla',
'my weird character ¥',
client.sock_file.readline.side_effect = [b'Normal text blabla',
b'my weird character \xc2\xa5',
UnicodeDecodeError('funnycodec', b'\x00\x00', 1, 2, 'This is just a fake reason!'),
'... show must go on',
b'... show must go on',
BrokenPipeError(),
'... and on',
b'... and on',
ConnectionResetError(),
'... and on',
b'... and on',
socket.error(),
'... and on',
'',
'... and on',
b'... and on',
b'',
b'... and on',
KeyboardInterrupt()]

try:
Expand All @@ -84,7 +84,7 @@ def test_run_keepalive(self, mock_socket, mock_time):
client.connect()

client.sock_file.readline = mock.MagicMock()
client.sock_file.readline.side_effect = ['Normal text blabla',
client.sock_file.readline.side_effect = [b'Normal text blabla',
KeyboardInterrupt()]

mock_time.side_effect = [0, 0, APRS_KEEPALIVE_TIME + 1, APRS_KEEPALIVE_TIME + 1]
Expand Down

0 comments on commit 5291dde

Please sign in to comment.