import runpy from pathlib import Path from profile import run from tarfile import SOLARIS_XHDTYPE import typer app = typer.Typer() @app.command("add", help="Add a new problem to the projecteuler repository.") def add_newproblem(num: int, name: str | None = None) -> None: """Add a new problem to the projecteuler repository.""" typer.echo(f"Adding problem {num} to the projecteuler repository.") if name: title = f"{num:04d}.{name}" else: title = f"{num:04d}" Path(f"solutions/{title}").mkdir(parents=True, exist_ok=True) Path(f"solutions/{title}/euler_{num}.py").touch() @app.command("list", help="List all problems in the projecteuler repository.") def list_problems(every: bool = False): """List all problems in the projecteuler repository.""" if every: typer.echo("Listing all problems in the projecteuler repository.") else: typer.echo("Listing near problems in the projecteuler repository.") for path in Path("solutions").iterdir(): if path.is_dir(): if every: tmp = list(path.iterdir()) tmp = all(ff.is_dir() for ff in tmp) if tmp: for ff in path.iterdir(): if ff.is_dir(): typer.echo(f"{ff.name}") else: typer.echo(f"{path.name}") else: if "_" in path.name: typer.echo(f"{path.name} (completed)") else: typer.echo(f"{path.name}") @app.command("solution", help="Run a solution for a given problem.") def run_solution(num: int) -> None: folders = list(Path("solutions").iterdir()) folders = [ folder for folder in folders if folder.is_dir() and folder.name.startswith(f"{num:04d}") ] runpy.run_path( (folders[0] / f"euler_{num}.py").resolve().as_posix(), run_name="__main__" ) @app.command("version", help="Display the version of the projecteuler CLI.") def version(): """Display the version of the projecteuler CLI.""" typer.echo("projecteuler solution version 0.1.0") if __name__ == "__main__": app()