|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a function to patch QProcess to support debugging of the |
|
8 process. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 _startOptions = [] |
|
14 |
|
15 |
|
16 def patchQProcess(module, startOptions): |
|
17 """ |
|
18 Function to patch the QtCore module's QProcess. |
|
19 |
|
20 @param module reference to the imported module to be patched |
|
21 @type module |
|
22 @param startOptions reference to the saved start options |
|
23 @type tuple |
|
24 """ # __IGNORE_WARNING_D234__ |
|
25 global _startOptions |
|
26 |
|
27 class QProcessWrapper(module.QProcess): |
|
28 """ |
|
29 Wrapper class for *.QProcess. |
|
30 """ |
|
31 def __init__(self, parent=None): |
|
32 """ |
|
33 Constructor |
|
34 """ |
|
35 super(QProcessWrapper, self).__init__(parent) |
|
36 |
|
37 def __modifyArgs(self, arguments): |
|
38 """ |
|
39 Private method to modify the arguments given to the start method. |
|
40 |
|
41 @param arguments list of program arguments |
|
42 @type list of str |
|
43 @return modified argument list |
|
44 @rtype list of str |
|
45 """ |
|
46 (wd, host, port, exceptions, tracePython, redirect, |
|
47 noencoding) = _startOptions[:7] |
|
48 modifiedArguments = [ |
|
49 os.path.join(os.path.dirname(__file__), "DebugClient.py"), |
|
50 "-h", host, |
|
51 "-p", str(port), |
|
52 "--no-passive" |
|
53 ] |
|
54 |
|
55 if wd: |
|
56 modifiedArguments.extend(["-w", wd]) |
|
57 if not exceptions: |
|
58 modifiedArguments.append("-e") |
|
59 if tracePython: |
|
60 modifiedArguments.append("-t") |
|
61 if not redirect: |
|
62 modifiedArguments.append("-n") |
|
63 if noencoding: |
|
64 modifiedArguments.append("--no-encoding") |
|
65 modifiedArguments.append("--") |
|
66 # end the arguments for DebugClient |
|
67 modifiedArguments.extend(arguments) |
|
68 |
|
69 return modifiedArguments |
|
70 |
|
71 def start(self, *args, **kwargs): |
|
72 """ |
|
73 Public method to start the process. |
|
74 |
|
75 This method patches the arguments such, that a debug client is |
|
76 started for the Python script. A Python script is assumed, if the |
|
77 program to be started contains the string 'python'. |
|
78 |
|
79 @param args arguments of the start call |
|
80 @type list |
|
81 @param kwargs keyword arguments of the start call |
|
82 @type dict |
|
83 """ |
|
84 if ( |
|
85 (len(args) >= 2 and isinstance(args[1], list)) or |
|
86 (len(args) == 1 and not isinstance(args[0], str)) or |
|
87 len(args) == 0 |
|
88 ): |
|
89 # TODO: implement this |
|
90 if len(args) >= 2: |
|
91 program = args[0] |
|
92 arguments = args[1] |
|
93 if len(args) > 2: |
|
94 mode = args[2] |
|
95 else: |
|
96 mode = module.QIODevice.ReadWrite |
|
97 else: |
|
98 program = self.program() |
|
99 arguments = self.arguments() |
|
100 if len(args) == 1: |
|
101 mode = args[0] |
|
102 else: |
|
103 mode = module.QIODevice.ReadWrite |
|
104 if "python" in program.lower(): |
|
105 # assume a Python script is to be started |
|
106 newArgs = self.__modifyArgs(arguments) |
|
107 super(QProcessWrapper, self).start(program, newArgs, mode) |
|
108 else: |
|
109 super(QProcessWrapper, self).start(*args, **kwargs) |
|
110 else: |
|
111 super(QProcessWrapper, self).start(*args, **kwargs) |
|
112 |
|
113 _startOptions = startOptions[:] |
|
114 module.QProcess = QProcessWrapper |