Skip to content

Commit

Permalink
fix: using context to manage in a clean way dynamic parameters passin…
Browse files Browse the repository at this point in the history
…g to pydantic models

this is required because the "os.chdir" solution (#193 (comment)) solved the path resolution for the image but bugged the path construction for paths in rendercv_settings (which should be relative to cwd and not to the path of the input file)
  • Loading branch information
kael-k committed Nov 7, 2024
1 parent a5a43bd commit bbe0ed2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
12 changes: 6 additions & 6 deletions rendercv/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,8 @@ def cli_command_render(
with printer.LiveProgressReporter(number_of_steps=number_of_steps) as progress:
progress.start_a_step("Validating the input file")

# Change the current working directory to the input file's directory (because
# the template overrides are looked up in the current working directory). The
# output files will be in the original working directory.
os.chdir(input_file_path.parent)

data_model = data.validate_input_dictionary_and_return_the_data_model(
input_file_as_a_dict
input_file_as_a_dict, input_file_path=input_file_path
)

render_command_settings: data.models.RenderCommandSettings = (
Expand All @@ -211,6 +206,11 @@ def cli_command_render(

progress.finish_the_current_step()

# Change the current working directory to the input file's directory (because
# the template overrides are looked up in the current working directory). The
# output files will be in the original working directory.
os.chdir(input_file_path.parent)

progress.start_a_step("Generating the LaTeX file")

latex_file_path_in_output_folder = (
Expand Down
9 changes: 9 additions & 0 deletions rendercv/data/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
models in RenderCV.
"""

from pathlib import Path
from typing import Optional

import pydantic


Expand All @@ -22,3 +25,9 @@ class RenderCVBaseModelWithExtraKeys(pydantic.BaseModel):
"""

model_config = pydantic.ConfigDict(extra="allow", validate_default=True)


class RenderCVContextModel(pydantic.BaseModel):
"""This class describe the context data structure that will be passed during model validation."""

input_file_path: Optional[Path]
7 changes: 7 additions & 0 deletions rendercv/data/models/curriculum_vitae.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,13 @@ def photo_path(
if not value:
return None
path = Path(value)
if path.is_absolute():
return path

if info.context.input_file_path:
return info.context.input_file_path.parent.joinpath(path).absolute()

# if no input file has been provided, then the path must be relative to cwd
return path.absolute()

@pydantic.field_validator("name")
Expand Down
8 changes: 7 additions & 1 deletion rendercv/data/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ruamel.yaml

from . import models
from .models.base import RenderCVContextModel


def read_a_yaml_file(file_path_or_contents: pathlib.Path | str) -> dict:
Expand Down Expand Up @@ -56,19 +57,24 @@ def read_a_yaml_file(file_path_or_contents: pathlib.Path | str) -> dict:

def validate_input_dictionary_and_return_the_data_model(
input_dictionary: dict,
input_file_path: pathlib.Path = None,
) -> models.RenderCVDataModel:
"""Validate the input dictionary by creating an instance of `RenderCVDataModel`,
which is a Pydantic data model of RenderCV's data format.
Args:
input_dictionary (dict): The input dictionary.
input_file_path (dict): Path of the input file.
Returns:
RenderCVDataModel: The data model.
"""

# Validate the parsed dictionary by creating an instance of RenderCVDataModel:
rendercv_data_model = models.RenderCVDataModel(**input_dictionary)
ctx = RenderCVContextModel(input_file_path=input_file_path)
rendercv_data_model = models.RenderCVDataModel.model_validate(
input_dictionary, context=ctx
)

return rendercv_data_model

Expand Down

0 comments on commit bbe0ed2

Please sign in to comment.