Skip to content

Commit 4dd0ae7

Browse files
committed
simplify key_list
1 parent 7dafc55 commit 4dd0ae7

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

bugwarrior/db.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import re
66
import subprocess
7-
from typing import TYPE_CHECKING, Any
7+
from typing import TYPE_CHECKING, Any, Sequence
88

99
from taskw import TaskWarriorShellout
1010
from taskw.exceptions import TaskwarriorError
@@ -50,13 +50,13 @@ def hamdist(str1: str, str2: str) -> int:
5050

5151

5252
def get_managed_task_uuids(
53-
tw: TaskWarriorShellout, key_list: dict[str, list[str]]
53+
tw: TaskWarriorShellout, unique_key_sets: Iterable[Iterable[str]]
5454
) -> set[str]:
5555
expected_task_ids = set()
56-
for keys in key_list.values():
56+
for unique_keys in unique_key_sets:
5757
tasks = tw.filter_tasks(
5858
{
59-
'and': [('%s.any' % key, None) for key in keys],
59+
'and': [('%s.any' % key, None) for key in unique_keys],
6060
'or': [('status', 'pending'), ('status', 'waiting')],
6161
}
6262
)
@@ -65,21 +65,25 @@ def get_managed_task_uuids(
6565
return expected_task_ids
6666

6767

68-
def make_unique_identifier(keys: dict[str, list[str]], issue: dict[str, Any]) -> str:
68+
def make_unique_identifier(
69+
key_lists: Iterable[tuple[str, ...]], issue: dict[str, Any]
70+
) -> str:
6971
"""For a given issue, make an identifier from its unique keys.
7072
7173
This is not the same as the taskwarrior uuid, which is assigned
7274
only once the task is created.
7375
"""
74-
for service, key_list in keys.items():
75-
if all([key in issue for key in key_list]):
76+
for key_list in key_lists:
77+
if all(key in issue for key in key_list):
7678
subset = {key: issue[key] for key in key_list}
7779
return json.dumps(subset, sort_keys=True)
7880
raise RuntimeError("Could not determine unique identifier for %s" % issue)
7981

8082

8183
def find_taskwarrior_uuid(
82-
tw: TaskWarriorShellout, keys: dict[str, list[str]], issue: dict[str, Any]
84+
tw: TaskWarriorShellout,
85+
unique_key_sets: Iterable[Sequence[str]],
86+
issue: dict[str, Any],
8387
) -> str:
8488
"""For a given issue issue, find its local taskwarrior UUID.
8589
@@ -116,11 +120,11 @@ def find_taskwarrior_uuid(
116120

117121
possibilities = set()
118122

119-
for service, key_list in keys.items():
120-
if any([key in issue for key in key_list]):
123+
for unique_keys in unique_key_sets:
124+
if any(key in issue for key in unique_keys):
121125
results = tw.filter_tasks(
122126
{
123-
'and': [("%s.is" % key, issue[key]) for key in key_list],
127+
'and': [("%s.is" % key, issue[key]) for key in unique_keys],
124128
'or': [
125129
('status', 'pending'),
126130
('status', 'waiting'),
@@ -136,7 +140,7 @@ def find_taskwarrior_uuid(
136140
r['status'] == 'completed' for r in results
137141
):
138142
for r in results[1:]:
139-
for k in key_list:
143+
for k in unique_keys:
140144
if r[k] != results[0][k]:
141145
break
142146
else:
@@ -265,7 +269,7 @@ def synchronize(
265269
dry_run: bool = False,
266270
) -> None:
267271
services = [service_config.service for service_config in conf.service_configs]
268-
key_list = build_key_list(services)
272+
unique_key_sets = build_unique_key_sets(services)
269273
uda_list = build_uda_config_overrides(services)
270274

271275
if uda_list:
@@ -300,7 +304,7 @@ def synchronize(
300304
continue
301305

302306
# De-duplicate issues coming in
303-
unique_identifier = make_unique_identifier(key_list, issue)
307+
unique_identifier = make_unique_identifier(unique_key_sets, issue)
304308
if unique_identifier in issue_map:
305309
log.debug(f"Merging tags and skipping. Seen {unique_identifier} of {issue}")
306310
# Merge and deduplicate tags.
@@ -333,7 +337,9 @@ def synchronize(
333337
service_config = successful_config_map[issue.pop('target')]
334338

335339
try:
336-
existing_taskwarrior_uuid = find_taskwarrior_uuid(tw, key_list, issue)
340+
existing_taskwarrior_uuid = find_taskwarrior_uuid(
341+
tw, unique_key_sets, issue
342+
)
337343
except MultipleMatches as e:
338344
log.exception("Multiple matches: %s", str(e))
339345
except NotFound: # Create new task
@@ -423,7 +429,7 @@ def synchronize(
423429
log.debug(f'Closing tasks for succeeding services: {list(successful_config_map)}.')
424430
succeeded_service_task_uuids = get_managed_task_uuids(
425431
tw,
426-
build_key_list(
432+
build_unique_key_sets(
427433
service_config.service for service_config in successful_config_map.values()
428434
),
429435
)
@@ -470,10 +476,8 @@ def synchronize(
470476
)
471477

472478

473-
def build_key_list(services: Iterable[str]) -> dict[str, list[str]]:
474-
return {
475-
service: get_service(service).ISSUE_CLASS.UNIQUE_KEY for service in services
476-
}
479+
def build_unique_key_sets(services: Iterable[str]) -> set[tuple[str, ...]]:
480+
return {get_service(service).ISSUE_CLASS.UNIQUE_KEY for service in services}
477481

478482

479483
def get_defined_udas_as_strings(conf: "Config") -> Iterator[str]:

0 commit comments

Comments
 (0)