Coverage for src/pytribeam/command_line.py: 0%

53 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2026-07-23 14:51 +0000

1""" 

2Command Line Entry Points Module 

3================================ 

4 

5This module provides command line entry points for various functions and utilities. 

6It serves as the interface between the command line and the underlying functionality 

7of the application. 

8 

9Important: 

10 This module should remain lightweight at import time. Do not import AutoScript, 

11 Laser API, pytribeam.constants, pytribeam.workflow, or GUI modules at the top 

12 level. Import those only inside functions that actually need them. 

13""" 

14 

15import argparse 

16from pathlib import Path 

17from typing import Final 

18 

19CLI_DOCS: Final[str] = """ 

20-------- 

21pytribeam 

22-------- 

23 

24pytribeam 

25 Prints this command line documentation. 

26 

27pytribeam_info 

28 Prints the module version, supported AutoScript and Laser API versions, 

29 and detected installed environment. 

30 

31pytribeam_gui 

32 Launches the GUI for creating configuration .yml files and controlling 

33 experimental collection. 

34 

35pytribeam_exp <path_to_file>.yml 

36 Runs the 3D data collection workflow based on an input .yml file. 

37 

38pytribeam_exp --help 

39 Prints help for the experiment command. 

40 

41Example: 

42 path/to/experiment/directory> pytribeam_exp path/to/config/file.yml 

43""" 

44 

45 

46def pytribeam(): 

47 """ 

48 Prints the command line documentation to the command window. 

49 

50 This function prints the contents of the global variable `CLI_DOCS` to the 

51 command window. It is assumed that `CLI_DOCS` contains the necessary 

52 documentation in string format. 

53 

54 Parameters 

55 ---------- 

56 None 

57 

58 Returns 

59 ------- 

60 None 

61 """ 

62 print(CLI_DOCS.strip()) 

63 

64 

65def module_info() -> None: 

66 """ 

67 Prints lightweight package and environment information. 

68 

69 This command is intended to verify installation and should not require a 

70 microscope connection, AutoScript runtime initialization, Laser API runtime 

71 initialization, or a license check. 

72 """ 

73 import pytribeam._package_metadata as pm 

74 

75 pytribeam_version = pm.get_pytribeam_version() 

76 pytribeam_commit = pm.get_pytribeam_commit_id() 

77 autoscript_version = pm.get_autoscript_version() 

78 laser_version = pm.get_laser_api_version() 

79 

80 print(f"{pm.MODULE_SHORT_NAME} module version: v{pytribeam_version or 'unknown'}") 

81 

82 if pytribeam_commit: 

83 print(f" Git commit: {pytribeam_commit}") 

84 

85 print(f" Maximum supported .yml schema version: v{pm.YML_SCHEMA_VERSION}") 

86 

87 print( 

88 " Supported Thermo Fisher AutoScript versions: " 

89 + ", ".join(f"v{x}" for x in pm.SUPPORTED_AUTOSCRIPT_VERSIONS) 

90 ) 

91 

92 print( 

93 " Supported Laser API versions: " 

94 + ", ".join(f"v{x}" for x in pm.SUPPORTED_LASER_API_VERSIONS) 

95 ) 

96 

97 print() 

98 print("Installed environment:") 

99 

100 print(" AutoScript:") 

101 print( 

102 " Distribution metadata: " 

103 f"{'detected' if autoscript_version else 'not detected'}, " 

104 f"version: {autoscript_version or 'not detected'}" 

105 ) 

106 print( 

107 " Import package autoscript_sdb_microscope_client: " 

108 f"{'available' if pm.autoscript_available() else 'not importable'}" 

109 ) 

110 

111 print() 

112 print(" Laser API:") 

113 print( 

114 " Distribution metadata: " 

115 f"{'detected' if laser_version else 'not detected'}, " 

116 f"version: {laser_version or 'not detected'}" 

117 ) 

118 print( 

119 " Import package Laser: " 

120 f"{'available' if pm.laser_api_available() else 'not importable'}" 

121 ) 

122 print( 

123 " Import package Laser.PythonControl: " 

124 f"{'available' if pm.laser_pythoncontrol_available() else 'not importable'}" 

125 ) 

126 

127 

128def launch_gui() -> None: 

129 """ 

130 Launches the pytribeam GUI. 

131 

132 GUI imports are intentionally delayed until this function is called. 

133 """ 

134 import pytribeam.GUI.runner as runner 

135 

136 app = runner.MainApplication() 

137 app.mainloop() 

138 

139 

140def build_experiment_parser() -> argparse.ArgumentParser: 

141 """ 

142 Builds the argument parser for the pytribeam_exp command. 

143 """ 

144 parser = argparse.ArgumentParser( 

145 description="Run a pytribeam experiment from a configuration .yml file." 

146 ) 

147 

148 parser.add_argument( 

149 "file_path", 

150 type=str, 

151 help="Path to the experiment configuration .yml file.", 

152 ) 

153 

154 return parser 

155 

156 

157def run_experiment() -> None: 

158 """ 

159 Runs an experiment from the command line. 

160 

161 The workflow import is intentionally delayed until after argument parsing. 

162 This allows `pytribeam_exp --help` to run without importing workflow, 

163 AutoScript, or Laser runtime modules. 

164 """ 

165 

166 def _positive_integer(prompt: str) -> int: 

167 """Helper function to get valid integer input.""" 

168 while True: 

169 try: 

170 value = int(input(prompt)) 

171 if value > 0: 

172 return value 

173 print("Invalid input. Please enter an integer greater than 0.") 

174 except ValueError: 

175 print("Invalid input. Please enter a valid integer.") 

176 

177 parser = build_experiment_parser() 

178 args = parser.parse_args() 

179 

180 start_slice = _positive_integer("Starting slice: ") 

181 start_step = _positive_integer("Starting step: ") 

182 

183 import pytribeam.workflow as workflow 

184 

185 workflow.run_experiment_cli( 

186 start_slice=start_slice, 

187 start_step=start_step, 

188 yml_path=Path(args.file_path), 

189 ) 

190 

191 

192def work_in_progress(): 

193 """ 

194 Prints the 'Work in Progress (WIP)' warning message to the console. 

195 

196 This function prints a warning message indicating that the function is a 

197 work in progress and has not yet been implemented. 

198 

199 Parameters 

200 ---------- 

201 None 

202 

203 Returns 

204 ------- 

205 None 

206 """ 

207 print("Warning: Work in progress (WIP), function not yet implemented.")