Coverage for graphqler / utils / file_utils.py: 79%

39 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-20 10:09 -0400

1from pathlib import Path 

2import yaml 

3import json 

4import shutil 

5 

6 

7def initialize_file(file_path: str | Path): 

8 """Initialize file_path with an empty file creating any folders along the way 

9 

10 Args: 

11 file_path (str | Path): The path to the file 

12 """ 

13 if isinstance(file_path, str): 

14 file_path = Path(file_path) 

15 

16 file_path.parent.mkdir(parents=True, exist_ok=True) 

17 open(file_path, "w").close() 

18 

19 

20def intialize_file_if_not_exists(file_path: Path): 

21 """Initialize file_path with an empty file creating any folders along the way if it does not exist 

22 

23 Args: 

24 file_path (Path): The path to the file 

25 """ 

26 if not file_path.exists(): 

27 initialize_file(file_path) 

28 

29 

30def recreate_path(dir_path: Path): 

31 """Recreate a directory 

32 

33 Args: 

34 dir_path (Path): The directory path 

35 """ 

36 # Convert the path to a Path object 

37 path = Path(dir_path) 

38 

39 # Remove the directory if it exists 

40 shutil.rmtree(path, ignore_errors=True) 

41 

42 # Re-create the directory 

43 path.mkdir(parents=True, exist_ok=True) 

44 

45 

46def get_or_create_directory(directory_path: str | Path) -> Path: 

47 """Gets a directory if it exists, otherwise creates it 

48 

49 Args: 

50 directory_path (str | Path): directory path 

51 

52 Returns: 

53 Path: The directory path 

54 """ 

55 if isinstance(directory_path, str): 

56 directory_path = Path(directory_path) 

57 

58 if not directory_path.exists(): 

59 directory_path.mkdir(parents=True, exist_ok=True) 

60 return directory_path 

61 

62 

63def get_or_create_file(file_path: Path) -> Path: 

64 """Gets a file if it exists, otherwise creates it 

65 

66 Args: 

67 file_path (Path): File path 

68 

69 Returns: 

70 Path: The file path 

71 """ 

72 if not file_path.exists(): 

73 initialize_file(file_path) 

74 return file_path 

75 

76 

77def write_json_to_file(contents: dict, output_file: str | Path): 

78 """Write JSON to a file 

79 

80 Args: 

81 contents (dict): Contents of the JSON 

82 output_file (str): Output file path 

83 """ 

84 with open(output_file, "w") as file_handle: 

85 json.dump(contents, file_handle, indent=4) 

86 

87 

88def write_dict_to_yaml(contents: dict, output_file: str | Path): 

89 """Writes dict to YAML file 

90 

91 Args: 

92 contents (dict): Contents of the YAML 

93 output_file (str): Output file path 

94 """ 

95 yaml_data = yaml.dump(contents, default_flow_style=False) 

96 with open(output_file, "w") as yaml_file: 

97 yaml_file.write(yaml_data) 

98 

99 

100def read_yaml_to_dict(read_path: Path) -> dict: 

101 """Reads yaml file to dict 

102 

103 Args: 

104 read_path (Path): Path of the yaml file 

105 

106 Returns: 

107 dict: Dictionary of the YAML file contents 

108 """ 

109 return yaml.safe_load(read_path.read_text()) 

110 

111 

112def get_graphqler_root() -> Path: 

113 """Gets graphqler's root 

114 

115 Returns: 

116 Path: Graphqler root 

117 """ 

118 return Path(__file__).parent.parent 

119 

120 

121def get_project_root() -> Path: 

122 """Gets the project root 

123 

124 Returns: 

125 Path: The project root 

126 """ 

127 return Path(__file__).parent.parent.parent