From 36cd3b0af8729b7cc3a9965faca7a3daec7939d8 Mon Sep 17 00:00:00 2001 From: Akib Date: Wed, 4 Sep 2024 18:28:29 +0000 Subject: [PATCH] refactor: removed the `render_options` from `rendercv_settings` --- rendercv/cli/commands.py | 62 +++++----- rendercv/cli/utilities.py | 28 +++-- rendercv/data/models/render.py | 132 ---------------------- rendercv/data/models/rendercv_settings.py | 101 ++++++++++++++++- 4 files changed, 147 insertions(+), 176 deletions(-) delete mode 100644 rendercv/data/models/render.py diff --git a/rendercv/cli/commands.py b/rendercv/cli/commands.py index 5cf58888..463a5c38 100644 --- a/rendercv/cli/commands.py +++ b/rendercv/cli/commands.py @@ -139,18 +139,18 @@ def cli_command_render( # Get paths: input_file_path: pathlib.Path = utilities.string_to_file_path( - input_file_name + string=input_file_name ) # type: ignore # dictionary for command line arguments: cli_args = { "use_local_latex_command": use_local_latex_command, "output_folder_name": output_folder_name, - "latex_path": utilities.string_to_file_path(latex_path), - "pdf_path": utilities.string_to_file_path(pdf_path), - "markdown_path": utilities.string_to_file_path(markdown_path), - "html_path": utilities.string_to_file_path(html_path), - "png_path": utilities.string_to_file_path(png_path), + "latex_path": latex_path, + "pdf_path": pdf_path, + "markdown_path": markdown_path, + "html_path": html_path, + "png_path": png_path, "dont_generate_png": dont_generate_png, "dont_generate_markdown": dont_generate_markdown, "dont_generate_html": dont_generate_html, @@ -184,18 +184,16 @@ def cli_command_render( data_as_a_dict = utilities.parse_render_settings(data_as_a_dict, cli_args) - render_options = data_as_a_dict.get("rendercv_settings", {}).get( - "render_options", {} - ) + rendercv_settings = data_as_a_dict.get("rendercv_settings", dict()) # Calculate the number of steps: number_of_steps = 6 - if render_options.get("dont_generate_png", False): + if rendercv_settings.get("dont_generate_png", False): number_of_steps -= 1 - if render_options.get("dont_generate_markdown", False): + if rendercv_settings.get("dont_generate_markdown", False): number_of_steps -= 2 else: - if render_options.get("dont_generate_html", False): + if rendercv_settings.get("dont_generate_html", False): number_of_steps -= 1 with printer.LiveProgressReporter(number_of_steps=number_of_steps) as progress: @@ -205,8 +203,8 @@ def cli_command_render( data_as_a_dict ) - render_options = data_model.rendercv_settings.render_options - output_directory = working_directory / render_options.output_folder_name + rendercv_settings = data_model.rendercv_settings + output_directory = working_directory / rendercv_settings.output_folder_name progress.finish_the_current_step() @@ -216,10 +214,12 @@ def cli_command_render( data_model, output_directory ) ) - if render_options.latex_path: + if rendercv_settings.latex_path: utilities.copy_files( latex_file_path_in_output_folder, - render_options.latex_path, + utilities.string_to_file_path( + parent=working_directory, string=rendercv_settings.latex_path + ), ) progress.finish_the_current_step() @@ -227,48 +227,56 @@ def cli_command_render( pdf_file_path_in_output_folder = renderer.render_a_pdf_from_latex( latex_file_path_in_output_folder, use_local_latex_command ) - if render_options.pdf_path: + if rendercv_settings.pdf_path: utilities.copy_files( pdf_file_path_in_output_folder, - render_options.pdf_path, + utilities.string_to_file_path( + parent=working_directory, string=rendercv_settings.pdf_path + ), ) progress.finish_the_current_step() - if not render_options.dont_generate_png: + if not rendercv_settings.dont_generate_png: progress.start_a_step("Rendering PNG files from the PDF") png_file_paths_in_output_folder = renderer.render_pngs_from_pdf( pdf_file_path_in_output_folder ) - if render_options.png_path: + if rendercv_settings.png_path: utilities.copy_files( png_file_paths_in_output_folder, - render_options.png_path, + utilities.string_to_file_path( + parent=working_directory, string=rendercv_settings.png_path + ), ) progress.finish_the_current_step() - if not render_options.dont_generate_markdown: + if not rendercv_settings.dont_generate_markdown: progress.start_a_step("Generating the Markdown file") markdown_file_path_in_output_folder = renderer.create_a_markdown_file( data_model, output_directory ) - if render_options.markdown_path: + if rendercv_settings.markdown_path: utilities.copy_files( markdown_file_path_in_output_folder, - render_options.markdown_path, + utilities.string_to_file_path( + parent=working_directory, string=rendercv_settings.markdown_path + ), ) progress.finish_the_current_step() - if not render_options.dont_generate_html: + if not rendercv_settings.dont_generate_html: progress.start_a_step( "Rendering the Markdown file to a HTML (for Grammarly)" ) html_file_path_in_output_folder = renderer.render_an_html_from_markdown( markdown_file_path_in_output_folder ) - if render_options.html_path: + if rendercv_settings.html_path: utilities.copy_files( html_file_path_in_output_folder, - render_options.html_path, + utilities.string_to_file_path( + parent=working_directory, string=rendercv_settings.html_path + ), ) progress.finish_the_current_step() diff --git a/rendercv/cli/utilities.py b/rendercv/cli/utilities.py index e850a34f..0031583c 100644 --- a/rendercv/cli/utilities.py +++ b/rendercv/cli/utilities.py @@ -13,18 +13,26 @@ from typing import Optional import typer -def string_to_file_path(string: Optional[str]) -> Optional[pathlib.Path]: +def string_to_file_path( + string: Optional[str], parent: Optional[str] = None +) -> Optional[pathlib.Path]: """Convert a string to a pathlib.Path object. If the string is None, then return None. Args: + parent (Optional[str]): The parent directory of the file path. string (str): The string to be converted to a pathlib.Path object. Returns: pathlib.Path: The pathlib.Path object. """ + # check if the string is not None: if string is not None: - return pathlib.Path(string).absolute() + # check if the parent is not None: + if parent is not None: + return pathlib.Path(parent).absolute() / string + else: + return pathlib.Path(string).absolute() else: return None @@ -331,19 +339,15 @@ def parse_render_settings( } # update the data of the rendercv settings: - render_cv_settings = dictionary.get("rendercv_settings", dict()) - if render_cv_settings is None: - print("render_cv_settings is None") - render_cv_settings = dict() + rendercv_settings = dictionary.get("rendercv_settings", dict()) + if rendercv_settings is None: + rendercv_settings = dict() - # get the render options: - render_options = render_cv_settings.get("render_options", dict()) - render_options = update_render_settings( - render_options, cli_arguments, cli_arguments_default + rendercv_settings = update_render_settings( + rendercv_settings, cli_arguments, cli_arguments_default ) # update the data model with the rendercv settings: - render_cv_settings["render_options"] = render_options - dictionary["rendercv_settings"] = render_cv_settings + dictionary["rendercv_settings"] = rendercv_settings return dictionary diff --git a/rendercv/data/models/render.py b/rendercv/data/models/render.py deleted file mode 100644 index be837501..00000000 --- a/rendercv/data/models/render.py +++ /dev/null @@ -1,132 +0,0 @@ -""" -The `rendercv.models.render` module contains the data model of the -`rendercv_settings.render` field of the input file. -""" - -from typing import Optional - -import pydantic - -from .base import RenderCVBaseModelWithExtraKeys - - -class RenderOptions(RenderCVBaseModelWithExtraKeys): - """This class is the data model of the render options. The values of each field - updates the `rendercv_settings.render` dictionary. - """ - - output_folder_name: Optional[str] = pydantic.Field( - default="rendercv_output", - title="Output Folder Name", - description=( - "The name of the folder where the output files will be saved. The default" - ' value is "rendercv_output".' - ), - ) - - use_local_latex_command: Optional[str] = pydantic.Field( - default=None, - title="Local LaTeX Command", - description=( - "The command to compile the LaTeX file to a PDF file. The default value is" - ' "pdflatex".' - ), - ) - - pdf_path: Optional[str] = pydantic.Field( - default=None, - title="PDF Path", - description=( - "The path of the PDF file. If it is not provided, the PDF file will not be" - " generated. The default value is an empty string." - ), - ) - - latex_path: Optional[str] = pydantic.Field( - default=None, - title="LaTeX Path", - description=( - "The path of the LaTeX file. If it is not provided, the LaTeX file will not" - " be generated. The default value is an empty string." - ), - ) - - html_path: Optional[str] = pydantic.Field( - default=None, - title="HTML Path", - description=( - "The path of the HTML file. If it is not provided, the HTML file will not" - " be generated. The default value is an empty string." - ), - ) - - png_path: Optional[str] = pydantic.Field( - default=None, - title="PNG Path", - description=( - "The path of the PNG file. If it is not provided, the PNG file will not be" - " generated. The default value is an empty string." - ), - ) - - markdown_path: Optional[str] = pydantic.Field( - default=None, - title="Markdown Path", - description=( - "The path of the Markdown file. If it is not provided, the Markdown file" - " will not be generated. The default value is an empty string." - ), - ) - - dont_generate_html: Optional[bool] = pydantic.Field( - default=False, - title="Generate HTML Flag", - description=( - "A boolean value to determine whether the HTML file will be generated. The" - " default value is False." - ), - ) - - dont_generate_markdown: Optional[bool] = pydantic.Field( - default=False, - title="Generate Markdown Flag", - description=( - "A boolean value to determine whether the Markdown file will be generated." - " The default value is False." - ), - ) - - dont_generate_png: Optional[bool] = pydantic.Field( - default=False, - title="Generate PNG Flag", - description=( - "A boolean value to determine whether the PNG file will be generated. The" - " default value is False." - ), - ) - - @pydantic.field_validator( - "output_folder_name", - "pdf_path", - "latex_path", - "html_path", - "png_path", - "markdown_path", - "dont_generate_html", - "dont_generate_markdown", - "dont_generate_png", - ) - @classmethod - def update_settings( - cls, value: Optional[str], info: pydantic.ValidationInfo - ) -> Optional[str]: - """Update the `rendercv_settings` dictionary with the provided values.""" - if value: - render_options[info.field_name] = value # type: ignore - - return value - - -# Initialize the `rendercv_settings.render` dictionary with the default values -render_options: dict[str, str] = {} -RenderOptions() # Initialize the render options with the default values diff --git a/rendercv/data/models/rendercv_settings.py b/rendercv/data/models/rendercv_settings.py index 16db4126..f216ba65 100644 --- a/rendercv/data/models/rendercv_settings.py +++ b/rendercv/data/models/rendercv_settings.py @@ -8,7 +8,6 @@ from typing import Optional import pydantic from .base import RenderCVBaseModelWithExtraKeys -from .render import RenderOptions class RenderCVSettings(RenderCVBaseModelWithExtraKeys): @@ -16,14 +15,106 @@ class RenderCVSettings(RenderCVBaseModelWithExtraKeys): updates the `rendercv_settings` dictionary. """ - render_options: Optional[RenderOptions] = pydantic.Field( + output_folder_name: Optional[str] = pydantic.Field( + default="rendercv_output", + title="Output Folder Name", + description=( + "The name of the folder where the output files will be saved. The default" + ' value is "rendercv_output".' + ), + ) + + use_local_latex_command: Optional[str] = pydantic.Field( default=None, - title="Render Options", - description="The options to render the output files.", + title="Local LaTeX Command", + description=( + "The command to compile the LaTeX file to a PDF file. The default value is" + ' "pdflatex".' + ), + ) + + pdf_path: Optional[str] = pydantic.Field( + default=None, + title="PDF Path", + description=( + "The path of the PDF file. If it is not provided, the PDF file will not be" + " generated. The default value is an empty string." + ), + ) + + latex_path: Optional[str] = pydantic.Field( + default=None, + title="LaTeX Path", + description=( + "The path of the LaTeX file. If it is not provided, the LaTeX file will not" + " be generated. The default value is an empty string." + ), + ) + + html_path: Optional[str] = pydantic.Field( + default=None, + title="HTML Path", + description=( + "The path of the HTML file. If it is not provided, the HTML file will not" + " be generated. The default value is an empty string." + ), + ) + + png_path: Optional[str] = pydantic.Field( + default=None, + title="PNG Path", + description=( + "The path of the PNG file. If it is not provided, the PNG file will not be" + " generated. The default value is an empty string." + ), + ) + + markdown_path: Optional[str] = pydantic.Field( + default=None, + title="Markdown Path", + description=( + "The path of the Markdown file. If it is not provided, the Markdown file" + " will not be generated. The default value is an empty string." + ), + ) + + dont_generate_html: Optional[bool] = pydantic.Field( + default=False, + title="Generate HTML Flag", + description=( + "A boolean value to determine whether the HTML file will be generated. The" + " default value is False." + ), + ) + + dont_generate_markdown: Optional[bool] = pydantic.Field( + default=False, + title="Generate Markdown Flag", + description=( + "A boolean value to determine whether the Markdown file will be generated." + " The default value is False." + ), + ) + + dont_generate_png: Optional[bool] = pydantic.Field( + default=False, + title="Generate PNG Flag", + description=( + "A boolean value to determine whether the PNG file will be generated. The" + " default value is False." + ), ) @pydantic.field_validator( - "render_options", + "output_folder_name", + "pdf_path", + "latex_path", + "html_path", + "png_path", + "markdown_path", + "dont_generate_html", + "dont_generate_markdown", + "dont_generate_png", ) @classmethod def update_settings(