eric6/DebugClients/Python/QProcessExtension.py

branch
multi_processing
changeset 7410
401791e6f50f
parent 7409
1413bfe73d41
child 7411
6d8dcb3551b3
equal deleted inserted replaced
7409:1413bfe73d41 7410:401791e6f50f
9 """ 9 """
10 10
11 import os 11 import os
12 12
13 _debugClient = None 13 _debugClient = None
14
15
16 # TODO: extend this with first line logic
17 def _isPythonProgram(program, arguments):
18 """
19 Protected function to check, if program is a Python interpreter and
20 arguments don't include '-m'.
21
22 @param program program to be executed
23 @type str
24 @param arguments list of command line arguments
25 @type list of str
26 @return flag indicating a python program and a tuple containing the
27 interpreter to be used and the arguments
28 @rtype tuple of (bool, tuple of (str, list of str))
29 """
30 prog = program.lower()
31 ok = "python" in prog and arguments[0] != '-m'
32 return ok, (program, arguments[:])
14 33
15 34
16 def patchQProcess(module, debugClient): 35 def patchQProcess(module, debugClient):
17 """ 36 """
18 Function to patch the QtCore module's QProcess. 37 Function to patch the QtCore module's QProcess.
26 45
27 class QProcessWrapper(module.QProcess): 46 class QProcessWrapper(module.QProcess):
28 """ 47 """
29 Wrapper class for *.QProcess. 48 Wrapper class for *.QProcess.
30 """ 49 """
50 _origQProcessStartDetached = module.QProcess.startDetached
51
31 def __init__(self, parent=None): 52 def __init__(self, parent=None):
32 """ 53 """
33 Constructor 54 Constructor
34 """ 55 """
35 super(QProcessWrapper, self).__init__(parent) 56 super(QProcessWrapper, self).__init__(parent)
36 57
37 def __modifyArgs(self, arguments, multiprocessSupport): 58 @classmethod
59 def modifyArgs(cls, arguments, multiprocessSupport):
38 """ 60 """
39 Private method to modify the arguments given to the start method. 61 Private method to modify the arguments given to the start method.
40 62
41 @param arguments list of program arguments 63 @param arguments list of program arguments
42 @type list of str 64 @type list of str
69 # end the arguments for DebugClient 91 # end the arguments for DebugClient
70 modifiedArguments.extend(arguments) 92 modifiedArguments.extend(arguments)
71 93
72 return modifiedArguments 94 return modifiedArguments
73 95
96 ###################################################################
97 ## Handling of 'start(...)' below
98 ###################################################################
99
74 def start(self, *args, **kwargs): 100 def start(self, *args, **kwargs):
75 """ 101 """
76 Public method to start the process. 102 Public method to start the process.
77 103
78 This method patches the arguments such, that a debug client is 104 This method patches the arguments such, that a debug client is
89 _debugClient.multiprocessSupport and 115 _debugClient.multiprocessSupport and
90 ((len(args) >= 2 and isinstance(args[1], list)) or 116 ((len(args) >= 2 and isinstance(args[1], list)) or
91 (len(args) == 1 and not isinstance(args[0], str)) or 117 (len(args) == 1 and not isinstance(args[0], str)) or
92 len(args) == 0) 118 len(args) == 0)
93 ): 119 ):
94 # TODO: implement this
95 if len(args) >= 2: 120 if len(args) >= 2:
96 program = args[0] 121 program = args[0]
97 arguments = args[1] 122 arguments = args[1]
98 if len(args) > 2: 123 if len(args) > 2:
99 mode = args[2] 124 mode = args[2]
104 arguments = self.arguments() 129 arguments = self.arguments()
105 if len(args) == 1: 130 if len(args) == 1:
106 mode = args[0] 131 mode = args[0]
107 else: 132 else:
108 mode = module.QIODevice.ReadWrite 133 mode = module.QIODevice.ReadWrite
109 if "python" in program.lower() and arguments[0] != "-m": 134 ok, (program, arguments) = _isPythonProgram(program, arguments)
110 # assume a Python script is to be started 135 if ok:
111 # '-m' option to python is not (yet) supported 136 newArgs = self.modifyArgs(
112 newArgs = self.__modifyArgs(
113 arguments, _debugClient.multiprocessSupport) 137 arguments, _debugClient.multiprocessSupport)
114 super(QProcessWrapper, self).start(program, newArgs, mode) 138 super(QProcessWrapper, self).start(program, newArgs, mode)
115 else: 139 else:
116 super(QProcessWrapper, self).start(*args, **kwargs) 140 super(QProcessWrapper, self).start(*args, **kwargs)
117 else: 141 else:
118 super(QProcessWrapper, self).start(*args, **kwargs) 142 super(QProcessWrapper, self).start(*args, **kwargs)
143
144 ###################################################################
145 ## Handling of 'startDetached(...)' below
146 ###################################################################
147
148 def startDetached(self, *args, **kwargs):
149 """
150 Public method to start the detached process.
151
152 This method patches the arguments such, that a debug client is
153 started for the Python script. A Python script is assumed, if the
154 program to be started contains the string 'python'.
155
156 @param args arguments of the start call
157 @type list
158 @param kwargs keyword arguments of the start call
159 @type dict
160 @return flag indicating a successful start
161 @rtype bool
162 """
163 if isinstance(self, str):
164 return QProcessWrapper.startDetachedStatic(
165 self, *args)
166 else:
167 return self.__startDetached(*args, **kwargs)
168
169 def __startDetached(self, *args, **kwargs):
170 """
171 Private method to start the detached process.
172
173 This method patches the arguments such, that a debug client is
174 started for the Python script. A Python script is assumed, if the
175 program to be started contains the string 'python'.
176
177 @param args arguments of the start call
178 @type list
179 @param kwargs keyword arguments of the start call
180 @type dict
181 @return flag indicating a successful start
182 @rtype bool
183 """
184 if (
185 _debugClient.debugging and
186 _debugClient.multiprocessSupport and
187 len(args) == 0
188 ):
189 program = self.program()
190 arguments = self.arguments()
191 wd = self.workingDirectory()
192
193 ok, (program, arguments) = _isPythonProgram(program, arguments)
194 if ok:
195 return QProcessWrapper.startDetachedStatic(
196 program, arguments, wd)
197 else:
198 return super(QProcessWrapper, self).startDetached(
199 *args, **kwargs)
200 else:
201 return super(QProcessWrapper, self).startDetached(
202 *args, **kwargs)
203
204 @staticmethod
205 def startDetachedStatic(*args, **kwargs):
206 """
207 Static method to start the detached process.
208
209 This method patches the arguments such, that a debug client is
210 started for the Python script. A Python script is assumed, if the
211 program to be started contains the string 'python'.
212
213 @param args arguments of the start call
214 @type list
215 @param kwargs keyword arguments of the start call
216 @type dict
217 @return flag indicating a successful start
218 @rtype bool
219 """
220 if (
221 _debugClient.debugging and
222 _debugClient.multiprocessSupport and
223 (len(args) >= 2 and isinstance(args[1], list))
224 ):
225 program = args[0]
226 arguments = args[1]
227 if len(args) >= 3:
228 wd = args[2]
229 else:
230 wd = ""
231 ok, (program, arguments) = _isPythonProgram(program, arguments)
232 if ok:
233 newArgs = QProcessWrapper.modifyArgs(
234 arguments, _debugClient.multiprocessSupport)
235 return QProcessWrapper._origQProcessStartDetached(
236 program, newArgs, wd)
237 else:
238 return QProcessWrapper._origQProcessStartDetached(
239 *args, **kwargs)
240 else:
241 return QProcessWrapper._origQProcessStartDetached(
242 *args, **kwargs)
119 243
120 _debugClient = debugClient 244 _debugClient = debugClient
121 module.QProcess = QProcessWrapper 245 module.QProcess = QProcessWrapper

eric ide

mercurial