Skip to content

Commit

Permalink
v0.9.1 (#33)
Browse files Browse the repository at this point in the history
tweaks in blocker stdout, and enable autosubmit/reopen options from the aoc cli, fix pypy tests
  • Loading branch information
wimglenn authored Dec 7, 2020
1 parent 85eb5d8 commit f9ec2fe
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion aocd/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_data(session=None, day=None, year=None, block=False):
if not block:
raise
q = block == "q"
blocker(quiet=q)
blocker(quiet=q, until=(year, day))
return puzzle.input_data


Expand Down
8 changes: 6 additions & 2 deletions aocd/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def main():
parser.add_argument("-d", "--days", type=int, nargs="+", choices=days)
parser.add_argument("-u", "--users", nargs="+", choices=users)
parser.add_argument("-t", "--timeout", type=int, default=DEFAULT_TIMEOUT)
parser.add_argument("-s", "--no-submit", action="store_true", help="disable autosubmit")
parser.add_argument("-r", "--reopen", action="store_true", help="open browser on NEW solves")
parser.add_argument("--log-level", default="WARNING", choices=log_levels)
args = parser.parse_args()
if not users:
Expand All @@ -77,6 +79,8 @@ def main():
days=args.days or days,
datasets={k: users[k] for k in (args.users or users)},
timeout=args.timeout,
autosubmit=not args.no_submit,
reopen=args.reopen,
)


Expand Down Expand Up @@ -147,7 +151,7 @@ def run_one(year, day, input_data, entry_point, timeout=DEFAULT_TIMEOUT, progres
return a, b, walltime, error


def run_for(plugins, years, days, datasets, timeout=DEFAULT_TIMEOUT, autosubmit=True):
def run_for(plugins, years, days, datasets, timeout=DEFAULT_TIMEOUT, autosubmit=True, reopen=False):
aoc_now = datetime.now(tz=AOC_TZ)
all_entry_points = pkg_resources.iter_entry_points(group="adventofcode.user")
entry_points = {ep.name: ep for ep in all_entry_points if ep.name in plugins}
Expand Down Expand Up @@ -196,7 +200,7 @@ def run_for(plugins, years, days, datasets, timeout=DEFAULT_TIMEOUT, autosubmit=
post = part == "a" or (part == "b" and hasattr(puzzle, "answer_a"))
if autosubmit and post:
try:
puzzle._submit(answer, part, reopen=False, quiet=True)
puzzle._submit(answer, part, reopen=reopen, quiet=True)
except AocdError as err:
log.warning("error submitting - %s", err)
try:
Expand Down
26 changes: 17 additions & 9 deletions aocd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@
AOC_TZ = gettz("America/New_York")


def blocker(quiet=False, dt=0.1, datefmt="%-I:%M %p"):
def blocker(quiet=False, dt=0.1, datefmt="%-I:%M %p", until=None):
"""
This function just blocks until the next puzzle unlocks.
Pass `quiet=True` to disable the spinner etc.
Pass `dt` (seconds) to update the status txt more/less frequently.
Pass until=(year, day) to block until some other unlock date.
"""
aoc_now = datetime.now(tz=AOC_TZ)
year = aoc_now.year
month = 12
day = aoc_now.day + 1
if aoc_now.month < 12:
day = 1
elif aoc_now.day >= 25:
day = 1
year += 1
if until is not None:
year, day = until
else:
year = aoc_now.year
day = aoc_now.day + 1
if aoc_now.month < 12:
day = 1
elif aoc_now.day >= 25:
day = 1
year += 1
unlock = datetime(year, month, day, tzinfo=AOC_TZ)
if datetime.now(tz=AOC_TZ) > unlock:
# it should already be unlocked - nothing to do
return
spinner = cycle(r"\|/-")
localzone = tzlocal.get_localzone()
while datetime.now(tz=AOC_TZ) < unlock:
Expand All @@ -37,5 +44,6 @@ def blocker(quiet=False, dt=0.1, datefmt="%-I:%M %p"):
if not quiet:
sys.stdout.write("\r")
if not quiet:
sys.stdout.write("\r".ljust(80))
# clears the "Unlock day" countdown line from the terminal
sys.stdout.write("\r".ljust(80) + "\n")
sys.stdout.flush()
2 changes: 1 addition & 1 deletion aocd/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.0"
__version__ = "0.9.1"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="advent-of-code-data",
version="0.9.0",
version="0.9.1",
description="Get your puzzle data with a single import",
long_description=open("README.rst").read(),
long_description_content_type="text/x-rst",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_puzzle_not_available_yet_block(requests_mock, caplog, mocker):
aocd.get_data(year=2101, day=1, block="q")
assert mock.called
assert mock.call_count == 2
blocker.assert_called_once_with(quiet=True)
blocker.assert_called_once_with(quiet=True, until=(2101, 1))


def test_session_token_in_req_headers(requests_mock):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def test_main(capsys, mocker, aocd_dir):
days=[3, 7],
datasets={"data1": "token1", "data2": "token2"},
timeout=60,
autosubmit=True,
reopen=False,
)


Expand Down
17 changes: 12 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import platform
import pytest
from aocd.utils import blocker
from freezegun import freeze_time


# 0.2 second before unlock day 1
@freeze_time("2020-11-30 23:59:59.8-05:00", tick=True)
cpython = platform.python_implementation() == "CPython"


@pytest.mark.xfail(not cpython, reason="freezegun tick is not working on pypy")
def test_blocker(capsys):
blocker(dt=0.2)
with freeze_time("2020-11-30 23:59:59.8-05:00", tick=True):
# 0.2 second before unlock day 1
blocker(dt=0.2)
out, err = capsys.readouterr()
assert " Unlock day 1 at " in out


@freeze_time("2020-11-30 23:59:59.8-05:00", tick=True)
@pytest.mark.xfail(not cpython, reason="freezegun auto-tick is not working on pypy either")
def test_blocker_quiet(capsys):
blocker(dt=0.2, quiet=True)
with freeze_time("2020-11-30 23:59:59.8-05:00", auto_tick_seconds=1):
blocker(dt=0.2, quiet=True)
out, err = capsys.readouterr()
assert not out

0 comments on commit f9ec2fe

Please sign in to comment.