From 30c1ffeafdbb2f272b4d134500ed3b719e4875fd Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Fri, 10 May 2024 15:56:34 +0300 Subject: [PATCH] data_models: generalize read_input_file --- rendercv/data_models.py | 45 ++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/rendercv/data_models.py b/rendercv/data_models.py index f02083f..0383981 100644 --- a/rendercv/data_models.py +++ b/rendercv/data_models.py @@ -1275,7 +1275,7 @@ def set_or_update_a_value( def read_input_file( - file_path: pathlib.Path, + file_path_or_contents: pathlib.Path | str, ) -> RenderCVDataModel: """Read the input file and return two instances of [RenderCVDataModel](#rendercv.data_models.RenderCVDataModel). The first instance is @@ -1288,26 +1288,33 @@ def read_input_file( Returns: RenderCVDataModel: The data models with $\\LaTeX$ and markdown strings. """ - # check if the file exists: - if not file_path.exists(): - raise FileNotFoundError( - f"The input file [magenta]{file_path}[/magenta] doesn't exist!" - ) + if isinstance(file_path_or_contents, pathlib.Path): + # check if the file exists: + if not file_path_or_contents.exists(): + raise FileNotFoundError( + f"The input file [magenta]{file_path_or_contents}[/magenta] doesn't" + " exist!" + ) - # check the file extension: - accepted_extensions = [".yaml", ".yml", ".json", ".json5"] - if file_path.suffix not in accepted_extensions: - user_friendly_accepted_extensions = [ - f"[green]{ext}[/green]" for ext in accepted_extensions - ] - user_friendly_accepted_extensions = ", ".join(user_friendly_accepted_extensions) - raise ValueError( - "The input file should have one of the following extensions:" - f" {user_friendly_accepted_extensions}. The input file is" - f" [magenta]{file_path}[/magenta]." - ) + # check the file extension: + accepted_extensions = [".yaml", ".yml", ".json", ".json5"] + if file_path_or_contents.suffix not in accepted_extensions: + user_friendly_accepted_extensions = [ + f"[green]{ext}[/green]" for ext in accepted_extensions + ] + user_friendly_accepted_extensions = ", ".join( + user_friendly_accepted_extensions + ) + raise ValueError( + "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 # validate the parsed dictionary by creating an instance of RenderCVDataModel: