Coverage for biobb_amber/process/common.py: 59%

37 statements  

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

1"""Common functions for package biobb_amber.process""" 

2 

3from pathlib import Path, PurePath 

4from typing import Optional, Union 

5 

6from biobb_common.tools import file_utils as fu 

7 

8 

9# CHECK INPUT PARAMETERS 

10def check_input_path(path, argument, optional, out_log, classname): 

11 """Checks input file""" 

12 if optional and not path: 

13 return None 

14 if not Path(path).exists(): 

15 fu.log(classname + ": Unexisting %s file, exiting" % argument, out_log) 

16 raise SystemExit(classname + ": Unexisting %s file" % argument) 

17 file_extension = PurePath(path).suffix 

18 if not is_valid_file(file_extension[1:], argument): 

19 fu.log( 

20 classname + ": Format %s in %s file is not compatible" 

21 % (file_extension[1:], argument), 

22 out_log, 

23 ) 

24 raise SystemExit( 

25 classname + ": Format %s in %s file is not compatible" 

26 % (file_extension[1:], argument) 

27 ) 

28 return path 

29 

30 

31# CHECK OUTPUT PARAMETERS 

32def check_output_path(path, argument, optional, out_log, classname): 

33 """Checks output file""" 

34 if optional and not path: 

35 return None 

36 if PurePath(path).parent and not Path(PurePath(path).parent).exists(): 

37 fu.log(classname + ": Unexisting %s folder, exiting" % argument, out_log) 

38 raise SystemExit(classname + ": Unexisting %s folder" % argument) 

39 file_extension = PurePath(path).suffix 

40 if not is_valid_file(file_extension[1:], argument): 

41 fu.log( 

42 classname + ": Format %s in %s file is not compatible" 

43 % (file_extension[1:], argument), 

44 out_log, 

45 ) 

46 raise SystemExit( 

47 classname + ": Format %s in %s file is not compatible" 

48 % (file_extension[1:], argument) 

49 ) 

50 return path 

51 

52 

53def is_valid_file(ext, argument): 

54 """Checks if file format is compatible""" 

55 formats = { 

56 "input_log_path": ["log", "out", "txt", "o"], 

57 "output_dat_path": ["dat", "txt", "csv"], 

58 } 

59 return ext in formats[argument] 

60 

61 

62def _from_string_to_list(input_data: Optional[Union[str, list[str]]]) -> list[str]: 

63 """ 

64 Converts a string to a list, splitting by commas or spaces. If the input is already a list, returns it as is. 

65 Returns an empty list if input_data is None. 

66 

67 Parameters: 

68 input_data (str, list, or None): The string, list, or None value to convert. 

69 

70 Returns: 

71 list: A list of string elements or an empty list if input_data is None. 

72 """ 

73 if input_data is None: 

74 return [] 

75 

76 if isinstance(input_data, list): 

77 # If input is already a list, return it 

78 return input_data 

79 

80 # If input is a string, determine the delimiter based on presence of commas 

81 delimiter = "," if "," in input_data else " " 

82 items = input_data.split(delimiter) 

83 

84 # Remove whitespace from each item and ignore empty strings 

85 processed_items = [item.strip() for item in items if item.strip()] 

86 

87 return processed_items