Skip to content

Commit b866d7d

Browse files
committed
Uniquify zone names (and warn about it)
Attempt to dedupe zone names by appending a number when pulling k7abd zones or expanding static talkgroups (although possibly less of an issue due to channel deduping that already exists) Fixes #80
1 parent 2674d5b commit b866d7d

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/dzcb/k7abd.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Zone,
3939
)
4040
import dzcb.tone
41+
from dzcb.util import unique_name
4142

4243

4344
logger = logging.getLogger(__name__)
@@ -97,7 +98,7 @@ def Codeplug_from_zone_dicts(zone_dicts):
9798
def update_static_talkgroups(ch):
9899
contacts.update(ch.static_talkgroups)
99100
grouplist = GroupList(
100-
name="{} TGS".format(ch.code),
101+
name="{} TGS".format(ch.code or ch.name[:5]),
101102
contacts=ch.static_talkgroups,
102103
)
103104
grouplists.append(grouplist)
@@ -126,9 +127,7 @@ def update_static_talkgroups(ch):
126127
ch = attr.evolve(ch, dedup_key=ch._dedup_key + 1)
127128
all_channels[ch.short_name] = ch
128129
updated_channels.append(ch)
129-
scanlists.append(
130-
attr.evolve(zscanlist, channels=updated_channels)
131-
)
130+
scanlists.append(attr.evolve(zscanlist, channels=updated_channels))
132131
zones.append(
133132
Zone(
134133
name=zname,
@@ -308,8 +307,8 @@ def update_zones_channels(zones_dict, in_zones, log_filename=None):
308307
:param log_filename: used for logging only
309308
"""
310309
_log_zones_channels(in_zones, log_filename)
311-
# XXX: instead, consider combining channels from same-named zones in different CSV files?
312-
zones_dict.update(in_zones)
310+
for zname, zchannels in in_zones.items():
311+
zones_dict[unique_name(zname, zones_dict)] = zchannels
313312

314313

315314
def Codeplug_from_k7abd(input_dir):

src/dzcb/model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import dzcb.exceptions
1616
import dzcb.munge
1717
import dzcb.tone
18+
from dzcb.util import unique_name
1819

1920
# XXX: i hate this
2021
NAME_MAX = 16
@@ -845,14 +846,16 @@ def expand_static_talkgroups(self, static_talkgroup_order=None):
845846
if static_talkgroup_order is None:
846847
static_talkgroup_order = []
847848
zones = list(self.zones)
849+
zone_names = set(z.name for z in zones)
848850
channels = []
849851
exp_scanlists = []
850852
for ch in self.channels:
851853
if not isinstance(ch, DigitalChannel) or not ch.static_talkgroups:
852854
channels.append(ch)
853855
continue
856+
exp_zone_name = unique_name(ch.short_name, zone_names)
854857
zscanlist = ScanList(
855-
name=ch.short_name,
858+
name=exp_zone_name,
856859
channels=[],
857860
)
858861
zone_channels = ch.from_talkgroups(
@@ -862,7 +865,7 @@ def expand_static_talkgroups(self, static_talkgroup_order=None):
862865
exp_scanlists.append(attr.evolve(zscanlist, channels=zone_channels))
863866
zones.append(
864867
Zone(
865-
name=ch.short_name,
868+
name=exp_zone_name,
866869
channels_a=zone_channels,
867870
channels_b=zone_channels,
868871
)

src/dzcb/util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23

34

@@ -15,6 +16,9 @@
1516
}
1617

1718

19+
logger = logging.getLogger(__name__)
20+
21+
1822
def getenv_bool(var_name, default=False):
1923
"""
2024
Retrieve the given environment variable as a bool.
@@ -26,3 +30,28 @@ def getenv_bool(var_name, default=False):
2630
if val is None:
2731
return default
2832
return STR_TO_BOOL[val.lower()]
33+
34+
35+
def unique_name(name, existing_names, fmt="{} {}"):
36+
"""
37+
Create a unique name by appending numbers.
38+
39+
:param name: the base name that numbers are added to
40+
:param existing_names: container of names that are taken (prefer set or dict)
41+
:param fmt: how to format the new name, default "{} {}"
42+
expects 2 positional args in a new-style format string
43+
:return: a name based on `name` that doesn't exist in `existing_names`.
44+
"""
45+
ix = 0
46+
maybe_unique_name = name
47+
while maybe_unique_name in existing_names:
48+
maybe_unique_name = fmt.format(name, ix)
49+
ix += 1
50+
if maybe_unique_name != name:
51+
logger.warning(
52+
"Deduping name {!r} -> {!r}. Consider using unique names for clarity.".format(
53+
name,
54+
maybe_unique_name,
55+
),
56+
)
57+
return maybe_unique_name

0 commit comments

Comments
 (0)