Coverage for biobb_amber/cphstats/common.py: 67%
27 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-28 08:28 +0000
1""" Common functions for package biobb_amber.cphstats """
2from pathlib import Path, PurePath
3from biobb_common.tools import file_utils as fu
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(classname + ': Unexisting %s file, exiting' % argument, out_log)
13 raise SystemExit(classname + ': Unexisting %s file' % argument)
14 file_extension = PurePath(path).suffix
15 if not is_valid_file(file_extension[1:], argument):
16 fu.log(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument), out_log)
17 raise SystemExit(classname + ': Format %s in %s file is not compatible' % (file_extension[1:], argument))
18 return path
21# CHECK OUTPUT PARAMETERS
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
36def is_valid_file(ext, argument):
37 """ Checks if file format is compatible """
38 formats = {
39 'input_cpin_path': ['cpin', 'txt', 'in'],
40 'input_cpout_path': ['cpout', 'zip', 'gzip', 'gz'],
41 'input_cein_path': ['cein', 'txt', 'in'],
42 'input_ceout_path': ['ceout', 'zip', 'gzip', 'gz'],
43 'output_dat_path': ['dat', 'out', 'txt', 'o'],
44 'output_population_path': ['dat', 'out', 'txt', 'o'],
45 'output_chunk_path': ['dat', 'out', 'txt', 'o'],
46 'output_cumulative_path': ['dat', 'out', 'txt', 'o'],
47 'output_conditional_path': ['dat', 'out', 'txt', 'o'],
48 'output_chunk_conditional_path': ['dat', 'out', 'txt', 'o'],
49 }
50 return ext in formats[argument]