]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
c368d904 | 5 | |
8fa876ca | 6 | import wx |
c368d904 RD |
7 | |
8 | #---------------------------------------------------------------------- | |
9 | ||
8fa876ca | 10 | class TestPanel(wx.Panel): |
c368d904 | 11 | def __init__(self, parent, ID, log): |
8fa876ca | 12 | wx.Panel.__init__(self, parent, ID) |
c368d904 RD |
13 | self.log = log |
14 | ||
15 | self.process = None | |
8fa876ca | 16 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
c368d904 RD |
17 | |
18 | # We can either derive from wxProcess and override OnTerminate | |
19 | # or we can let wxProcess send this window an event that is | |
20 | # caught in the normal way... | |
8fa876ca | 21 | self.Bind(wx.EVT_END_PROCESS, self.OnProcessEnded) |
c368d904 RD |
22 | |
23 | ||
24 | # Make the controls | |
8fa876ca RD |
25 | prompt = wx.StaticText(self, -1, 'Command line:') |
26 | self.cmd = wx.TextCtrl(self, -1, 'python -u data/echo.py') | |
27 | self.exBtn = wx.Button(self, -1, 'Execute') | |
c368d904 | 28 | |
8fa876ca | 29 | self.out = wx.TextCtrl(self, -1, '', style=wx.TE_MULTILINE|wx.TE_READONLY) |
c368d904 | 30 | |
8fa876ca RD |
31 | self.inp = wx.TextCtrl(self, -1, '', style=wx.TE_PROCESS_ENTER) |
32 | self.sndBtn = wx.Button(self, -1, 'Send') | |
33 | self.termBtn = wx.Button(self, -1, 'Close Stream') | |
1e4a197e RD |
34 | self.inp.Enable(False) |
35 | self.sndBtn.Enable(False) | |
36 | self.termBtn.Enable(False) | |
c368d904 RD |
37 | |
38 | # Hook up the events | |
8fa876ca RD |
39 | self.Bind(wx.EVT_BUTTON, self.OnExecuteBtn, self.exBtn) |
40 | self.Bind(wx.EVT_BUTTON, self.OnSendText, self.sndBtn) | |
41 | self.Bind(wx.EVT_BUTTON, self.OnCloseStream, self.termBtn) | |
42 | self.Bind(wx.EVT_TEXT_ENTER, self.OnSendText, self.inp) | |
c368d904 RD |
43 | |
44 | ||
45 | # Do the layout | |
8fa876ca RD |
46 | box1 = wx.BoxSizer(wx.HORIZONTAL) |
47 | box1.Add(prompt, 0, wx.ALIGN_CENTER) | |
48 | box1.Add(self.cmd, 1, wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT, 5) | |
c368d904 RD |
49 | box1.Add(self.exBtn, 0) |
50 | ||
8fa876ca RD |
51 | box2 = wx.BoxSizer(wx.HORIZONTAL) |
52 | box2.Add(self.inp, 1, wx.ALIGN_CENTER) | |
53 | box2.Add(self.sndBtn, 0, wx.LEFT, 5) | |
54 | box2.Add(self.termBtn, 0, wx.LEFT, 5) | |
c368d904 | 55 | |
8fa876ca RD |
56 | sizer = wx.BoxSizer(wx.VERTICAL) |
57 | sizer.Add(box1, 0, wx.EXPAND|wx.ALL, 10) | |
58 | sizer.Add(self.out, 1, wx.EXPAND|wx.ALL, 10) | |
59 | sizer.Add(box2, 0, wx.EXPAND|wx.ALL, 10) | |
c368d904 RD |
60 | |
61 | self.SetSizer(sizer) | |
1e4a197e | 62 | self.SetAutoLayout(True) |
c368d904 RD |
63 | |
64 | ||
65 | def __del__(self): | |
66 | if self.process is not None: | |
67 | self.process.Detach() | |
68 | self.process.CloseOutput() | |
69 | self.process = None | |
70 | ||
71 | ||
72 | def OnExecuteBtn(self, evt): | |
73 | cmd = self.cmd.GetValue() | |
74 | ||
8fa876ca | 75 | self.process = wx.Process(self) |
c368d904 | 76 | self.process.Redirect(); |
8fa876ca | 77 | pid = wx.Execute(cmd, wx.EXEC_ASYNC, self.process) |
c368d904 RD |
78 | self.log.write('OnExecuteBtn: "%s" pid: %s\n' % (cmd, pid)) |
79 | ||
1e4a197e RD |
80 | self.inp.Enable(True) |
81 | self.sndBtn.Enable(True) | |
82 | self.termBtn.Enable(True) | |
83 | self.cmd.Enable(False) | |
84 | self.exBtn.Enable(False) | |
c368d904 RD |
85 | self.inp.SetFocus() |
86 | ||
87 | ||
88 | def OnSendText(self, evt): | |
89 | text = self.inp.GetValue() | |
90 | self.inp.SetValue('') | |
91 | self.log.write('OnSendText: "%s"\n' % text) | |
92 | self.process.GetOutputStream().write(text + '\n') | |
93 | self.inp.SetFocus() | |
94 | ||
95 | ||
96 | def OnCloseStream(self, evt): | |
97 | self.log.write('OnCloseStream\n') | |
a541c325 | 98 | #print "b4 CloseOutput" |
c368d904 | 99 | self.process.CloseOutput() |
a541c325 | 100 | #print "after CloseOutput" |
c368d904 RD |
101 | |
102 | def OnIdle(self, evt): | |
103 | if self.process is not None: | |
104 | stream = self.process.GetInputStream() | |
105 | ||
1e4a197e | 106 | if stream.CanRead(): |
c368d904 RD |
107 | text = stream.read() |
108 | self.out.AppendText(text) | |
109 | ||
110 | ||
111 | def OnProcessEnded(self, evt): | |
112 | self.log.write('OnProcessEnded, pid:%s, exitCode: %s\n' % | |
113 | (evt.GetPid(), evt.GetExitCode())) | |
114 | ||
115 | stream = self.process.GetInputStream() | |
8fa876ca | 116 | |
1e4a197e | 117 | if stream.CanRead(): |
c368d904 RD |
118 | text = stream.read() |
119 | self.out.AppendText(text) | |
120 | ||
121 | self.process.Destroy() | |
122 | self.process = None | |
1e4a197e RD |
123 | self.inp.Enable(False) |
124 | self.sndBtn.Enable(False) | |
125 | self.termBtn.Enable(False) | |
126 | self.cmd.Enable(True) | |
127 | self.exBtn.Enable(True) | |
c368d904 RD |
128 | |
129 | ||
130 | #---------------------------------------------------------------------- | |
131 | ||
132 | def runTest(frame, nb, log): | |
133 | win = TestPanel(nb, -1, log) | |
134 | return win | |
135 | ||
136 | ||
137 | #---------------------------------------------------------------------- | |
138 | ||
139 | ||
140 | overview = """\ | |
141 | <html><body> | |
142 | <h2>wxProcess</h2> | |
143 | ||
144 | wxProcess lets you get notified when an asyncronous child process | |
145 | started by wxExecute terminates, and also to get input/output streams | |
146 | for the child process's stdout, stderr and stdin. | |
147 | ||
148 | <p> | |
149 | This demo launches a simple python script that echos back on stdout | |
150 | lines that it reads from stdin. You can send text to the echo | |
151 | process' stdin by typing in the lower textctrl and clicking Send. | |
152 | ||
153 | <p> | |
154 | Clicking the Close Stream button will close the demo's end of the | |
155 | stdin pipe to the child process. In our case that will cause the | |
156 | child process to exit its main loop. | |
157 | ||
158 | </body><html> | |
159 | """ | |
1fded56b RD |
160 | |
161 | ||
1fded56b RD |
162 | if __name__ == '__main__': |
163 | import sys,os | |
164 | import run | |
165 | run.main(['', os.path.basename(sys.argv[0])]) |