user typer for CLI

This commit is contained in:
Sina Atalay 2023-10-20 19:34:06 +02:00
parent f94db9bf53
commit 7377d6491b
3 changed files with 53 additions and 18 deletions

View File

@ -16,6 +16,7 @@ dependencies = [
'pyspellchecker==0.7.2',
'ruamel.yaml==0.17.35',
'email-validator==2.0.0.post2',
'typer[all]==0.9.0',
]
classifiers = [
"Intended Audience :: Science/Research",

View File

@ -1,27 +1,61 @@
import sys
import os
import logging
from typing import Annotated
from .rendering import read_input_file, render_template, run_latex
import typer
from jinja2 import Environment, PackageLoader
def main(input_file_path=None):
if len(sys.argv) < 2:
if input_file_path is None:
raise ValueError("Please provide the input file path.")
elif len(sys.argv) == 2:
input_file_path = sys.argv[1]
else:
if input_file_path is None:
raise ValueError(
"More than one input is provided. Please provide only one input, which is"
" the input file path."
)
logger = logging.getLogger(__name__)
file_path = os.path.join(os.getcwd(), input_file_path)
data = read_input_file(file_path)
output_latex_file = render_template(data)
run_latex(output_latex_file)
app = typer.Typer(
help="RenderCV - A LateX CV generator from YAML",
add_completion=False,
pretty_exceptions_enable=True,
pretty_exceptions_short=True,
pretty_exceptions_show_locals=True,
)
@app.command(help="Render a YAML input file")
def render(
input_file: Annotated[
str,
typer.Argument(help="Name of the YAML input file"),
]
):
try:
file_path = os.path.abspath(input_file)
data = read_input_file(file_path)
output_latex_file = render_template(data)
run_latex(output_latex_file)
except Exception as e:
logger.critical(e)
typer.Abort()
@app.command(help="Generate a YAML input file to get started")
def new(name: Annotated[str, typer.Argument(help="Full name")]):
try:
environment = Environment(
loader=PackageLoader("rendercv", os.path.join("templates")),
trim_blocks=True,
lstrip_blocks=True,
)
environment.variable_start_string = "<<"
environment.variable_end_string = ">>"
template = environment.get_template(f"new_input.yaml.j2")
new_input_file = template.render(name=name)
name = name.replace(" ", "_")
file_name = f"{name}_CV.yaml"
with open(file_name, "w", encoding="utf-8") as file:
file.write(new_input_file)
except Exception as e:
logger.critical(e)
typer.Abort()
if __name__ == "__main__":
main()
app()

Binary file not shown.