Coverage for biobb_cp2k/cp2k/common.py: 64%

28 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-19 13:23 +0000

1""" Common functions for package biobb_cp2k.cp2k """ 

2from pathlib import Path, PurePath 

3from biobb_common.tools import file_utils as fu 

4 

5 

6# CHECK INPUT PARAMETERS 

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

8 """ Checks input file """ 

9 if optional and not path: 

10 return None 

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

12 fu.log("Path: " + path) 

13 fu.log("Path " + path + " --- " + classname + ': Unexisting %s file, exiting' % argument, out_log) 

14 raise SystemExit(classname + ': Unexisting %s file' % argument) 

15 file_extension = PurePath(path).suffix 

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

17 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log) 

18 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument)) 

19 return path 

20 

21 

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

23 """ Checks output file """ 

24 if optional and not path: 

25 return None 

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

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

28 raise SystemExit(classname + ': Unexisting %s folder' % argument) 

29 file_extension = PurePath(path).suffix 

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

31 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log) 

32 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument)) 

33 return path 

34 

35 

36def is_valid_file(ext, argument): 

37 """ Checks if file format is compatible """ 

38 formats = { 

39 'input_inp_path': ['in', 'inp', 'txt', 'wfn'], 

40 'input_pdb_path': ['pdb'], 

41 'input_rst_path': ['wfn'], 

42 'output_inp_path': ['in', 'inp', 'txt'], 

43 'output_log_path': ['log', 'out', 'txt', 'o'], 

44 'output_outzip_path': ['zip', 'gz', 'gzip'], 

45 'output_rst_path': ['wfn'] 

46 } 

47 return ext in formats[argument]