Coverage for biobb_template/template/template.py: 76%

58 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-30 16:51 +0000

1#!/usr/bin/env python3 

2 

3"""Module containing the Template class and the command line interface.""" 

4import argparse 

5import shutil 

6from pathlib import PurePath 

7from biobb_common.generic.biobb_object import BiobbObject 

8from biobb_common.configuration import settings 

9from biobb_common.tools import file_utils as fu 

10from biobb_common.tools.file_utils import launchlogger 

11 

12 

13# 1. Rename class as required 

14class Template(BiobbObject): 

15 """ 

16 | biobb_template Template 

17 | Short description for the `template <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Mandatory. 

18 | Long description for the `template <http://templatedocumentation.org>`_ module in Restructured Text (reST) syntax. Optional. 

19 

20 Args: 

21 input_file_path1 (str): Description for the first input file path. File type: input. `Sample file <https://urlto.sample>`_. Accepted formats: top (edam:format_3881). 

22 input_file_path2 (str) (Optional): Description for the second input file path (optional). File type: input. `Sample file <https://urlto.sample>`_. Accepted formats: dcd (edam:format_3878). 

23 output_file_path (str): Description for the output file path. File type: output. `Sample file <https://urlto.sample>`_. Accepted formats: zip (edam:format_3987). 

24 properties (dic): 

25 * **boolean_property** (*bool*) - (True) Example of boolean property. 

26 * **binary_path** (*str*) - ("zip") Example of executable binary property. 

27 * **remove_tmp** (*bool*) - (True) [WF property] Remove temporal files. 

28 * **restart** (*bool*) - (False) [WF property] Do not execute if output files exist. 

29 

30 Examples: 

31 This is a use example of how to use the building block from Python:: 

32 

33 from biobb_template.template.template import template 

34 

35 prop = { 

36 'boolean_property': True 

37 } 

38 template(input_file_path1='/path/to/myTopology.top', 

39 output_file_path='/path/to/newCompressedFile.zip', 

40 input_file_path2='/path/to/mytrajectory.dcd', 

41 properties=prop) 

42 

43 Info: 

44 * wrapped_software: 

45 * name: Zip 

46 * version: >=3.0 

47 * license: BSD 3-Clause 

48 * ontology: 

49 * name: EDAM 

50 * schema: http://edamontology.org/EDAM.owl 

51 

52 """ 

53 

54 # 2. Adapt input and output file paths as required. Include all files, even optional ones 

55 def __init__(self, input_file_path1, output_file_path, input_file_path2=None, properties=None, **kwargs) -> None: 

56 properties = properties or {} 

57 

58 # 2.0 Call parent class constructor 

59 super().__init__(properties) 

60 self.locals_var_dict = locals().copy() 

61 

62 # 2.1 Modify to match constructor parameters 

63 # Input/Output files 

64 self.io_dict = { 

65 'in': {'input_file_path1': input_file_path1, 'input_file_path2': input_file_path2}, 

66 'out': {'output_file_path': output_file_path} 

67 } 

68 

69 # 3. Include all relevant properties here as 

70 # self.property_name = properties.get('property_name', property_default_value) 

71 

72 # Properties specific for BB 

73 self.boolean_property = properties.get('boolean_property', True) 

74 self.binary_path = properties.get('binary_path', 'zip') 

75 self.properties = properties 

76 

77 # Check the properties 

78 self.check_properties(properties) 

79 # Check the arguments 

80 self.check_arguments() 

81 

82 @launchlogger 

83 def launch(self) -> int: 

84 """Execute the :class:`Template <template.template.Template>` object.""" 

85 

86 # 4. Setup Biobb 

87 if self.check_restart(): 

88 return 0 

89 self.stage_files() 

90 

91 # Creating temporary folder 

92 self.tmp_folder = fu.create_unique_dir() 

93 fu.log('Creating %s temporary folder' % self.tmp_folder, self.out_log) 

94 

95 # 5. Include here all mandatory input files 

96 # Copy input_file_path1 to temporary folder 

97 shutil.copy(self.io_dict['in']['input_file_path1'], self.tmp_folder) 

98 

99 # 6. Prepare the command line parameters as instructions list 

100 instructions = ['-j'] 

101 if self.boolean_property: 

102 instructions.append('-v') 

103 fu.log('Appending optional boolean property', self.out_log, self.global_log) 

104 

105 # 7. Build the actual command line as a list of items (elements order will be maintained) 

106 self.cmd = [self.binary_path, 

107 ' '.join(instructions), 

108 self.io_dict['out']['output_file_path'], 

109 str(PurePath(self.tmp_folder).joinpath(PurePath(self.io_dict['in']['input_file_path1']).name))] 

110 fu.log('Creating command line with instructions and required arguments', self.out_log, self.global_log) 

111 

112 # 8. Repeat for optional input files if provided 

113 if self.io_dict['in']['input_file_path2']: 

114 # Copy input_file_path2 to temporary folder 

115 shutil.copy(self.io_dict['in']['input_file_path2'], self.tmp_folder) 

116 # Append optional input_file_path2 to cmd 

117 self.cmd.append(str(PurePath(self.tmp_folder).joinpath(PurePath(self.io_dict['in']['input_file_path2']).name))) 

118 fu.log('Appending optional argument to command line', self.out_log, self.global_log) 

119 

120 # 9. Uncomment to check the command line 

121 # print(' '.join(cmd)) 

122 

123 # Run Biobb block 

124 self.run_biobb() 

125 

126 # Copy files to host 

127 self.copy_to_host() 

128 

129 # Remove temporary file(s) 

130 self.tmp_files.extend([ 

131 self.stage_io_dict.get("unique_dir"), 

132 self.tmp_folder 

133 ]) 

134 self.remove_tmp_files() 

135 

136 # Check output arguments 

137 self.check_arguments(output_files_created=True, raise_exception=False) 

138 

139 return self.return_code 

140 

141 

142def template(input_file_path1: str, output_file_path: str, input_file_path2: str = None, properties: dict = None, **kwargs) -> int: 

143 """Create :class:`Template <template.template.Template>` class and 

144 execute the :meth:`launch() <template.template.Template.launch>` method.""" 

145 

146 return Template(input_file_path1=input_file_path1, 

147 output_file_path=output_file_path, 

148 input_file_path2=input_file_path2, 

149 properties=properties, **kwargs).launch() 

150 

151 

152def main(): 

153 """Command line execution of this building block. Please check the command line documentation.""" 

154 parser = argparse.ArgumentParser(description='Description for the template module.', formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=99999)) 

155 parser.add_argument('--config', required=False, help='Configuration file') 

156 

157 # 10. Include specific args of each building block following the examples. They should match step 2 

158 required_args = parser.add_argument_group('required arguments') 

159 required_args.add_argument('--input_file_path1', required=True, help='Description for the first input file path. Accepted formats: top.') 

160 parser.add_argument('--input_file_path2', required=False, help='Description for the second input file path (optional). Accepted formats: dcd.') 

161 required_args.add_argument('--output_file_path', required=True, help='Description for the output file path. Accepted formats: zip.') 

162 

163 args = parser.parse_args() 

164 args.config = args.config or "{}" 

165 properties = settings.ConfReader(config=args.config).get_prop_dic() 

166 

167 # 11. Adapt to match Class constructor (step 2) 

168 # Specific call of each building block 

169 template(input_file_path1=args.input_file_path1, 

170 output_file_path=args.output_file_path, 

171 input_file_path2=args.input_file_path2, 

172 properties=properties) 

173 

174 

175if __name__ == '__main__': 

176 main() 

177 

178# 12. Complete documentation strings