Coverage for rendercv/data/reader.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 17:51 +0000

1""" 

2The `rendercv.data.reader` module contains the functions that are used to read the input 

3file (YAML or JSON) and return them as an instance of `RenderCVDataModel`, which is a 

4Pydantic data model of RenderCV's data format. 

5""" 

6 

7import pathlib 

8 

9import ruamel.yaml 

10 

11from . import models 

12 

13 

14def read_a_yaml_file(file_path_or_contents: pathlib.Path | str) -> dict: 

15 """Read a YAML file and return its content as a dictionary. The YAML file can be 

16 given as a path to the file or as the contents of the file as a string. 

17 

18 Args: 

19 file_path_or_contents (pathlib.Path | str): The path to the YAML file or the 

20 contents of the YAML file as a string. 

21 

22 Returns: 

23 dict: The content of the YAML file as a dictionary. 

24 """ 

25 if isinstance(file_path_or_contents, pathlib.Path): 

26 # Check if the file exists: 

27 if not file_path_or_contents.exists(): 

28 raise FileNotFoundError( 

29 f"The input file [magenta]{file_path_or_contents}[/magenta] doesn't" 

30 " exist!" 

31 ) 

32 

33 # Check the file extension: 

34 accepted_extensions = [".yaml", ".yml", ".json", ".json5"] 

35 if file_path_or_contents.suffix not in accepted_extensions: 

36 user_friendly_accepted_extensions = [ 

37 f"[green]{ext}[/green]" for ext in accepted_extensions 

38 ] 

39 user_friendly_accepted_extensions = ", ".join( 

40 user_friendly_accepted_extensions 

41 ) 

42 raise ValueError( 

43 "The input file should have one of the following extensions:" 

44 f" {user_friendly_accepted_extensions}. The input file is" 

45 f" [magenta]{file_path_or_contents}[/magenta]." 

46 ) 

47 

48 file_content = file_path_or_contents.read_text(encoding="utf-8") 

49 else: 

50 file_content = file_path_or_contents 

51 

52 yaml_as_a_dictionary: dict = ruamel.yaml.YAML().load(file_content) 

53 

54 return yaml_as_a_dictionary 

55 

56 

57def validate_input_dictionary_and_return_the_data_model( 

58 input_dictionary: dict, 

59) -> models.RenderCVDataModel: 

60 """Validate the input dictionary by creating an instance of `RenderCVDataModel`, 

61 which is a Pydantic data model of RenderCV's data format. 

62 

63 Args: 

64 input_dictionary (dict): The input dictionary. 

65 

66 Returns: 

67 RenderCVDataModel: The data model. 

68 """ 

69 

70 # Validate the parsed dictionary by creating an instance of RenderCVDataModel: 

71 rendercv_data_model = models.RenderCVDataModel(**input_dictionary) 

72 

73 return rendercv_data_model 

74 

75 

76def read_input_file( 

77 file_path_or_contents: pathlib.Path | str, 

78) -> models.RenderCVDataModel: 

79 """Read the input file (YAML or JSON) and return them as an instance of 

80 `RenderCVDataModel`, which is a Pydantic data model of RenderCV's data format. 

81 

82 Args: 

83 file_path_or_contents (str): The path to the input file or the contents of the 

84 input file as a string. 

85 

86 Returns: 

87 RenderCVDataModel: The data model. 

88 """ 

89 input_as_dictionary = read_a_yaml_file(file_path_or_contents) 

90 

91 rendercv_data_model = validate_input_dictionary_and_return_the_data_model( 

92 input_as_dictionary 

93 ) 

94 

95 return rendercv_data_model