-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from GriceTurrble/feat/2024-cli
feat: 2024 cli
- Loading branch information
Showing
12 changed files
with
210 additions
and
37 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"] | ||
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.