Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve date introspection to work with example like adventofcode/202… #96

Merged
merged 4 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions aocd/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def get_day_and_year():
"""
pattern_year = r"201[5-9]|202[0-9]"
pattern_day = r"2[0-5]|1[0-9]|[1-9]"
pattern_path = re.escape(os.sep) + re.escape(os.sep).join([r"20\d\d", r"[0-2]?\d", r".*\.py$"])
visited = []

def giveup(msg):
log.info("introspection failure")
for fname in visited:
log.info("stack crawl visited %s", fname)
return AocdError(msg)

for frame in traceback.extract_stack():
filename = frame[0]
linetxt = frame[-1] or ""
Expand All @@ -112,6 +121,7 @@ def get_day_and_year():
"aocd" not in linetxt,
"ipykernel" in filename,
]
visited.append(filename)
if not any(reasons_to_skip_frame):
log.debug("stack crawl found %s", filename)
abspath = os.path.abspath(filename)
Expand All @@ -126,6 +136,18 @@ def get_day_and_year():
if abspath and re.search(pattern_day, abspath):
basename = os.path.basename(abspath)
break
elif re.search(pattern_path, filename):
year = day = None
for part in filename.split(os.sep):
if not part.isdigit():
continue
if len(part) == 4:
year = int(part)
elif 1 <= len(part) <= 2:
day = int(part)
if year is not None and day is not None:
log.debug("year=%s day=%s filename=%s", year, day, filename)
return day, year
log.debug("skipping frame %s", filename)
else:
import __main__
Expand All @@ -135,10 +157,10 @@ def get_day_and_year():
year = most_recent_year()
return day, year
log.debug("non-interactive")
raise AocdError("Failed introspection of filename")
raise giveup("Failed introspection of filename")
years = {int(year) for year in re.findall(pattern_year, abspath)}
if len(years) > 1:
raise AocdError("Failed introspection of year")
raise giveup("Failed introspection of year")
year = years.pop() if years else None
basename_no_years = re.sub(pattern_year, "", basename)
try:
Expand All @@ -152,4 +174,4 @@ def get_day_and_year():
log.debug("year=%s day=%s", year or "?", day)
return day, year
log.debug("giving up introspection for %s", abspath)
raise AocdError("Failed introspection of day")
raise giveup("Failed introspection of day")
2 changes: 1 addition & 1 deletion aocd/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.3.1"
11 changes: 11 additions & 0 deletions tests/test_date_introspection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

from aocd.exceptions import AocdError
Expand Down Expand Up @@ -61,3 +63,12 @@ def test_ipynb_fail_no_numbers_in_ipynb_filename(mocker):
mocker.patch("aocd.get.get_ipynb_path", "puzzle.py")
with pytest.raises(AocdError("Failed introspection of filename")):
get_day_and_year()


def test_no_numbers_in_py_filename_but_date_in_abspath(mocker):
fname = os.sep.join(["adventofcode", "2022", "02", "main.py"])
fake_stack = [(fname, 1, "<test>", "from aocd import data")]
mocker.patch("aocd.get.traceback.extract_stack", return_value=fake_stack)
day, year = get_day_and_year()
assert day == 2
assert year == 2022