mirror of https://github.com/eyhc1/rendercv.git
improve logging
This commit is contained in:
parent
4e48bc0c44
commit
ea87935e03
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from .rendering import read_input_file, render_template, run_latex
|
from .rendering import read_input_file, render_template, run_latex
|
||||||
|
@ -18,7 +19,68 @@ app = typer.Typer(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def user_friendly_errors(func):
|
||||||
|
"""Function decorator to make Pydantic's error messages more user-friendly.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
func (function): Function to decorate
|
||||||
|
Returns:
|
||||||
|
function: Decorated function
|
||||||
|
"""
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
func(*args, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
# Modify Pydantic's error message to make it more user-friendly
|
||||||
|
error_message = e.__repr__()
|
||||||
|
error_messages = error_message.split("\n")
|
||||||
|
for error_line in error_messages.copy():
|
||||||
|
new_error_line = None
|
||||||
|
|
||||||
|
if "RenderCVDataModel" in error_line:
|
||||||
|
new_error_line = error_line.replace(" for RenderCVDataModel", "!")
|
||||||
|
|
||||||
|
if "For further information" in error_line:
|
||||||
|
# Remove further information line
|
||||||
|
error_messages.remove(error_line)
|
||||||
|
|
||||||
|
# Modify Pydantic's error message:
|
||||||
|
match = re.match(
|
||||||
|
r"(.*) \[type=\w+, input_value=(.*), input_type=\w+\]",
|
||||||
|
error_line,
|
||||||
|
)
|
||||||
|
if match:
|
||||||
|
new_error_line = f"{match.group(1)}"
|
||||||
|
|
||||||
|
# Add a period at the end of the sentence if there is none
|
||||||
|
if not (new_error_line[-1] == "." or new_error_line[-1] == "!"):
|
||||||
|
new_error_line = new_error_line + "."
|
||||||
|
|
||||||
|
# If the input value is not a dictionary, add it to the error
|
||||||
|
# message
|
||||||
|
if "{" not in match.group(2):
|
||||||
|
new_error_line = (
|
||||||
|
new_error_line + f" The input was {match.group(2)}!"
|
||||||
|
)
|
||||||
|
# If the error line was modified, replace it
|
||||||
|
if new_error_line is not None:
|
||||||
|
error_messages[error_messages.index(error_line)] = new_error_line
|
||||||
|
|
||||||
|
error_message = "\n ".join(error_messages)
|
||||||
|
|
||||||
|
# Print the error message
|
||||||
|
logger.critical(error_message)
|
||||||
|
|
||||||
|
# Abort the program
|
||||||
|
logger.info("Aborting RenderCV.")
|
||||||
|
typer.Abort()
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
@app.command(help="Render a YAML input file")
|
@app.command(help="Render a YAML input file")
|
||||||
|
@user_friendly_errors
|
||||||
def render(
|
def render(
|
||||||
input_file: Annotated[
|
input_file: Annotated[
|
||||||
str,
|
str,
|
||||||
|
@ -30,40 +92,33 @@ def render(
|
||||||
Args:
|
Args:
|
||||||
input_file (str): Name of the YAML input file
|
input_file (str): Name of the YAML input file
|
||||||
"""
|
"""
|
||||||
try:
|
file_path = os.path.abspath(input_file)
|
||||||
file_path = os.path.abspath(input_file)
|
data = read_input_file(file_path)
|
||||||
data = read_input_file(file_path)
|
output_latex_file = render_template(data)
|
||||||
output_latex_file = render_template(data)
|
run_latex(output_latex_file)
|
||||||
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")
|
@app.command(help="Generate a YAML input file to get started")
|
||||||
|
@user_friendly_errors
|
||||||
def new(name: Annotated[str, typer.Argument(help="Full name")]):
|
def new(name: Annotated[str, typer.Argument(help="Full name")]):
|
||||||
"""Generate a YAML input file to get started.
|
"""Generate a YAML input file to get started.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name (str): Full name
|
name (str): Full name
|
||||||
"""
|
"""
|
||||||
try:
|
environment = Environment(
|
||||||
environment = Environment(
|
loader=PackageLoader("rendercv", os.path.join("templates")),
|
||||||
loader=PackageLoader("rendercv", os.path.join("templates")),
|
)
|
||||||
)
|
environment.variable_start_string = "<<"
|
||||||
environment.variable_start_string = "<<"
|
environment.variable_end_string = ">>"
|
||||||
environment.variable_end_string = ">>"
|
|
||||||
|
|
||||||
template = environment.get_template("new_input.yaml.j2")
|
template = environment.get_template("new_input.yaml.j2")
|
||||||
new_input_file = template.render(name=name)
|
new_input_file = template.render(name=name)
|
||||||
|
|
||||||
name = name.replace(" ", "_")
|
name = name.replace(" ", "_")
|
||||||
file_name = f"{name}_CV.yaml"
|
file_name = f"{name}_CV.yaml"
|
||||||
with open(file_name, "w", encoding="utf-8") as file:
|
with open(file_name, "w", encoding="utf-8") as file:
|
||||||
file.write(new_input_file)
|
file.write(new_input_file)
|
||||||
except Exception as e:
|
|
||||||
logger.critical(e)
|
|
||||||
typer.Abort()
|
|
||||||
|
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
|
@ -73,5 +128,6 @@ def cli():
|
||||||
"""
|
"""
|
||||||
app()
|
app()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cli()
|
cli()
|
||||||
|
|
Loading…
Reference in New Issue