Coverage for rendercv/data/models/rendercv_data_model.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-26 00:25 +0000

1""" 

2The `rendercv.data.models.rendercv_data_model` module contains the `RenderCVDataModel` 

3data model, which is the main data model that defines the whole input file structure. 

4""" 

5 

6import pathlib 

7from typing import Optional 

8 

9import pydantic 

10 

11from ...themes import ClassicThemeOptions 

12from .base import RenderCVBaseModelWithoutExtraKeys 

13from .curriculum_vitae import CurriculumVitae 

14from .design import RenderCVDesign 

15from .locale import Locale 

16from .rendercv_settings import RenderCVSettings 

17 

18INPUT_FILE_DIRECTORY: Optional[pathlib.Path] = None 

19 

20 

21class RenderCVDataModel(RenderCVBaseModelWithoutExtraKeys): 

22 """This class binds both the CV and the design information together.""" 

23 

24 # `cv` is normally required, but don't enforce it in JSON Schema to allow 

25 # `design` or `locale` fields to have individual YAML files. 

26 model_config = pydantic.ConfigDict(json_schema_extra={"required": []}) 

27 cv: CurriculumVitae = pydantic.Field( 

28 title="Curriculum Vitae", 

29 description="The data of the CV.", 

30 ) 

31 design: RenderCVDesign = pydantic.Field( 

32 default=ClassicThemeOptions(theme="classic"), 

33 title="Design", 

34 description=( 

35 "The design information of the CV. The default is the classic theme." 

36 ), 

37 ) 

38 locale: Locale = pydantic.Field( 

39 default=None, # type: ignore 

40 title="Locale Catalog", 

41 description=( 

42 "The locale catalog of the CV to allow the support of multiple languages." 

43 ), 

44 ) 

45 rendercv_settings: RenderCVSettings = pydantic.Field( 

46 default=RenderCVSettings(), 

47 title="RenderCV Settings", 

48 description="The settings of the RenderCV.", 

49 ) 

50 

51 @pydantic.model_validator(mode="before") 

52 @classmethod 

53 def update_paths( 

54 cls, model, info: pydantic.ValidationInfo 

55 ) -> Optional[RenderCVSettings]: 

56 """Update the paths in the RenderCV settings.""" 

57 global INPUT_FILE_DIRECTORY # NOQA: PLW0603 

58 

59 context = info.context 

60 if context: 

61 input_file_directory = context.get("input_file_directory", None) 

62 INPUT_FILE_DIRECTORY = input_file_directory 

63 else: 

64 INPUT_FILE_DIRECTORY = None 

65 

66 return model 

67 

68 @pydantic.field_validator("locale", mode="before") 

69 @classmethod 

70 def update_locale(cls, value) -> Locale: 

71 """Update the output folder name in the RenderCV settings.""" 

72 # Somehow, we need this for `test_if_local_catalog_resets` to pass. 

73 if value is None: 

74 return Locale() 

75 

76 return value 

77 

78 

79rendercv_data_model_fields = tuple(RenderCVDataModel.model_fields.keys())