From 34ff5fbc6a9681c58dcc5d359614f793a696a1d9 Mon Sep 17 00:00:00 2001 From: Magnus Wolffelt Date: Wed, 18 Aug 2021 00:06:34 +0200 Subject: [PATCH] Allow operation.py to ignore TACAN rules --- game/operation/operation.py | 4 +++- gen/tacan.py | 13 ++++++++++--- tests/test_tacan.py | 9 +++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/game/operation/operation.py b/game/operation/operation.py index 00f1d81b..b717a775 100644 --- a/game/operation/operation.py +++ b/game/operation/operation.py @@ -229,7 +229,9 @@ class Operation: logging.error(f"TACAN beacon has no channel: {beacon.callsign}") else: cls.tacan_registry.reserve( - beacon.tacan_channel, TacanUsage.TransmitReceive + beacon.tacan_channel, + TacanUsage.TransmitReceive, + ignore_rules=True, ) @classmethod diff --git a/gen/tacan.py b/gen/tacan.py index d17110b5..127ad86b 100644 --- a/gen/tacan.py +++ b/gen/tacan.py @@ -103,7 +103,12 @@ class TacanRegistry: except StopIteration: raise OutOfTacanChannelsError(band) - def reserve(self, channel: TacanChannel, intended_usage: TacanUsage) -> None: + def reserve( + self, + channel: TacanChannel, + intended_usage: TacanUsage, + ignore_rules: bool = False, + ) -> None: """Reserves the given channel. Reserving a channel ensures that it will not be allocated in the future. @@ -111,13 +116,15 @@ class TacanRegistry: Args: channel: The channel to reserve. intended_usage: What the caller intends to use the tacan channel for. + ignore_rules: Whether to reserve regardless of recommended rules. Raises: TacanChannelInUseError: The given channel is already in use. TacanChannelForbiddenError: The given channel is forbidden. """ - if channel.number in UNAVAILABLE[intended_usage][channel.band]: - raise TacanChannelForbiddenError(channel) + if not ignore_rules: + if channel.number in UNAVAILABLE[intended_usage][channel.band]: + raise TacanChannelForbiddenError(channel) if channel in self.allocated_channels: raise TacanChannelInUseError(channel) self.allocated_channels.add(channel) diff --git a/tests/test_tacan.py b/tests/test_tacan.py index 216cac58..92c11f49 100644 --- a/tests/test_tacan.py +++ b/tests/test_tacan.py @@ -24,6 +24,15 @@ def test_allocate_first_few_channels() -> None: assert chan3 == TacanChannel(32, TacanBand.X) +def test_reserve_ignoring_rules() -> None: + registry = TacanRegistry() + with pytest.raises(TacanChannelForbiddenError): + registry.reserve(TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive) + registry.reserve( + TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive, ignore_rules=True + ) + + def test_allocate_different_usages() -> None: """Make sure unallocated channels for one use don't make channels unavailable for other usage""" registry = TacanRegistry()