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
« 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
7def initialize_file(file_path: str | Path):
8 """Initialize file_path with an empty file creating any folders along the way
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)
16 file_path.parent.mkdir(parents=True, exist_ok=True)
17 open(file_path, "w").close()
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
23 Args:
24 file_path (Path): The path to the file
25 """
26 if not file_path.exists():
27 initialize_file(file_path)
30def recreate_path(dir_path: Path):
31 """Recreate a directory
33 Args:
34 dir_path (Path): The directory path
35 """
36 # Convert the path to a Path object
37 path = Path(dir_path)
39 # Remove the directory if it exists
40 shutil.rmtree(path, ignore_errors=True)
42 # Re-create the directory
43 path.mkdir(parents=True, exist_ok=True)
46def get_or_create_directory(directory_path: str | Path) -> Path:
47 """Gets a directory if it exists, otherwise creates it
49 Args:
50 directory_path (str | Path): directory path
52 Returns:
53 Path: The directory path
54 """
55 if isinstance(directory_path, str):
56 directory_path = Path(directory_path)
58 if not directory_path.exists():
59 directory_path.mkdir(parents=True, exist_ok=True)
60 return directory_path
63def get_or_create_file(file_path: Path) -> Path:
64 """Gets a file if it exists, otherwise creates it
66 Args:
67 file_path (Path): File path
69 Returns:
70 Path: The file path
71 """
72 if not file_path.exists():
73 initialize_file(file_path)
74 return file_path
77def write_json_to_file(contents: dict, output_file: str | Path):
78 """Write JSON to a file
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)
88def write_dict_to_yaml(contents: dict, output_file: str | Path):
89 """Writes dict to YAML file
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)
100def read_yaml_to_dict(read_path: Path) -> dict:
101 """Reads yaml file to dict
103 Args:
104 read_path (Path): Path of the yaml file
106 Returns:
107 dict: Dictionary of the YAML file contents
108 """
109 return yaml.safe_load(read_path.read_text())
112def get_graphqler_root() -> Path:
113 """Gets graphqler's root
115 Returns:
116 Path: Graphqler root
117 """
118 return Path(__file__).parent.parent
121def get_project_root() -> Path:
122 """Gets the project root
124 Returns:
125 Path: The project root
126 """
127 return Path(__file__).parent.parent.parent