solve type problems

This commit is contained in:
Sina Atalay 2023-10-13 22:32:15 +02:00
parent a05635a6d3
commit cd029ed88e
1 changed files with 17 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import shutil
from datetime import date from datetime import date
import logging import logging
import time import time
from typing import Optional
from rendercv.data_model import RenderCVDataModel from rendercv.data_model import RenderCVDataModel
@ -74,7 +75,7 @@ def markdown_to_latex(markdown_string: str) -> str:
return latex_string return latex_string
def markdown_link_to_url(value: str) -> bool: def markdown_link_to_url(value: str) -> str:
"""Convert a markdown link to a normal string URL. """Convert a markdown link to a normal string URL.
This function is used as a Jinja2 filter. This function is used as a Jinja2 filter.
@ -107,7 +108,9 @@ def markdown_link_to_url(value: str) -> bool:
raise ValueError("markdown_link_to_url should only be used on markdown links!") raise ValueError("markdown_link_to_url should only be used on markdown links!")
def make_it_something(value: str, something: str, match_str: str = None) -> str: def make_it_something(
value: str, something: str, match_str: Optional[str] = None
) -> str:
"""Make the matched parts of the string something. If the match_str is None, the """Make the matched parts of the string something. If the match_str is None, the
whole string will be made something. whole string will be made something.
@ -129,6 +132,8 @@ def make_it_something(value: str, something: str, match_str: str = None) -> str:
keyword = "underline" keyword = "underline"
elif something == "make_it_italic": elif something == "make_it_italic":
keyword = "textit" keyword = "textit"
else:
raise ValueError(f"Unknown keyword {something}!")
if match_str is None: if match_str is None:
return f"\\{keyword}{{{value}}}" return f"\\{keyword}{{{value}}}"
@ -140,7 +145,7 @@ def make_it_something(value: str, something: str, match_str: str = None) -> str:
return value return value
def make_it_bold(value: str, match_str: str = None) -> str: def make_it_bold(value: str, match_str: Optional[str] = None) -> str:
"""Make the matched parts of the string bold. If the match_str is None, the whole """Make the matched parts of the string bold. If the match_str is None, the whole
string will be made bold. string will be made bold.
@ -162,7 +167,7 @@ def make_it_bold(value: str, match_str: str = None) -> str:
return make_it_something(value, "make_it_bold", match_str) return make_it_something(value, "make_it_bold", match_str)
def make_it_underlined(value: str, match_str: str = None) -> str: def make_it_underlined(value: str, match_str: Optional[str] = None) -> str:
"""Make the matched parts of the string underlined. If the match_str is None, the """Make the matched parts of the string underlined. If the match_str is None, the
whole string will be made underlined. whole string will be made underlined.
@ -184,7 +189,7 @@ def make_it_underlined(value: str, match_str: str = None) -> str:
return make_it_something(value, "make_it_underlined", match_str) return make_it_something(value, "make_it_underlined", match_str)
def make_it_italic(value: str, match_str: str = None) -> str: def make_it_italic(value: str, match_str: Optional[str] = None) -> str:
"""Make the matched parts of the string italic. If the match_str is None, the whole """Make the matched parts of the string italic. If the match_str is None, the whole
string will be made italic. string will be made italic.
@ -212,7 +217,7 @@ def divide_length_by(length: str, divider: float) -> str:
Length is a string with the following regex pattern: `\d+\.?\d* *(cm|in|pt|mm|ex|em)` Length is a string with the following regex pattern: `\d+\.?\d* *(cm|in|pt|mm|ex|em)`
""" """
# Get the value as a float and the unit as a string: # Get the value as a float and the unit as a string:
value = re.search(r"\d+\.?\d*", length).group() value = re.search(r"\d+\.?\d*", length).group() # type: ignore
unit = re.findall(r"[^\d\.\s]+", length)[0] unit = re.findall(r"[^\d\.\s]+", length)[0]
return str(float(value) / divider) + " " + unit return str(float(value) / divider) + " " + unit
@ -249,6 +254,11 @@ def read_input_file(file_path: str) -> RenderCVDataModel:
""" """
start_time = time.time() start_time = time.time()
logger.info(f"Reading and validating the input file {file_path} has started.") logger.info(f"Reading and validating the input file {file_path} has started.")
# check if the file exists:
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} doesn't exist!")
with open(file_path) as file: with open(file_path) as file:
yaml = YAML() yaml = YAML()
raw_json = yaml.load(file) raw_json = yaml.load(file)
@ -264,7 +274,7 @@ def read_input_file(file_path: str) -> RenderCVDataModel:
return data return data
def render_template(data: RenderCVDataModel, output_path: str = None) -> str: def render_template(data: RenderCVDataModel, output_path: Optional[str] = None) -> str:
"""Render the template using the given data. """Render the template using the given data.
Args: Args: