Skip to content

Commit

Permalink
feat(cli): Replace click with typer
Browse files Browse the repository at this point in the history
`typer` still uses `click`, but has a much nicer API
that integrates well with the type hints we are using anyway.
  • Loading branch information
alexpovel committed Jul 21, 2022
1 parent 3007cba commit a50d347
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
74 changes: 48 additions & 26 deletions ancv/__main__.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
"""Render JSON resumes to rich ANSI text for terminal output.
Comes with a server serving either an API or a single file, and a CLI to render files
locally.
"""

import json
import sys
from typing import TextIO
from pathlib import Path
from typing import Optional

import click
import typer
from pydantic import ValidationError

import ancv.web.server
from ancv.data.models.resume import ResumeSchema
from ancv.visualization.templates import Template

app = typer.Typer(no_args_is_help=True, help=__doc__)
server_app = typer.Typer(no_args_is_help=True, help="Interacts with the web server.")

@click.group(context_settings={"show_default": True})
def cli() -> None:
pass
app.add_typer(server_app, name="serve")


@cli.command(help="Starts the web server.")
@click.option("--host", default="0.0.0.0", help="The hostname to bind to.")
@click.option("--port", default=8080, help="The port to bind to.")
@click.option(
"--path",
help="File system path for an HTTP server Unix domain socket.",
)
def serve(host: str, port: int, path: str) -> None:
@server_app.command()
def api(
host: str = typer.Argument("0.0.0.0", help="Hostname to bind to."),
port: int = typer.Argument(8080, help="Port to bind to."),
path: Optional[str] = typer.Argument(
None, help="File system path for an HTTP server UNIX domain socket."
),
) -> None:
"""Starts the web server and serves the API."""

ancv.web.server.run(host=host, port=port, path=path)


@cli.command(help="Locally renders the JSON resume at the given file path.")
@click.argument("file", type=click.File())
def render(file: TextIO) -> None:
contents = json.loads(file.read())
@app.command()
def render(
path: Path = typer.Argument(
Path("resume.json"),
help="File path to the JSON resume file.",
)
) -> None:
"""Locally renders the JSON resume at the given file path."""

with open(path, "r", encoding="utf8") as file:
contents = json.loads(file.read())

resume = ResumeSchema(**contents)
template = Template.from_model_config(resume)
output = template.render()
print(output)
return None


@cli.command(help="Checks the validity of the given JSON resume without rendering.")
@click.argument("file", type=click.File())
def validate(file: TextIO) -> None:
contents = json.loads(file.read())
ec = 0
@app.command()
def validate(
path: Path = typer.Argument(
Path("resume.json"),
help="File path to the JSON resume file.",
)
) -> None:
"""Checks the validity of the given JSON resume without rendering."""

with open(path, "r", encoding="utf8") as file:
contents = json.loads(file.read())

try:
ResumeSchema(**contents)
except ValidationError as e:
print(str(e))
ec = 1
raise typer.Exit(code=1)
else:
print("Pass!")
sys.exit(ec)


if __name__ == "__main__":
cli()
app()
4 changes: 2 additions & 2 deletions ancv/web/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import AsyncGenerator
from typing import AsyncGenerator, Optional

from aiohttp import ClientSession, web
from cachetools import TTLCache
Expand All @@ -14,7 +14,7 @@
_ROUTES = web.RouteTableDef()


def run(host: str, port: int, path: str) -> None:
def run(host: Optional[str], port: Optional[int], path: Optional[str]) -> None:
LOGGER.debug("Instantiating web application.")
app = web.Application()
LOGGER.debug("Adding routes.")
Expand Down
23 changes: 22 additions & 1 deletion poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ structlog = "^21.5.0"
cachetools = "^5.2.0"
humanize = "^4.1.0"
rich = "^12.4.4"
click = "^8.1.3"
typer = "^0.6.1"

[tool.poetry.dev-dependencies]
black = "^22.3.0"
Expand Down

0 comments on commit a50d347

Please sign in to comment.