mirror of https://github.com/eyhc1/rendercv.git
data_models: generalize read_input_file
This commit is contained in:
parent
7de8ef8a4a
commit
30c1ffeafd
|
@ -1275,7 +1275,7 @@ def set_or_update_a_value(
|
||||||
|
|
||||||
|
|
||||||
def read_input_file(
|
def read_input_file(
|
||||||
file_path: pathlib.Path,
|
file_path_or_contents: pathlib.Path | str,
|
||||||
) -> RenderCVDataModel:
|
) -> RenderCVDataModel:
|
||||||
"""Read the input file and return two instances of
|
"""Read the input file and return two instances of
|
||||||
[RenderCVDataModel](#rendercv.data_models.RenderCVDataModel). The first instance is
|
[RenderCVDataModel](#rendercv.data_models.RenderCVDataModel). The first instance is
|
||||||
|
@ -1288,26 +1288,33 @@ def read_input_file(
|
||||||
Returns:
|
Returns:
|
||||||
RenderCVDataModel: The data models with $\\LaTeX$ and markdown strings.
|
RenderCVDataModel: The data models with $\\LaTeX$ and markdown strings.
|
||||||
"""
|
"""
|
||||||
# check if the file exists:
|
if isinstance(file_path_or_contents, pathlib.Path):
|
||||||
if not file_path.exists():
|
# check if the file exists:
|
||||||
raise FileNotFoundError(
|
if not file_path_or_contents.exists():
|
||||||
f"The input file [magenta]{file_path}[/magenta] doesn't exist!"
|
raise FileNotFoundError(
|
||||||
)
|
f"The input file [magenta]{file_path_or_contents}[/magenta] doesn't"
|
||||||
|
" exist!"
|
||||||
|
)
|
||||||
|
|
||||||
# check the file extension:
|
# check the file extension:
|
||||||
accepted_extensions = [".yaml", ".yml", ".json", ".json5"]
|
accepted_extensions = [".yaml", ".yml", ".json", ".json5"]
|
||||||
if file_path.suffix not in accepted_extensions:
|
if file_path_or_contents.suffix not in accepted_extensions:
|
||||||
user_friendly_accepted_extensions = [
|
user_friendly_accepted_extensions = [
|
||||||
f"[green]{ext}[/green]" for ext in accepted_extensions
|
f"[green]{ext}[/green]" for ext in accepted_extensions
|
||||||
]
|
]
|
||||||
user_friendly_accepted_extensions = ", ".join(user_friendly_accepted_extensions)
|
user_friendly_accepted_extensions = ", ".join(
|
||||||
raise ValueError(
|
user_friendly_accepted_extensions
|
||||||
"The input file should have one of the following extensions:"
|
)
|
||||||
f" {user_friendly_accepted_extensions}. The input file is"
|
raise ValueError(
|
||||||
f" [magenta]{file_path}[/magenta]."
|
"The input file should have one of the following extensions:"
|
||||||
)
|
f" {user_friendly_accepted_extensions}. The input file is"
|
||||||
|
f" [magenta]{file_path_or_contents}[/magenta]."
|
||||||
|
)
|
||||||
|
|
||||||
|
file_content = file_path_or_contents.read_text(encoding="utf-8")
|
||||||
|
else:
|
||||||
|
file_content = file_path_or_contents
|
||||||
|
|
||||||
file_content = file_path.read_text(encoding="utf-8")
|
|
||||||
input_as_dictionary: dict[str, Any] = ruamel.yaml.YAML().load(file_content) # type: ignore
|
input_as_dictionary: dict[str, Any] = ruamel.yaml.YAML().load(file_content) # type: ignore
|
||||||
|
|
||||||
# validate the parsed dictionary by creating an instance of RenderCVDataModel:
|
# validate the parsed dictionary by creating an instance of RenderCVDataModel:
|
||||||
|
|
Loading…
Reference in New Issue