Coverage for biobb_template/template/template_container.py: 24%

54 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 TemplateContainer class and the command line interface.""" 

4import argparse 

5from biobb_common.generic.biobb_object import BiobbObject 

6from biobb_common.configuration import settings 

7from biobb_common.tools import file_utils as fu 

8from biobb_common.tools.file_utils import launchlogger 

9 

10 

11# 1. Rename class as required 

12class TemplateContainer(BiobbObject): 

13 """ 

14 | biobb_template TemplateContainer 

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

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

17 

18 Args: 

19 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). 

20 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). 

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

22 properties (dic): 

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

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

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

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

27 * **container_path** (*str*) - (None) Container path definition. 

28 * **container_image** (*str*) - ('mmbirb/zip:latest') Container image definition. 

29 * **container_volume_path** (*str*) - ('/tmp') Container volume path definition. 

30 * **container_working_dir** (*str*) - (None) Container working directory definition. 

31 * **container_user_id** (*str*) - (None) Container user_id definition. 

32 * **container_shell_path** (*str*) - ('/bin/bash') Path to default shell inside the container. 

33 

34 Examples: 

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

36 

37 from biobb_template.template.template_container import template_container 

38 

39 prop = { 

40 'boolean_property': True, 

41 'container_path': 'docker', 

42 'container_image': 'mmbirb/zip:latest', 

43 'container_volume_path': '/tmp' 

44 } 

45 template_container(input_file_path1='/path/to/myTopology.top', 

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

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

48 properties=prop) 

49 

50 Info: 

51 * wrapped_software: 

52 * name: Zip 

53 * version: >=3.0 

54 * license: BSD 3-Clause 

55 * ontology: 

56 * name: EDAM 

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

58 

59 """ 

60 

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

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

63 properties = properties or {} 

64 

65 # 2.0 Call parent class constructor 

66 super().__init__(properties) 

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

68 

69 # 2.1 Modify to match constructor parameters 

70 # Input/Output files 

71 self.io_dict = { 

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

73 'out': {'output_file_path': output_file_path} 

74 } 

75 

76 # 3. Include all relevant properties here as 

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

78 

79 # Properties specific for BB 

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

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

82 self.properties = properties 

83 

84 # Check the properties 

85 self.check_properties(properties) 

86 # Check the arguments 

87 self.check_arguments() 

88 

89 @launchlogger 

90 def launch(self) -> int: 

91 """Execute the :class:`TemplateContainer <template.template_container.TemplateContainer>` object.""" 

92 

93 # 4. Setup Biobb 

94 if self.check_restart(): 

95 return 0 

96 self.stage_files() 

97 

98 # Creating temporary folder 

99 self.tmp_folder = fu.create_unique_dir() 

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

101 

102 # 5. Prepare the command line parameters as instructions list 

103 instructions = ['-j'] 

104 if self.boolean_property: 

105 instructions.append('-v') 

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

107 

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

109 self.cmd = [self.binary_path, 

110 ' '.join(instructions), 

111 self.stage_io_dict['out']['output_file_path'], 

112 self.stage_io_dict['in']['input_file_path1']] 

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

114 

115 # 7. Repeat for optional input files if provided 

116 if self.stage_io_dict['in']['input_file_path2']: 

117 # Append optional input_file_path2 to cmd 

118 self.cmd.append(self.stage_io_dict['in']['input_file_path2']) 

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

120 

121 # 8. Uncomment to check the command line 

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

123 

124 # Run Biobb block 

125 self.run_biobb() 

126 

127 # Copy files to host 

128 self.copy_to_host() 

129 

130 # Remove temporary file(s) 

131 self.tmp_files.extend([ 

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

133 self.tmp_folder 

134 ]) 

135 self.remove_tmp_files() 

136 

137 # Check output arguments 

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

139 

140 return self.return_code 

141 

142 

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

144 """Create :class:`TemplateContainer <template.template_container.TemplateContainer>` class and 

145 execute the :meth:`launch() <template.template_container.TemplateContainer.launch>` method.""" 

146 

147 return TemplateContainer(input_file_path1=input_file_path1, 

148 output_file_path=output_file_path, 

149 input_file_path2=input_file_path2, 

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

151 

152 

153def main(): 

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

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

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

157 

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

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

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

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

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

163 

164 args = parser.parse_args() 

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

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

167 

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

169 # Specific call of each building block 

170 template_container(input_file_path1=args.input_file_path1, 

171 output_file_path=args.output_file_path, 

172 input_file_path2=args.input_file_path2, 

173 properties=properties) 

174 

175 

176if __name__ == '__main__': 

177 main() 

178 

179# 13. Complete documentation strings