From dbe7db329c40276b6344c45fdd5aa468a7796ca0 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Fri, 5 Jul 2024 01:22:37 +0300 Subject: [PATCH] data: finalize refactoring --- rendercv/data/__init__.py | 20 +++++++++---------- rendercv/data/generator.py | 9 +++++++-- rendercv/data/models/__init__.py | 22 +++++++++++++++++++++ rendercv/data/models/base.py | 5 +++++ rendercv/data/models/computers.py | 10 +++++----- rendercv/data/models/curriculum_vitae.py | 10 +++++++--- rendercv/data/models/design.py | 13 ++++++++---- rendercv/data/models/entry_types.py | 6 ++++++ rendercv/data/models/locale_catalog.py | 5 +++++ rendercv/data/models/rendercv_data_model.py | 7 +++---- rendercv/data/reader.py | 11 ++++------- 11 files changed, 82 insertions(+), 36 deletions(-) diff --git a/rendercv/data/__init__.py b/rendercv/data/__init__.py index 25ef9302..b1e5f205 100644 --- a/rendercv/data/__init__.py +++ b/rendercv/data/__init__.py @@ -1,5 +1,5 @@ """ -The `rendercv.data_models` package contains the necessary classes and functions for +The `rendercv.data` package contains the necessary classes and functions for - Parsing and validating a YAML input file - Computing some properties based on a YAML input file (like converting ISO dates to @@ -11,6 +11,12 @@ The validators and data format of RenderCV are written using [Pydantic](https://github.com/pydantic/pydantic). """ +from .generator import ( + create_a_sample_data_model, + create_a_sample_yaml_input_file, + generate_json_schema, + generate_json_schema_file, +) from .models import ( BulletEntry, CurriculumVitae, @@ -23,18 +29,11 @@ from .models import ( PublicationEntry, RenderCVDataModel, SocialNetwork, + available_social_networks, available_theme_options, format_date, - available_social_networks, -) -from .reader import ( - create_a_sample_data_model, - create_a_sample_yaml_input_file, - generate_json_schema, - generate_json_schema_file, - read_input_file, - set_or_update_a_value, ) +from .reader import read_input_file __all__ = [ "OneLineEntry", @@ -53,7 +52,6 @@ __all__ = [ "create_a_sample_yaml_input_file", "generate_json_schema_file", "generate_json_schema", - "set_or_update_a_value", "read_input_file", "format_date", "Entry", diff --git a/rendercv/data/generator.py b/rendercv/data/generator.py index fd408c54..bfba110b 100644 --- a/rendercv/data/generator.py +++ b/rendercv/data/generator.py @@ -1,3 +1,8 @@ +""" +The `rendercv.data.generators` module contains all the functions for generating the JSON +Schema of the input data format and a sample YAML input file. +""" + import io import json import pathlib @@ -6,7 +11,7 @@ from typing import Any, Optional import pydantic import ruamel.yaml -from . import models +from . import models, reader def dictionary_to_yaml(dictionary: dict[str, Any]): @@ -47,7 +52,7 @@ def create_a_sample_data_model( # read the sample_content.yaml file sample_content = pathlib.Path(__file__).parent / "sample_content.yaml" - sample_content_dictionary = read_a_yaml_file(sample_content) + sample_content_dictionary = reader.read_a_yaml_file(sample_content) cv = models.CurriculumVitae(**sample_content_dictionary) # Update the name: diff --git a/rendercv/data/models/__init__.py b/rendercv/data/models/__init__.py index 4271915d..1526387e 100644 --- a/rendercv/data/models/__init__.py +++ b/rendercv/data/models/__init__.py @@ -1,3 +1,25 @@ +""" +The `rendercv.data.models` package contains all the Pydantic data models, validators, +and computed fields that are used in RenderCV. The package is divided into several +modules, each containing a different group of data models. + +- `base.py`: Contains `RenderCVBaseModel`, which is the parent class of all the data + models in RenderCV. +- `computers.py`: Contains all the functions that are used to compute some values of + the fields in the data models. For example, converting ISO dates to human-readable + dates. +- `entry_types.py`: Contains all the data models that are used to represent the entries + in the CV. +- `curriculum_vitae.py`: Contains the `CurriculumVitae` data model, which is the main + data model that contains all the content of the CV. +- `design.py`: Contains the data model that is used to represent the design options of + the CV. +- `locale_catalog.py`: Contains the data model that is used to represent the locale + catalog of the CV. +- `rendercv_data_model.py`: Contains the `RenderCVDataModel` data model, which is the + main data model that defines the whole input file structure. +""" + from .computers import format_date from .curriculum_vitae import CurriculumVitae, SocialNetwork, available_social_networks from .design import available_theme_options diff --git a/rendercv/data/models/base.py b/rendercv/data/models/base.py index b4e9523c..99c0907f 100644 --- a/rendercv/data/models/base.py +++ b/rendercv/data/models/base.py @@ -1,3 +1,8 @@ +""" +The `rendercv.data.models.base` module contains the `RenderCVBaseModel` class, which is +the parent class of all the data models in RenderCV. +""" + import pydantic diff --git a/rendercv/data/models/computers.py b/rendercv/data/models/computers.py index 2e4de48b..8bf90871 100644 --- a/rendercv/data/models/computers.py +++ b/rendercv/data/models/computers.py @@ -1,14 +1,14 @@ """ -The `rendercv.data_models.computed_fields` module contains functions that compute -some properties based on the input data. For example, it includes functions that -calculate the time span between two dates, the date string, the URL of a social network, -etc. +The `rendercv.data.models.computers` module contains functions that compute some +properties based on the input data. For example, it includes functions that calculate +the time span between two dates, the date string, the URL of a social network, etc. """ import re from datetime import date as Date from typing import Optional +from .curriculum_vitae import CurriculumVitae from .locale_catalog import locale_catalog @@ -246,7 +246,7 @@ def compute_social_network_url(network: str, username: str): return url -def compute_connections(cv) -> list[dict[str, str]]: +def compute_connections(cv: "CurriculumVitae") -> list[dict[str, str]]: """Bring together all the connections in the CV, such as social networks, phone number, email, etc and return them as a list of dictionaries. Each dictionary contains the following keys: "latex_icon", "url", "clean_url", and "placeholder." diff --git a/rendercv/data/models/curriculum_vitae.py b/rendercv/data/models/curriculum_vitae.py index 3ea50ed0..0527bddb 100644 --- a/rendercv/data/models/curriculum_vitae.py +++ b/rendercv/data/models/curriculum_vitae.py @@ -1,3 +1,8 @@ +""" +The `rendercv.data.models.curriculum_vitae` module contains the data model of the `cv` +field of the input file. +""" + import functools import re from typing import Annotated, Any, Literal, Optional, Type, get_args @@ -5,8 +10,7 @@ from typing import Annotated, Any, Literal, Optional, Type, get_args import pydantic import pydantic_extra_types.phone_numbers as pydantic_phone_numbers -from . import computers as computers -from . import entry_types +from . import computers, entry_types from .base import RenderCVBaseModel # ====================================================================================== @@ -411,7 +415,7 @@ class CurriculumVitae(RenderCVBaseModel): if self.sections_input is not None: for title, entries in self.sections_input.items(): - title = util.dictionary_key_to_proper_section_title(title) + title = computers.dictionary_key_to_proper_section_title(title) # The first entry can be used because all the entries in the section are # already validated with the `validate_a_section` function: diff --git a/rendercv/data/models/design.py b/rendercv/data/models/design.py index 111d2794..b391f590 100644 --- a/rendercv/data/models/design.py +++ b/rendercv/data/models/design.py @@ -1,3 +1,8 @@ +""" +The `rendercv.data.models.design` module contains the data model of the `design` field +of the input file. +""" + import importlib import importlib.util import pathlib @@ -5,10 +10,10 @@ from typing import Annotated, Any, Type import pydantic -from ...themes.classic import ClassicThemeOptions -from ...themes.engineeringresumes import EngineeringresumesThemeOptions -from ...themes.moderncv import ModerncvThemeOptions -from ...themes.sb2nov import Sb2novThemeOptions +from ...renderer.themes.classic import ClassicThemeOptions +from ...renderer.themes.engineeringresumes import EngineeringresumesThemeOptions +from ...renderer.themes.moderncv import ModerncvThemeOptions +from ...renderer.themes.sb2nov import Sb2novThemeOptions from . import entry_types from .base import RenderCVBaseModel diff --git a/rendercv/data/models/entry_types.py b/rendercv/data/models/entry_types.py index 3f2cfa2e..ed9aff36 100644 --- a/rendercv/data/models/entry_types.py +++ b/rendercv/data/models/entry_types.py @@ -1,3 +1,8 @@ +""" +`rendercv.models.data.entry_types` module contains the data models of all the available +entry types in RenderCV. +""" + import functools import re from datetime import date as Date @@ -160,6 +165,7 @@ EndDate = Annotated[ # Create the entry models: ============================================================= # ====================================================================================== + class OneLineEntry(RenderCVBaseModel): """This class is the data model of `OneLineEntry`.""" diff --git a/rendercv/data/models/locale_catalog.py b/rendercv/data/models/locale_catalog.py index ff3f6608..0d43042f 100644 --- a/rendercv/data/models/locale_catalog.py +++ b/rendercv/data/models/locale_catalog.py @@ -1,3 +1,8 @@ +""" +`rendercv.models.locale_catalog` module contains the data model of the `locale_catalog` +field of the input file. +""" + from typing import Annotated, Optional import annotated_types as at diff --git a/rendercv/data/models/rendercv_data_model.py b/rendercv/data/models/rendercv_data_model.py index 1fcdbe94..dcd6b5ba 100644 --- a/rendercv/data/models/rendercv_data_model.py +++ b/rendercv/data/models/rendercv_data_model.py @@ -1,14 +1,13 @@ """ -The `rendercv.data_models.models` module contains all the Pydantic data models used in -RenderCV. These data models define the data format and the usage of computed fields and -the validators. +The `rendercv.data.models.rendercv_data_model` module contains the `RenderCVDataModel` +data model, which is the main data model that defines the whole input file structure. """ from typing import Optional import pydantic -from ...themes.classic import ClassicThemeOptions +from ...renderer.themes.classic import ClassicThemeOptions # Disable Pydantic warnings: # warnings.filterwarnings("ignore") diff --git a/rendercv/data/reader.py b/rendercv/data/reader.py index b574fd42..ce3b073a 100644 --- a/rendercv/data/reader.py +++ b/rendercv/data/reader.py @@ -1,15 +1,12 @@ """ -The `rendercv.data_models.generators` module contains the functions that are used to -generate a sample YAML input file and the JSON schema of RenderCV based on the data -models defined in `rendercv.data_models.models`. +The `rendercv.data.reader` module contains the functions that are used to read the input +file (YAML or JSON) and return them as an instance of `RenderCVDataModel`, which is a +Pydantic data model of RenderCV's data format. """ -import io -import json import pathlib -from typing import Any, Optional +from typing import Any -import pydantic import ruamel.yaml from . import models