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

46 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-22 17:35 +0000

1#!/usr/bin/env python3 

2 

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

4import shutil 

5from pathlib import PurePath 

6from biobb_common.generic.biobb_object import BiobbObject 

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 Template(BiobbObject): 

13 """ 

14 | biobb_template Template 

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

16 | Long description for the `template <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 * **sandbox_path** (*str*) - ("./") [WF property] Parent path to the sandbox directory. 

28 

29 Examples: 

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

31 

32 from biobb_template.template.template import template 

33 

34 prop = { 

35 'boolean_property': True 

36 } 

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

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

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

40 properties=prop) 

41 

42 Info: 

43 * wrapped_software: 

44 * name: Zip 

45 * version: >=3.0 

46 * license: BSD 3-Clause 

47 * ontology: 

48 * name: EDAM 

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

50 

51 """ 

52 

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

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

55 properties = properties or {} 

56 

57 # 2.0 Call parent class constructor 

58 super().__init__(properties) 

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

60 

61 # 2.1 Modify to match constructor parameters 

62 # Input/Output files 

63 self.io_dict = { 

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

65 'out': {'output_file_path': output_file_path} 

66 } 

67 

68 # 3. Include all relevant properties here as 

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

70 

71 # Properties specific for BB 

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

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

74 self.properties = properties 

75 

76 # Check the properties 

77 self.check_properties(properties) 

78 # Check the arguments 

79 self.check_arguments() 

80 

81 @launchlogger 

82 def launch(self) -> int: 

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

84 

85 # 4. Setup Biobb 

86 if self.check_restart(): 

87 return 0 

88 self.stage_files() 

89 

90 # Creating temporary folder 

91 tmp_folder = fu.create_unique_dir() 

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

93 

94 # 5. Include here all mandatory input files 

95 # Copy input_file_path1 to temporary folder 

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

97 

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

99 instructions = ['-j'] 

100 if self.boolean_property: 

101 instructions.append('-v') 

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

103 

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

105 self.cmd = [self.binary_path, 

106 ' '.join(instructions), 

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

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

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

110 

111 # 8. Repeat for optional input files if provided 

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

113 # Copy input_file_path2 to temporary folder 

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

115 # Append optional input_file_path2 to cmd 

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

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

118 

119 # 9. Uncomment to check the command line 

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

121 

122 # Run Biobb block 

123 self.run_biobb() 

124 

125 # Copy files to host 

126 self.copy_to_host() 

127 

128 # Remove temporary file(s) 

129 self.tmp_files.append(tmp_folder) 

130 self.remove_tmp_files() 

131 

132 # Check output arguments 

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

134 

135 return self.return_code 

136 

137 

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

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

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

141 return Template(**dict(locals())).launch() 

142 

143 

144template.__doc__ = Template.__doc__ 

145main = Template.get_main(template, 'Description for the template module.') 

146 

147 

148if __name__ == '__main__': 

149 main() 

150 

151# 12. Complete documentation strings