Simplify and rename TACAN registry reserve function (#1559)

* Simplify and rename TACAN registry reserve function

* Remove unused tacan error
This commit is contained in:
Magnus Wolffelt 2021-08-18 14:46:55 +02:00 committed by GitHub
parent 0cb10e4224
commit 056e6b28da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 77 deletions

View File

@ -228,11 +228,7 @@ class Operation:
if beacon.channel is None:
logging.error(f"TACAN beacon has no channel: {beacon.callsign}")
else:
cls.tacan_registry.reserve(
beacon.tacan_channel,
TacanUsage.TransmitReceive,
ignore_rules=True,
)
cls.tacan_registry.mark_unavailable(beacon.tacan_channel)
@classmethod
def _create_radio_registry(

View File

@ -60,13 +60,6 @@ class TacanChannelInUseError(RuntimeError):
super().__init__(f"{channel} is already in use")
class TacanChannelForbiddenError(RuntimeError):
"""Raised when attempting to reserve a, for technical reasons, forbidden channel."""
def __init__(self, channel: TacanChannel) -> None:
super().__init__(f"{channel} is forbidden")
class TacanRegistry:
"""Manages allocation of TACAN channels."""
@ -103,28 +96,17 @@ class TacanRegistry:
except StopIteration:
raise OutOfTacanChannelsError(band)
def reserve(
self,
channel: TacanChannel,
intended_usage: TacanUsage,
ignore_rules: bool = False,
) -> None:
def mark_unavailable(self, channel: TacanChannel) -> None:
"""Reserves the given channel.
Reserving a channel ensures that it will not be allocated in the future.
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 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)

View File

@ -2,7 +2,6 @@ from gen.tacan import (
OutOfTacanChannelsError,
TacanBand,
TacanChannel,
TacanChannelForbiddenError,
TacanChannelInUseError,
TacanRegistry,
TacanUsage,
@ -24,15 +23,6 @@ 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()
@ -53,7 +43,7 @@ def test_reserve_all_valid_transmit_receive() -> None:
print("All valid x", ALL_VALID_X_TR)
for num in ALL_VALID_X_TR:
registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.TransmitReceive)
registry.mark_unavailable(TacanChannel(num, TacanBand.X))
with pytest.raises(OutOfTacanChannelsError):
registry.alloc_for_band(TacanBand.X, TacanUsage.TransmitReceive)
@ -69,7 +59,7 @@ def test_reserve_all_valid_a2a() -> None:
print("All valid x", ALL_VALID_X_A2A)
for num in ALL_VALID_X_A2A:
registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.AirToAir)
registry.mark_unavailable(TacanChannel(num, TacanBand.X))
with pytest.raises(OutOfTacanChannelsError):
registry.alloc_for_band(TacanBand.X, TacanUsage.AirToAir)
@ -80,47 +70,8 @@ def test_reserve_all_valid_a2a() -> None:
assert chanTR == TacanChannel(1, TacanBand.X)
@pytest.mark.skip(reason="TODO")
def test_allocate_all() -> None:
pass
def test_reserve_invalid_tr_channels() -> None:
registry = TacanRegistry()
some_invalid_channels = [
TacanChannel(2, TacanBand.X),
TacanChannel(30, TacanBand.X),
TacanChannel(47, TacanBand.X),
TacanChannel(63, TacanBand.X),
TacanChannel(2, TacanBand.Y),
TacanChannel(30, TacanBand.Y),
TacanChannel(64, TacanBand.Y),
TacanChannel(92, TacanBand.Y),
]
for chan in some_invalid_channels:
with pytest.raises(TacanChannelForbiddenError):
registry.reserve(chan, TacanUsage.TransmitReceive)
def test_reserve_invalid_a2a_channels() -> None:
registry = TacanRegistry()
some_invalid_channels = [
TacanChannel(1, TacanBand.X),
TacanChannel(36, TacanBand.X),
TacanChannel(64, TacanBand.X),
TacanChannel(99, TacanBand.X),
TacanChannel(1, TacanBand.Y),
TacanChannel(36, TacanBand.Y),
TacanChannel(64, TacanBand.Y),
TacanChannel(99, TacanBand.Y),
]
for chan in some_invalid_channels:
with pytest.raises(TacanChannelForbiddenError):
registry.reserve(chan, TacanUsage.AirToAir)
def test_reserve_again() -> None:
registry = TacanRegistry()
with pytest.raises(TacanChannelInUseError):
registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive)
registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive)
registry.mark_unavailable(TacanChannel(1, TacanBand.X))
registry.mark_unavailable(TacanChannel(1, TacanBand.X))