Coverage for pephubclient/files_manager.py: 61%

56 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-04 18:15 +0000

1import os 

2from contextlib import suppress 

3from pathlib import Path 

4 

5import pandas 

6import toml 

7import yaml 

8import zipfile 

9 

10from pephubclient.constants import CachedToken 

11from pephubclient.exceptions import PEPExistsError 

12 

13 

14class FilesManager: 

15 @staticmethod 

16 def save_token_data(path: str, token: CachedToken) -> None: 

17 """ 

18 Save cached token data (jwt + base url) to provided path as TOML. 

19 """ 

20 Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True) 

21 with open(path, "w") as f: 

22 toml.dump(token.model_dump(), f) 

23 

24 @staticmethod 

25 def load_token_data(path: str) -> CachedToken: 

26 """ 

27 Load cached token data from the TOML file, or return defaults if missing. 

28 """ 

29 with suppress(FileNotFoundError): 

30 with open(path, "r") as f: 

31 return CachedToken(**toml.load(f)) 

32 return CachedToken() 

33 

34 @staticmethod 

35 def create_project_folder( 

36 parent_path: str, 

37 folder_name: str, 

38 ) -> str: 

39 """ 

40 Create new project folder 

41 

42 :param parent_path: parent path to create folder in 

43 :param folder_name: folder name 

44 :return: folder_path 

45 """ 

46 if parent_path: 

47 if not Path(parent_path).exists(): 

48 raise OSError( 

49 f"Parent path does not exist. Provided path: {parent_path}" 

50 ) 

51 folder_path = os.path.join(parent_path or os.getcwd(), folder_name) 

52 Path(folder_path).mkdir(parents=True, exist_ok=True) 

53 return folder_path 

54 

55 @staticmethod 

56 def save_yaml(config: dict, full_path: str, not_force: bool = False): 

57 FilesManager.check_writable(path=full_path, force=not not_force) 

58 with open(full_path, "w") as outfile: 

59 yaml.dump(config, outfile, default_flow_style=False) 

60 

61 @staticmethod 

62 def save_pandas(df: pandas.DataFrame, full_path: str, not_force: bool = False): 

63 FilesManager.check_writable(path=full_path, force=not not_force) 

64 df.to_csv(full_path, index=False) 

65 

66 @staticmethod 

67 def file_exists(full_path: str) -> bool: 

68 return os.path.isfile(full_path) 

69 

70 @staticmethod 

71 def delete_file_if_exists(filename: str) -> None: 

72 with suppress(FileNotFoundError): 

73 os.remove(filename) 

74 print( 

75 f"\033[38;5;11m{f'File was deleted successfully -> {filename}'}\033[0m" 

76 ) 

77 

78 @staticmethod 

79 def check_writable(path: str, force: bool = True): 

80 if not force and os.path.isfile(path): 

81 raise PEPExistsError(f"File already exists and won't be updated: {path}") 

82 

83 @staticmethod 

84 def save_zip_file(files_dict: dict, file_path: str, force: bool = False) -> None: 

85 """ 

86 Save zip file with provided files as dict. 

87 

88 :param files_dict: dict with files to save. e.g. {"file1.txt": "file1 content"} 

89 :param file_path: filename to save zip file to 

90 :param force: overwrite file if exists 

91 :return: None 

92 """ 

93 FilesManager.check_writable(path=file_path, force=force) 

94 with zipfile.ZipFile( 

95 file_path, mode="w", compression=zipfile.ZIP_DEFLATED 

96 ) as zf: 

97 for name, res in files_dict.items(): 

98 zf.writestr(name, str.encode(res))