diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f874f72..78ea037 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,18 +18,19 @@ repos: - id: setup-cfg-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.9.0 + rev: v0.14.10 hooks: - - id: ruff ###### Relevant part below ###### - - id: ruff + - id: ruff-check + types_or: [python, pyi] args: ["check", "--select", "I", "--fix"] ###### Relevant part above ###### - id: ruff-format + types_or: [python, pyi] - repo: https://github.com/astral-sh/uv-pre-commit # uv version. - rev: 0.5.16 + rev: 0.9.22 hooks: # Compile requirements - id: pip-compile diff --git a/aprsd/cmds/listen.py b/aprsd/cmds/listen.py index d9038c6..e81a62a 100644 --- a/aprsd/cmds/listen.py +++ b/aprsd/cmds/listen.py @@ -67,7 +67,11 @@ class APRSDListenProcessThread(rx.APRSDFilterThread): def print_packet(self, packet): if self.log_packets: - packet_log.log(packet, force_log=True) + packet_log.log( + packet, + packet_count=self.packet_count, + force_log=True, + ) def process_packet(self, packet: type[core.Packet]): if self.plugin_manager: diff --git a/aprsd/packets/log.py b/aprsd/packets/log.py index a48720d..6f33b80 100644 --- a/aprsd/packets/log.py +++ b/aprsd/packets/log.py @@ -22,10 +22,15 @@ DEGREES_COLOR = 'fg #FFA900' def log_multiline( - packet, tx: Optional[bool] = False, header: Optional[bool] = True + packet, + tx: Optional[bool] = False, + header: Optional[bool] = True, + force_log: Optional[bool] = False, ) -> None: """LOG a packet to the logfile.""" - if not CONF.enable_packet_logging: + # If logging is disabled and we're not forcing log, return early + # However, if we're forcing log, we still proceed + if not CONF.enable_packet_logging and not force_log: return if CONF.log_packet_format == 'compact': return @@ -77,12 +82,15 @@ def log_multiline( if hasattr(packet, 'comment') and packet.comment: logit.append(f' Comment : {packet.comment}') - raw = packet.raw.replace('<', '\\<') + raw = packet.raw + if raw: + raw = raw.replace('<', '\\<') + else: + raw = '' logit.append(f' Raw : {raw}') logit.append(f'{header_str}________(<{PACKET_COLOR}>{name})') LOGU.opt(colors=True).info('\n'.join(logit)) - LOG.debug(repr(packet)) def log( @@ -92,12 +100,17 @@ def log( packet_count: Optional[int] = None, force_log: Optional[bool] = False, ) -> None: + # If logging is disabled and we're not forcing log, return early if not CONF.enable_packet_logging and not force_log: return + + # Handle multiline format if CONF.log_packet_format == 'multiline': - log_multiline(packet, tx, header) + log_multiline(packet, tx, header, force_log) return + # Handle compact format - this is the default case + # This is the compact format logging logic (which was unreachable before) if not packet_count: packet_count = '' else: @@ -169,4 +182,6 @@ def log( ) LOGU.opt(colors=True).info(' '.join(logit)) - log_multiline(packet, tx, header) + # Note: We don't call log_multiline again here for compact format since it's already handled above + if CONF.log_packet_format == 'both': + log_multiline(packet, tx, header, force_log) diff --git a/aprsd/threads/rx.py b/aprsd/threads/rx.py index 64c1421..3a22319 100644 --- a/aprsd/threads/rx.py +++ b/aprsd/threads/rx.py @@ -98,7 +98,6 @@ class APRSDRXThread(APRSDThread): ) return self.pkt_count += 1 - packet_log.log(packet, packet_count=self.pkt_count) self.packet_queue.put(packet) @@ -106,6 +105,7 @@ class APRSDFilterThread(APRSDThread): def __init__(self, thread_name, packet_queue): super().__init__(thread_name) self.packet_queue = packet_queue + self.packet_count = 0 def filter_packet(self, packet): # Do any packet filtering prior to processing @@ -120,11 +120,12 @@ class APRSDFilterThread(APRSDThread): doesn't want to log packets. """ - packet_log.log(packet) + packet_log.log(packet, packet_count=self.packet_count) def loop(self): try: packet = self.packet_queue.get(timeout=1) + self.packet_count += 1 self.print_packet(packet) if packet: if self.filter_packet(packet): diff --git a/tests/fake.py b/tests/fake.py index 53f0980..6c78a82 100644 --- a/tests/fake.py +++ b/tests/fake.py @@ -30,7 +30,25 @@ def fake_packet( if response: packet_dict['response'] = response - return core.factory(packet_dict) + packet = core.factory(packet_dict) + # Call prepare to build the raw data + packet.prepare() + return packet + + +def fake_gps_packet(): + """Create a properly prepared GPSPacket for testing.""" + packet = core.GPSPacket( + from_call=FAKE_FROM_CALLSIGN, + to_call=FAKE_TO_CALLSIGN, + latitude=37.7749, + longitude=-122.4194, + symbol='>', + comment='Test GPS comment', + ) + # Call prepare to build the raw data + packet.prepare() + return packet def fake_ack_packet(): diff --git a/tests/threads/test_rx.py b/tests/threads/test_rx.py index d5e85a7..ffc3fcc 100644 --- a/tests/threads/test_rx.py +++ b/tests/threads/test_rx.py @@ -261,10 +261,11 @@ class TestAPRSDFilterThread(unittest.TestCase): def test_print_packet(self): """Test print_packet() method.""" packet = fake.fake_packet() + self.filter_thread.packet_count = 5 # Set a packet count with mock.patch('aprsd.threads.rx.packet_log') as mock_log: self.filter_thread.print_packet(packet) - mock_log.log.assert_called_with(packet) + mock_log.log.assert_called_with(packet, packet_count=5) def test_loop_with_packet(self): """Test loop() with packet in queue."""