Skip to content

Commit dfbe5fd

Browse files
authored
Merge pull request #160 from wimglenn/issue-133
Always pre-check for an unlocked puzzle page when accessing examples.
2 parents 30bbea4 + 2ac2399 commit dfbe5fd

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

aocd/models.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _get_examples(self, parser_name="reference"):
268268
# logs warning and returns an empty list if the parser plugin raises an
269269
# exception for any reason.
270270
try:
271-
page = examples.Page.from_raw(html=self._get_prose())
271+
page = examples.Page.from_raw(html=self._get_prose(force_precheck=True))
272272
parser = _load_example_parser(name=parser_name)
273273
if getattr(parser, "uses_real_datas", True):
274274
datas = examples._get_unique_real_inputs(self.year, self.day)
@@ -737,9 +737,16 @@ def _request_puzzle_page(self):
737737
_ensure_intermediate_dirs(self.prose0_path)
738738
self.prose0_path.write_text(text, encoding="utf-8")
739739

740-
def _get_prose(self):
740+
def _get_prose(self, force_precheck=False):
741741
# prefer to return full prose (i.e. part b is solved or unlocked)
742742
# prefer to return prose with answers from same the user id as self.user.id
743+
if force_precheck:
744+
unlocked_files = [
745+
*AOCD_DATA_DIR.glob("*/" + self.prose1_path.name),
746+
*AOCD_DATA_DIR.glob("*/" + self.prose2_path.name),
747+
]
748+
if not unlocked_files:
749+
self._request_puzzle_page()
743750
for path in self.prose2_path, self.prose1_path:
744751
if path.is_file():
745752
log.debug("_get_prose cache hit %s", path)
@@ -753,7 +760,8 @@ def _get_prose(self):
753760
log.debug("_get_prose cache hit %s", self.prose0_path)
754761
return self.prose0_path.read_text(encoding="utf-8")
755762
log.debug("_get_prose cache miss year=%d day=%d", self.year, self.day)
756-
self._request_puzzle_page()
763+
if not force_precheck:
764+
self._request_puzzle_page()
757765
for path in self.prose2_path, self.prose1_path, self.prose0_path:
758766
if path.is_file():
759767
log.debug("_get_prose using %s", path)

tests/test_example_parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
<title>Day 1 - Advent of Code 1234</title>
1010
<article>
1111
<pre><code>test input data</code></pre>
12-
<code>test answer_a</code>
12+
<p>Your puzzle answer was <code>test answer_a</code>.</p>
1313
</article>
1414
<article>
15-
<code>test answer_b</code>
15+
<p>Your puzzle answer was <code>test answer_b</code>.</p>
1616
</article>
17+
<p>Both parts of this puzzle are complete! They provide two gold stars: **</p>
1718
"""
1819

1920

@@ -70,7 +71,7 @@ def test_invalid_page_no_title():
7071

7172
def test_aoce(mocker, freezer, pook, capsys):
7273
pook.get(
73-
url="https://adventofcode.com:443/2022/day/1",
74+
url="https://adventofcode.com/2022/day/1",
7475
response_body=fake_prose,
7576
)
7677
freezer.move_to("2022-12-01 12:00:00-0500")

tests/test_models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,25 @@ def test_user_from_unknown_id(aocd_config_dir):
374374
User.from_id("blah")
375375

376376

377-
def test_examples_cache(aocd_data_dir, pook):
377+
def test_examples_cache(aocd_data_dir, pook, caplog):
378+
caplog.set_level(logging.DEBUG)
378379
mock = pook.get(
379380
url="https://adventofcode.com/2014/day/1",
380381
response_body=(
381382
"<title>Day 1 - Advent of Code 2014</title>"
382383
"<article><pre><code>1\n2\n3\n</code></pre><code>abc</code></article>"
383384
"<article><pre><code>1\n2\n3\n</code></pre><code>xyz</code></article>"
385+
"The first half of this puzzle is complete!"
386+
"<p>Your puzzle answer was <code>answerA</code></p>"
384387
),
385388
times=1,
386389
)
387390
puzzle = Puzzle(day=1, year=2014)
388391
assert mock.calls == 0
389392
assert puzzle.examples[0].input_data == "1\n2\n3"
390393
assert mock.calls == 1
391-
assert puzzle.examples[0].input_data
392-
assert mock.calls == 1
394+
assert puzzle.examples[0].input_data == "1\n2\n3"
395+
assert mock.calls == 1, "Should re-use cached result"
393396

394397

395398
def test_example_partial(aocd_data_dir, pook):

0 commit comments

Comments
 (0)