CASP Python Libraries
This documentation primarily focuses on the usage of functions from 'casp', 'casp_be', 'casp_gpio' and 'casp_udio' namespaces.
Loading...
Searching...
No Matches
_casp_process.py
1"""! This module defines classes that provide interfaces to perform low level read/write operations with CASP executables. Each class defined in this module creates a subprocess for a particular executable and provides interfaces to talk to that subprocess based on funtionality and need.
2In most cases interfaces defined in this module are not required to be used directly by the user. It is recommended to use the wrapper functions provided in \ref casp.py and \ref casp_gpio.py modules instead.
3Typical interfaces that this module provides:
4 - Interface to create an executable as a subprocess
5 - Interface to write data to the subprocess and read the returned response from the subprocess
6 - Interface to terminate the subprocess
7
8This module also provides global objects for all classes defined in this module. These global objects are used directly by upper level interfaces to provide required functionalities.
9"""
10
11import subprocess
12import sys
13import os
14import time
15import casp_paths
16
17if sys.version_info[:3] < (3,9):
18 from typing import List as list
19
21 """! CaspProcess class provides interfaces to create, read/write and terminate 'CASP' executable as a subprocess.
22 A global object of this class \ref casp_proc is also created in this module. It is used by upper level interfaces from \ref casp.py module.
23 """
24
25 def init(self, make_visible: bool=False, casp_file: str="CASP-Eval"):
26 """! Starts a python subprocess for 'CASP or CASP-Eval' executable with command line interface (CLI) enabled.
27
28 Arguments:
29
30 make_visible: allows the GUI window to be visible or hidden. Default is to hide the window.
31
32 casp_file: allows user to specify different version of CASP executable other than the evaluation version. In most cases this argument is not required to be specified by the user.
33 """
34 casp_fpath = os.path.join(casp_paths.casp_exe_path, casp_file)
35 #form arguments
36 arguments: list[str]
37 if make_visible == True:
38 arguments = ["#cli"]
39 else:
40 arguments = ["#cli", "#hide"]
41 #form command
42 command = [casp_fpath] + arguments
43 #start process
44 self.__process = subprocess.Popen(
45 command,
46 stdin=subprocess.PIPE,
47 stdout=subprocess.PIPE,
48 stderr=subprocess.PIPE,
49 text=True, # Handle as strings instead of bytes
50 bufsize=0 # Unbuffered
51 )
52 #wait till casp initializes
53 print(f"Initializing CASP...")
54 response = self.__process.stdout.readline()
55 print(f"CASP: {response.strip()}")
56
57 def write(self, cmd: str, arguments: list[str]=None) -> str:
58 """! In general, data exchange betweem the python side interfaces and the executable is in the form of 'command' and a list of corresponding 'arguments'.
59 This interface writes the command along with its argument list to the subprocess. Waits for the response and returns the response received from the subprocess.
60
61 Arguments:
62
63 cmd: command string to write to the subprocess
64
65 arguments: argument list to write to the subprocess
66
67 Return Value:
68
69 Response string received from the subprocess
70 """
71 arg_len = 0
72 if arguments:
73 arg_len = len(arguments)
74 arg = cmd
75 if arg_len > 1:
76 arg = arg + '$' + '$'.join(arguments)
77 elif arg_len > 0:
78 arg = arg + '$' + arguments[0]
79 self.__process.stdin.write(arg + '\n') #'\n' is must at the end
80 self.__process.stdin.flush()
81 response = self.__process.stdout.readline() #response should also end with '\n'
82 #print(response.strip())
83 return response.strip()
84
85 def close(self):
86 """! Closes or terminates the subprocess and waits till subprocess is terminated before returning."""
87 print(f"Terminating CASP process...")
88 self.__process.stdin.close()
89 time.sleep(2); #give some time for the msgs to log.
90 self.__process.terminate()
91 self.__process.wait()
92 print(f"...Done")
93
95 """! CaspBlockEditorProcess class provides interfaces to create, read/write and terminate 'BlockEditor' executable as a subprocess.
96 A global object of this class \ref casp_be_proc is also created in this module. It is used by upper level interfaces from \ref casp_be.py module.
97 """
98
99 def init(self, casp_file: str="BlockEditor"):
100 """! Starts a python subprocess for 'BlockEditor' executable with command line interface (CLI) enabled.
101
102 Arguments:
103
104 casp_file: allows user to specify different version of 'BlockEditor' executable when required. In most cases this argument is not required to be specified by the user.
105 """
106 casp_fpath = os.path.join(casp_paths.casp_exe_path, casp_file)
107 command = [casp_fpath, "#cli_on"]
108 #start process
109 self.__process = subprocess.Popen(
110 command,
111 stdin=subprocess.PIPE,
112 stdout=subprocess.PIPE,
113 stderr=subprocess.PIPE,
114 text=True, # Handle as strings instead of bytes
115 bufsize=0 # Unbuffered
116 )
117 #wait till casp initializes
118 print(f"Initializing CASP Block Editor...")
119 response = self.__process.stdout.readline()
120 print(f"CASP: {response.strip()}")
121
122 def write(self, cmd: str, arguments: list[str]=None) -> str:
123 """! In general, data exchange betweem the python side interfaces and the executable is in the form of 'command' and a list of corresponding 'arguments'.
124 This interface writes the command along with its argument list to the subprocess. Waits for the response and returns the response received from the subprocess.
125
126 Arguments:
127
128 cmd: command string to write to the subprocess
129
130 arguments: argument list to write to the subprocess
131
132 Return Value:
133
134 Response string received from the subprocess
135 """
136 arg_len = 0
137 if arguments:
138 arg_len = len(arguments)
139 arg = cmd
140 if arg_len > 1:
141 arg = arg + '$' + '$'.join(arguments)
142 elif arg_len > 0:
143 arg = arg + '$' + arguments[0]
144 self.__process.stdin.write(arg + '\n') #'\n' is must at the end
145 self.__process.stdin.flush()
146 response = self.__process.stdout.readline() #response should also end with '\n'
147 #print(response.strip())
148 return response.strip()
149
150 def close(self):
151 """! Closes or terminates the subprocess and waits till subprocess is terminated before returning."""
152 print(f"Terminating CASP Block Editor process...")
153 self.__process.stdin.close()
154 time.sleep(2); #give some time for the msgs to log.
155 self.__process.terminate()
156 self.__process.wait()
157 print(f"...Done")
158
160 """! CaspBlockViewerProcess class provides interfaces to start and terminate 'BlockViewer' executable as a subprocess. The 'BlockViewer' executable is used to browse CASP blocks.
161 A global object of this class \ref casp_bv_proc is also created in this module. It is used by upper level interfaces from \ref casp.py module to invoke the block viewer executable.
162 """
163
164 def init(self, casp_file: str="BlockViewer"):
165 """! Starts a subprocess for 'BlockViewer' executable with command line interface (CLI) enabled.
166
167 Arguments:
168
169 casp_file: allows user to specify different version of 'BlockViewer' executable when required. In most cases this argument is not required to be specified by the user.
170 """
171 casp_fpath = os.path.join(casp_paths.casp_exe_path, casp_file)
172 #start process
173 self.__process = subprocess.Popen(
174 casp_fpath,
175 stdin=subprocess.PIPE,
176 stdout=subprocess.PIPE,
177 stderr=subprocess.PIPE,
178 text=True, # Handle as strings instead of bytes
179 bufsize=0 # Unbuffered
180 )
181
182 def close(self):
183 """! Closes or terminates the subprocess and waits till subprocess is terminated before returning."""
184 self.__process.stdin.close()
185 self.__process.terminate()
186 self.__process.wait()
187
189 """! CaspGpioProcess class provides interfaces to create, read/write and terminate 'casp_gpio_bridge' executable as a subprocess.
190 A global object of this class \ref casp_gpio_proc is also created in this module. It is used by upper level interfaces from \ref casp_gpio.py module.
191 """
192
193 def init(self, casp_file: str="casp_gpio_bridge"):
194 """! Starts a subprocess for 'casp_gpio_bridge' executable with command line interface (CLI) enabled.
195 Arguments:
196
197 casp_file: allows user to specify different version of the executable when required. In most cases this argument is not required to be specified by the user.
198 """
199 casp_fpath = os.path.join(casp_paths.casp_exe_path, casp_file)
200 command = [casp_fpath, "#cli"]
201 #start process
202 self.__process = subprocess.Popen(
203 command,
204 stdin=subprocess.PIPE,
205 stdout=subprocess.PIPE,
206 stderr=subprocess.PIPE,
207 text=True, # Handle as strings instead of bytes
208 bufsize=0 # Unbuffered
209 )
210 #wait till casp initializes
211 print(f"Initializing...")
212 response = self.__process.stdout.readline()
213 print(f"CASP: {response.strip()}")
214
215 def write(self, cmd: str, arguments: list[str]=None) -> str:
216 """! Data exchange betweem the python side interfaces and the executable is in the form of 'command' and a list of corresponding 'arguments'.
217 This interface writes the command along with its argument list to the subprocess.
218 Waits for the response and returns the response received from the subprocess.
219
220 Arguments:
221
222 cmd: command string to write to the subprocess
223
224 arguments: argument list to write to the subprocess
225
226 Return Value:
227
228 Response string received from the subprocess
229 """
230 arg_len = 0
231 if arguments:
232 arg_len = len(arguments)
233 arg = cmd
234 if arg_len > 1:
235 arg = arg + '$' + '$'.join(arguments)
236 elif arg_len > 0:
237 arg = arg + '$' + arguments[0]
238 self.__process.stdin.write(arg + '\n') #'\n' is must at the end
239 self.__process.stdin.flush()
240 response = self.__process.stdout.readline() #response should also end with '\n'
241 #print(response.strip())
242 return response.strip()
243
244 def close(self):
245 """! Closes or terminates the subprocess and waits till subprocess is terminated before returning."""
246 print(f"Terminating process...")
247 self.__process.stdin.close()
248 time.sleep(2); #give some time for the msgs to log.
249 self.__process.terminate()
250 self.__process.wait()
251 print(f"...Done")
252
253
254casp_proc = CaspProcess()
255
256
258
259
261
262
263casp_gpio_proc = CaspGpioProcess()
264
CaspBlockEditorProcess class provides interfaces to create, read/write and terminate 'BlockEditor' ex...
init(self, str casp_file="BlockEditor")
Starts a python subprocess for 'BlockEditor' executable with command line interface (CLI) enabled.
str write(self, str cmd, list[str] arguments=None)
In general, data exchange betweem the python side interfaces and the executable is in the form of 'co...
close(self)
Closes or terminates the subprocess and waits till subprocess is terminated before returning.
CaspBlockViewerProcess class provides interfaces to start and terminate 'BlockViewer' executable as a...
init(self, str casp_file="BlockViewer")
Starts a subprocess for 'BlockViewer' executable with command line interface (CLI) enabled.
close(self)
Closes or terminates the subprocess and waits till subprocess is terminated before returning.
CaspGpioProcess class provides interfaces to create, read/write and terminate 'casp_gpio_bridge' exec...
init(self, str casp_file="casp_gpio_bridge")
Starts a subprocess for 'casp_gpio_bridge' executable with command line interface (CLI) enabled.
close(self)
Closes or terminates the subprocess and waits till subprocess is terminated before returning.
str write(self, str cmd, list[str] arguments=None)
Data exchange betweem the python side interfaces and the executable is in the form of 'command' and a...
CaspProcess class provides interfaces to create, read/write and terminate 'CASP' executable as a subp...
str write(self, str cmd, list[str] arguments=None)
In general, data exchange betweem the python side interfaces and the executable is in the form of 'co...
close(self)
Closes or terminates the subprocess and waits till subprocess is terminated before returning.
init(self, bool make_visible=False, str casp_file="CASP-Eval")
Starts a python subprocess for 'CASP or CASP-Eval' executable with command line interface (CLI) enabl...