From 7377d6491bbabe3e6589f334b70e17bd2e9dbc1c Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Fri, 20 Oct 2023 19:34:06 +0200 Subject: [PATCH] user typer for CLI --- pyproject.toml | 1 + rendercv/__main__.py | 70 ++++++++++++++++++++++++++++++++----------- requirements.txt | Bin 462 -> 500 bytes 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1ce5062..b9ee883 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/rendercv/__main__.py b/rendercv/__main__.py index 223579e..3645325 100644 --- a/rendercv/__main__.py +++ b/rendercv/__main__.py @@ -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() diff --git a/requirements.txt b/requirements.txt index 98fcac376dc5de9e3e072bb7e1e8d6371b32410f..c737012c6980d3bc55206d066cfabd858971b19f 100644 GIT binary patch delta 46 xcmX@d{DpbLIYu>J1}=sYhDwG4hE#?khG>RFh8!S_Wv~T80|q??OCUC2000b42^jzY delta 7 Ocmeyue2#g;IYs~ugab$b