Skip to content

Commit 35b29cb

Browse files
committed
Support caching (and reusing) the mapping of ingested tasks
1 parent 5df7661 commit 35b29cb

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

scripts/element_tracking.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
import contextlib
2-
from typing import Callable, Iterator, MutableMapping, Optional, Set
2+
import json
3+
from pathlib import Path
4+
from types import TracebackType
5+
from typing import (
6+
Callable,
7+
Dict,
8+
Iterator,
9+
MutableMapping,
10+
Optional,
11+
Set,
12+
Type,
13+
)
14+
15+
16+
class ElementsCache:
17+
def __init__(self, cache_path: Path) -> None:
18+
self.cache_path = cache_path
19+
self.elements: Dict[str, int] = {}
20+
21+
if cache_path.exists():
22+
self.elements = json.loads(cache_path.read_text())
23+
24+
def __enter__(self) -> Dict[str, int]:
25+
return self.elements
26+
27+
def __exit__(
28+
self,
29+
exc_type: Optional[Type[BaseException]],
30+
exc_val: Optional[BaseException],
31+
exc_tb: Optional[TracebackType],
32+
) -> None:
33+
self.cache_path.write_text(json.dumps(self.elements))
334

435

536
class ElementsInProgress:

scripts/import.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import contextlib
45
from pathlib import Path
5-
from typing import TYPE_CHECKING, Callable, MutableMapping, Optional
6+
from typing import TYPE_CHECKING, Callable, Dict, MutableMapping, Optional
67

78
import yaml
8-
from element_tracking import ElementsInProgress
9+
from element_tracking import ElementsCache, ElementsInProgress
910
from import_backends import FakeTracBackend, GitHubBackend, RealTracBackend
1011
from ticket_type import Ticket
1112

@@ -123,6 +124,12 @@ def parse_args() -> argparse.Namespace:
123124
'year',
124125
help="SR year to generate for (specify as just the number part)",
125126
)
127+
parser.add_argument(
128+
'--cache',
129+
help="Path to a JSON file in which to load/store mapping from existing tasks.",
130+
type=Path,
131+
default=None,
132+
)
126133

127134
backends_group = parser.add_mutually_exclusive_group()
128135
backends_group.add_argument(
@@ -147,7 +154,15 @@ def main(arguments: argparse.Namespace) -> None:
147154
else:
148155
backend = FakeTracBackend()
149156

150-
add(arguments.base, backend, arguments.year)
157+
with contextlib.ExitStack() as stack:
158+
if arguments.cache:
159+
elements: Optional[Dict[str, int]] = stack.enter_context(
160+
ElementsCache(arguments.cache),
161+
)
162+
else:
163+
elements = None
164+
165+
add(arguments.base, backend, arguments.year, known_elements=elements)
151166

152167

153168
if __name__ == '__main__':

0 commit comments

Comments
 (0)