-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`typer` still uses `click`, but has a much nicer API that integrates well with the type hints we are using anyway.
- Loading branch information
Showing
4 changed files
with
73 additions
and
30 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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() |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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