slowly move away from utilities.py

This commit is contained in:
Sina Atalay 2024-01-30 19:49:57 +01:00
parent bb2e5c05cd
commit 07be2e78fd
1 changed files with 6 additions and 74 deletions

View File

@ -5,7 +5,6 @@ import os
from ruamel.yaml import YAML
from . import data_models as dm
from .terminal_reporter import warning, error, information
@ -64,7 +63,7 @@ def escape_latex_characters(sentence: str) -> str:
return sentence
def parse_date_string(date_string: str) -> Date | int:
def parse_date_string(date_string: str) -> Date:
"""Parse a date string in YYYY-MM-DD, YYYY-MM, or YYYY format and return a
datetime.date object.
@ -80,38 +79,17 @@ def parse_date_string(date_string: str) -> Date | int:
# Then it is in YYYY-MM format
# Assign a random day since days are not rendered in the CV
date = Date.fromisoformat(f"{date_string}-01")
elif re.match(r"\d{4}", date_string):
# Then it is in YYYY format
# Then keep it as an integer
date = int(date_string)
else:
raise ValueError(
f'The date string "{date_string}" is not in YYYY-MM-DD, YYYY-MM, or YYYY'
" format."
)
if isinstance(date, Date):
# Then it means the date is a Date object, so check if it is a past date:
if date > Date.today():
raise ValueError(
f'The date "{date_string}" is in the future. Please check the dates.'
)
elif isinstance(date, int):
# Then it means the date is an integer, so check if it is a past date:
if date > Date.today().year:
raise ValueError(
f'The date "{date_string}" is in the future. Please check the dates.'
)
elif not isinstance(date, str):
raise RuntimeError(
"This error shouldn't have been raised. Please open an issue on GitHub."
)
return date
def format_date(date: Date | int) -> str:
"""Formats a date to a string in the following format: "Jan. 2021".
def format_date(date: Date) -> str:
"""Formats a `Date` object to a string in the following format: "Jan. 2021".
It uses month abbreviations, taken from
[Yale University Library](https://web.library.yale.edu/cataloging/months).
@ -130,13 +108,6 @@ def format_date(date: Date | int) -> str:
Returns:
str: The formatted date.
"""
if not isinstance(date, (Date, int)):
raise TypeError("date is not a Date object or an integer!")
if isinstance(date, int):
# Then it means the user only provided the year, so just return the year
return str(date)
# Month abbreviations,
# taken from: https://web.library.yale.edu/cataloging/months
abbreviations_of_months = [
@ -155,47 +126,8 @@ def format_date(date: Date | int) -> str:
]
month = int(date.strftime("%m"))
monthAbbreviation = abbreviations_of_months[month - 1]
year = date.strftime("%Y")
date_string = f"{monthAbbreviation} {year}"
month_abbreviation = abbreviations_of_months[month - 1]
year = date.strftime(format="%Y")
date_string = f"{month_abbreviation} {year}"
return date_string
def read_input_file(file_path: str) -> dm.RenderCVDataModel:
"""Read the input file and return an instance of RenderCVDataModel.
Args:
file_path (str): The path to the input file.
Returns:
str: The input file as a string.
"""
start_time = time.time()
information(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 input file {file_path} doesn't exist.")
# check the file extension:
accepted_extensions = [".yaml", ".yml", ".json", ".json5"]
if not any(file_path.endswith(extension) for extension in accepted_extensions):
raise ValueError(
f"The file {file_path} doesn't have an accepted extension!"
f" Accepted extensions are: {accepted_extensions}"
)
with open(file_path) as file:
yaml = YAML()
raw_json = yaml.load(file)
data = RenderCVDataModel(**raw_json)
end_time = time.time()
time_taken = end_time - start_time
information(
f"Reading and validating the input file {file_path} has finished in"
f" {time_taken:.2f} s."
)
return data