Skip to content

Commit

Permalink
Merge pull request #146 from GriceTurrble/feat/2024-cli
Browse files Browse the repository at this point in the history
feat: 2024 cli
  • Loading branch information
GriceTurrble authored Dec 1, 2024
2 parents dd6a130 + 0ddea7a commit bcbcff3
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 37 deletions.
Empty file added 2024/Justfile
Empty file.
23 changes: 15 additions & 8 deletions 2024/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ name = "grice-py-aoc-2024"
version = "2024.12.1"
description = "Add your description here"
readme = "README.md"
authors = [{ name = "Galen Rice", email = "[email protected]" }]
requires-python = ">=3.13"
dependencies = ["httpx>=0.28.0", "polars>=1.16.0"]

[project.scripts]
day01 = "grice_py_aoc_2024.day01.main:main"
dependencies = [
"click>=8.1.7",
"httpx>=0.28.0",
"polars>=1.16.0",
"toml>=0.10.2",
]
[[project.authors]]
name = "Galen Rice"
email = "[email protected]"

[dependency-groups]
dev = ["pytest>=8.3.4"]
Expand All @@ -17,7 +21,10 @@ dev = ["pytest>=8.3.4"]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project.scripts]
cli = "grice_py_aoc_2024.cli:cli"
day01 = "grice_py_aoc_2024.day01.main:main"
day02 = "grice_py_aoc_2024.day02.main:main"

[tool.pytest.ini_options]
addopts = """
--verbose
"""
addopts = "--verbose"
70 changes: 70 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from pathlib import Path
import toml

import click

DIR = Path(__file__).parent
MAIN_PY_TEMPLATE = DIR / "main.py-tpl"
TEST_PY_TEMPLATE = DIR / "test_day.py-tpl"


@click.group()
def cli():
pass


def _add_day_to_pyproject_toml(day: int) -> None:
pyproject_toml = DIR / "../../pyproject.toml"
if not pyproject_toml.exists():
raise click.ClickException(f"pyproject.toml not found at {pyproject_toml}")

content = toml.loads(pyproject_toml.read_text())
day_key = f"day{day:0>2}"
scripts = content["project"]["scripts"]
if day_key in scripts:
raise click.ClickException(f"Day {day} script already exists in pyproject.toml")
scripts[day_key] = f"grice_py_aoc_2024.{day_key}.main:main"

pyproject_toml.write_text(toml.dumps(content))


def _create_day_files(day: int) -> None:
day_path = DIR / f"day{day:0>2}"
if day_path.exists():
raise click.ClickException(f"Day {day} already exists at {day_path}")

day_path.mkdir()
(day_path / "__init__.py").touch()
(day_path / "inputs.txt").touch()
(day_path / "test_inputs.txt").touch()
_create_templates(day=day, path=day_path)


def _create_templates(day: int, path: Path) -> None:
# Create the file for the main code from template
main_py = path / "main.py"
main_py.touch()
main_py.write_text(MAIN_PY_TEMPLATE.read_text())

# Create the file for the test code from template
test_py = path / f"test_day{day:0>2}.py"
test_py.touch()
test_py.write_text(TEST_PY_TEMPLATE.read_text())


@cli.command()
@click.argument("day", type=int)
def create_day(day: int) -> None:
day_path = DIR / f"day{day:0>2}"
if day_path.exists():
raise click.ClickException(f"Day {day} already exists at {day_path}")
_create_day_files(day=day)
_add_day_to_pyproject_toml(day=day)

print(f">> Day {day} generated at:\n {day_path}/main.py")
print(f">> You can run the day's code using `uv run day{day:0>2}`")
print(" and run tests for this and any other day using `uv run pytest`")


if __name__ == "__main__":
cli()
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/day02/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from pathlib import Path
import typing

if typing.TYPE_CHECKING:
from io import TextIOWrapper

FILE = Path(__file__).parent / "inputs.txt"


def part1(inputs: TextIOWrapper):
raise NotImplementedError


def part2(inputs: TextIOWrapper):
raise NotImplementedError


def main():
with open(FILE) as f:
print(f"PART 1: {part1(f)}")

with open(FILE) as f:
print(f"PART 2: {part2(f)}")


if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/day02/test_day02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from pathlib import Path

from .main import part1, part2

TEST_FILE = Path(__file__).parent / "test_inputs.txt"

RESULT_PART_1 = 123
RESULT_PART_2 = 456

def test_part1():
with open(TEST_FILE) as f:
result = part1(f)
assert result == RESULT_PART_1


def test_part2():
with open(TEST_FILE) as f:
result = part2(f)
assert result == RESULT_PART_2
Empty file.
29 changes: 29 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/main.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from pathlib import Path
import typing

if typing.TYPE_CHECKING:
from io import TextIOWrapper

FILE = Path(__file__).parent / "inputs.txt"


def part1(inputs: TextIOWrapper):
raise NotImplementedError


def part2(inputs: TextIOWrapper):
raise NotImplementedError


def main():
with open(FILE) as f:
print(f"PART 1: {part1(f)}")

with open(FILE) as f:
print(f"PART 2: {part2(f)}")


if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions 2024/python/src/grice_py_aoc_2024/test_day.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from pathlib import Path

from .main import part1, part2

TEST_FILE = Path(__file__).parent / "test_inputs.txt"

RESULT_PART_1 = 123
RESULT_PART_2 = 456

def test_part1():
with open(TEST_FILE) as f:
result = part1(f)
assert result == RESULT_PART_1


def test_part2():
with open(TEST_FILE) as f:
result = part2(f)
assert result == RESULT_PART_2
25 changes: 25 additions & 0 deletions 2024/python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 0 additions & 29 deletions Makefile

This file was deleted.

0 comments on commit bcbcff3

Please sign in to comment.