From 419706aa4846a5dd4beda0a0128ca39e5b5220ae Mon Sep 17 00:00:00 2001 From: Akib Date: Sun, 1 Sep 2024 15:20:18 +0000 Subject: [PATCH] fix: fixed the number of steps bug in the command.py --- examples/John_Doe_ClassicTheme_CV.yaml | 5 +- rendercv/cli/commands.py | 107 ++++++++++---------- rendercv/cli/printer.py | 16 +++ rendercv/cli/utilities.py | 22 ++++ rendercv/data/models/rendercv_data_model.py | 2 +- rendercv/data/models/rendercv_settings.py | 49 ++++----- rendercv/renderer/renderer.py | 1 - 7 files changed, 120 insertions(+), 82 deletions(-) diff --git a/examples/John_Doe_ClassicTheme_CV.yaml b/examples/John_Doe_ClassicTheme_CV.yaml index 2f3aab65..d601e855 100644 --- a/examples/John_Doe_ClassicTheme_CV.yaml +++ b/examples/John_Doe_ClassicTheme_CV.yaml @@ -143,6 +143,5 @@ design: show_timespan_in: [] rendercv_settings: - output_folder_name: output - no_html: true - no_png: false \ No newline at end of file + no_png: true + no_markdown: true diff --git a/rendercv/cli/commands.py b/rendercv/cli/commands.py index 6b948403..da8b2e60 100644 --- a/rendercv/cli/commands.py +++ b/rendercv/cli/commands.py @@ -5,7 +5,7 @@ commands of RenderCV. import os import pathlib -from typing import Annotated, Literal, Optional +from typing import Annotated, Optional import typer from rich import print @@ -57,7 +57,7 @@ def cli_command_render( "-o", help="Name of the output folder.", ), - ] = "rendercv_output", + ] = None, latex_path: Annotated[ Optional[str], typer.Option( @@ -142,14 +142,17 @@ def cli_command_render( input_file_name ) # type: ignore - paths: dict[ - Literal["latex", "pdf", "markdown", "html", "png"], Optional[pathlib.Path] - ] = { - "latex": utilities.string_to_file_path(latex_path), - "pdf": utilities.string_to_file_path(pdf_path), - "markdown": utilities.string_to_file_path(markdown_path), - "html": utilities.string_to_file_path(html_path), - "png": utilities.string_to_file_path(png_path), + # dictionary for command line arguments: + cli_args = { + "output_folder_name": output_folder_name, + "latex_path": latex_path, + "pdf_path": pdf_path, + "markdown_path": markdown_path, + "html_path": html_path, + "png_path": png_path, + "no_markdown": dont_generate_markdown, + "no_html": dont_generate_html, + "no_png": dont_generate_png, } # change the current working directory to the input file's directory (because @@ -163,17 +166,8 @@ def cli_command_render( # 4. render PNG files from the PDF # 5. generate the Markdown file # 6. render the Markdown file to a HTML (for Grammarly) - number_of_steps = 6 - if dont_generate_png: - number_of_steps = number_of_steps - 1 - if dont_generate_markdown: - # if the Markdown file is not generated, then the HTML file is not generated - number_of_steps = number_of_steps - 2 - else: - if dont_generate_html: - number_of_steps = number_of_steps - 1 - - with printer.LiveProgressReporter(number_of_steps) as progress: + initial_steps = 1 + with printer.LiveProgressReporter(number_of_steps=initial_steps) as progress: progress.start_a_step("Reading and validating the input file") data_as_a_dict = data.read_a_yaml_file(input_file_path) @@ -188,40 +182,35 @@ def cli_command_render( ) # update the data of the rendercv settings: render_cv_settings = data_as_a_dict.get("rendercv_settings", dict()) + render_cv_settings = utilities.build_rendercv_settings( + render_cv_settings, cli_args + ) + + # update the data model with the rendercv settings: + data_as_a_dict["rendercv_settings"] = render_cv_settings data_model = data.validate_input_dictionary_and_return_the_data_model( data_as_a_dict ) - # update the data model values with cli arguments: - if dont_generate_html is not None: - data_model.rendercv_settings.no_html = dont_generate_html - if dont_generate_markdown is not None: - data_model.rendercv_settings.no_markdown = dont_generate_markdown - if dont_generate_png is not None: - data_model.rendercv_settings.no_png = dont_generate_png - if paths["html"]: - data_model.rendercv_settings.html_path = paths["html"] - if paths["latex"]: - data_model.rendercv_settings.latex_path = paths["latex"] - if paths["markdown"]: - data_model.rendercv_settings.markdown_path = paths["markdown"] - if paths["pdf"]: - data_model.rendercv_settings.pdf_path = paths["pdf"] - if paths["png"]: - data_model.rendercv_settings.png_path = paths["png"] - if output_folder_name == "rendercv_output": - # check if the output folder name is specified in the input file: - if data_model.rendercv_settings.output_folder_name is None: - data_model.rendercv_settings.output_folder_name = output_folder_name - # don't update the output folder name if it is specified in the input file: - - else: - data_model.rendercv_settings.output_folder_name = output_folder_name - - output_directory = pathlib.Path.cwd() / data_model.rendercv_settings.output_folder_name + + output_directory = ( + pathlib.Path.cwd() / data_model.rendercv_settings.output_folder_name + ) progress.finish_the_current_step() + # Calculate the number of steps: + number_of_steps = 6 + if data_model.rendercv_settings.no_png: + number_of_steps -= 1 + if data_model.rendercv_settings.no_markdown: + number_of_steps -= 2 + else: + if data_model.rendercv_settings.no_html: + number_of_steps -= 1 + + progress.update_total_steps(number_of_steps) + progress.start_a_step("Generating the LaTeX file") latex_file_path_in_output_folder = ( renderer.create_a_latex_file_and_copy_theme_files( @@ -229,7 +218,10 @@ def cli_command_render( ) ) if data_model.rendercv_settings.latex_path: - utilities.copy_files(latex_file_path_in_output_folder, data_model.rendercv_settings.latex_path) + utilities.copy_files( + latex_file_path_in_output_folder, + data_model.rendercv_settings.latex_path, + ) progress.finish_the_current_step() progress.start_a_step("Rendering the LaTeX file to a PDF") @@ -237,7 +229,9 @@ def cli_command_render( latex_file_path_in_output_folder, use_local_latex_command ) if data_model.rendercv_settings.pdf_path: - utilities.copy_files(pdf_file_path_in_output_folder, data_model.rendercv_settings.pdf_path) + utilities.copy_files( + pdf_file_path_in_output_folder, data_model.rendercv_settings.pdf_path + ) progress.finish_the_current_step() if not data_model.rendercv_settings.no_png: @@ -246,7 +240,10 @@ def cli_command_render( pdf_file_path_in_output_folder ) if data_model.rendercv_settings.png_path: - utilities.copy_files(png_file_paths_in_output_folder, data_model.rendercv_settings.png_path) + utilities.copy_files( + png_file_paths_in_output_folder, + data_model.rendercv_settings.png_path, + ) progress.finish_the_current_step() if not data_model.rendercv_settings.no_markdown: @@ -256,7 +253,8 @@ def cli_command_render( ) if data_model.rendercv_settings.markdown_path: utilities.copy_files( - markdown_file_path_in_output_folder, data_model.rendercv_settings.markdown_path + markdown_file_path_in_output_folder, + data_model.rendercv_settings.markdown_path, ) progress.finish_the_current_step() @@ -268,7 +266,10 @@ def cli_command_render( markdown_file_path_in_output_folder ) if data_model.rendercv_settings.html_path: - utilities.copy_files(html_file_path_in_output_folder, data_model.rendercv_settings.html_path) + utilities.copy_files( + html_file_path_in_output_folder, + data_model.rendercv_settings.html_path, + ) progress.finish_the_current_step() diff --git a/rendercv/cli/printer.py b/rendercv/cli/printer.py index 525b5df2..efa67984 100644 --- a/rendercv/cli/printer.py +++ b/rendercv/cli/printer.py @@ -84,6 +84,22 @@ class LiveProgressReporter(rich.live.Live): f"{self.current_step_name} has started." ) + def update_total_steps(self, total_steps: int): + """Update the total number of steps. + + Args: + total_steps (int): The total number of steps. + """ + self.number_of_steps = total_steps + self.overall_progress.update( + self.overall_task_id, + total=total_steps, + description=( + f"[bold #AAAAAA]({self.current_step} out of" + f" {self.number_of_steps} steps finished)" + ), + ) + def finish_the_current_step(self): """Finish the current step and update the progress bars.""" self.step_progress.stop_task(self.current_step_id) diff --git a/rendercv/cli/utilities.py b/rendercv/cli/utilities.py index 16b4c285..62c563ba 100644 --- a/rendercv/cli/utilities.py +++ b/rendercv/cli/utilities.py @@ -105,6 +105,7 @@ def set_or_update_values( return dictionary + def copy_files(paths: list[pathlib.Path] | pathlib.Path, new_path: pathlib.Path): """Copy files to the given path. If there are multiple files, then rename the new path by adding a number to the end of the path. @@ -259,3 +260,24 @@ def parse_render_command_override_arguments( key_and_values[key] = value return key_and_values + + +def build_rendercv_settings( + dictionary: dict, + command_line_arguments: dict[str, str], +) -> dict[str, str]: + """Build the RenderCV settings dictionary by combining the dictionary and the command line arguments. + + Args: + dictionary (dict): The dictionary to be combined with the command line + arguments. + command_line_arguments (dict[str, str]): The command line arguments. + + Returns: + dict[str, str]: The combined dictionary. + """ + # Combine the dictionary and the command line arguments if the values are not None: + for key, value in command_line_arguments.items(): + if value is not None: + dictionary = set_or_update_a_value(dictionary, key, value) # type: ignore + return dictionary diff --git a/rendercv/data/models/rendercv_data_model.py b/rendercv/data/models/rendercv_data_model.py index a58084c2..324314f0 100644 --- a/rendercv/data/models/rendercv_data_model.py +++ b/rendercv/data/models/rendercv_data_model.py @@ -52,7 +52,7 @@ class RenderCVDataModel(RenderCVBaseModelWithoutExtraKeys): LocaleCatalog() return locale_catalog - + @pydantic.field_validator("rendercv_settings") @classmethod def initialize_rendercv_settings( diff --git a/rendercv/data/models/rendercv_settings.py b/rendercv/data/models/rendercv_settings.py index 44e3523d..addbd931 100644 --- a/rendercv/data/models/rendercv_settings.py +++ b/rendercv/data/models/rendercv_settings.py @@ -3,21 +3,21 @@ The `rendercv.models.rendercv_settings` module contains the data model of the `rendercv_settings` field of the input file. """ -from typing import Annotated, Literal, Optional +from typing import Optional -import annotated_types as at import pydantic + class RenderCVSettings(pydantic.BaseModel): """This class is the data model of the rendercv settings. The values of each field updates the `rendercv_settings` dictionary. """ - + model_config = pydantic.ConfigDict( extra="forbid", validate_default=True, # To initialize the rendercv settings with the default values ) - + output_folder_name: Optional[str] = pydantic.Field( default="rendercv_output", title="Output Folder Name", @@ -26,7 +26,7 @@ class RenderCVSettings(pydantic.BaseModel): ' value is "rendercv_output".' ), ) - + pdf_path: Optional[str] = pydantic.Field( default=None, title="PDF Path", @@ -35,7 +35,7 @@ class RenderCVSettings(pydantic.BaseModel): " generated. The default value is an empty string." ), ) - + latex_path: Optional[str] = pydantic.Field( default=None, title="LaTeX Path", @@ -44,16 +44,16 @@ class RenderCVSettings(pydantic.BaseModel): " 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." + "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", @@ -62,16 +62,16 @@ class RenderCVSettings(pydantic.BaseModel): " 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." + "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." ), ) - + no_html: Optional[bool] = pydantic.Field( default=False, title="Generate HTML Flag", @@ -80,7 +80,7 @@ class RenderCVSettings(pydantic.BaseModel): " default value is False." ), ) - + no_markdown: Optional[bool] = pydantic.Field( default=False, title="Generate Markdown Flag", @@ -89,7 +89,7 @@ class RenderCVSettings(pydantic.BaseModel): " The default value is False." ), ) - + no_png: Optional[bool] = pydantic.Field( default=False, title="Generate PNG Flag", @@ -98,9 +98,7 @@ class RenderCVSettings(pydantic.BaseModel): " default value is False." ), ) - - - + @pydantic.field_validator( "output_folder_name", "pdf_path", @@ -112,13 +110,16 @@ class RenderCVSettings(pydantic.BaseModel): "no_png", ) @classmethod - def update_settings(cls, value: Optional[str], info: pydantic.ValidationInfo) -> Optional[str]: + def update_settings( + cls, value: Optional[str], info: pydantic.ValidationInfo + ) -> Optional[str]: """Update the `rendercv_settings` dictionary with the provided values.""" if value: - rendercv_settings[info.field_name] = value # type: ignore - + rendercv_settings[info.field_name] = value # type: ignore + return value - + + # Initialize the rendercv settings with the default values rendercv_settings: dict[str, str] = {} -RenderCVSettings() # Initialize the rendercv settings with the default values \ No newline at end of file +RenderCVSettings() # Initialize the rendercv settings with the default values diff --git a/rendercv/renderer/renderer.py b/rendercv/renderer/renderer.py index b54d2684..8ea32acd 100644 --- a/rendercv/renderer/renderer.py +++ b/rendercv/renderer/renderer.py @@ -12,7 +12,6 @@ import sys from typing import Optional import fitz - import markdown from .. import data