~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-04-28 04:12:35 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:750
Console: Flush current output before requesting input from Web

This makes raw_input() and other interactive scripts behave in a usable 
fashion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
            if 'interrupt' in ln:
97
97
                raise Interrupt()
98
98
 
 
99
class WebIO(object):
 
100
    """Provides a file like interface to the Web front end of the console.
 
101
    You may print text to the console using write(), flush any buffered output 
 
102
    using flush(), or request text from the console using readline()"""
 
103
    
 
104
    def __init__(self, cmdQ, lineQ):
 
105
        self.cmdQ = cmdQ
 
106
        self.lineQ = lineQ
 
107
        self.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
108
        self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
 
109
 
 
110
    def write(self, stuff):
 
111
        self.stdout.write(stuff)
 
112
 
 
113
    def flush(self):
 
114
        self.stdout.flush()
 
115
 
 
116
    def readline(self):
 
117
        self.stdout.flush()
 
118
        return self.stdin.readline()
 
119
 
99
120
class PythonRunner(Thread):
100
121
    def __init__(self, cmdQ, lineQ):
101
122
        self.cmdQ = cmdQ
102
123
        self.lineQ = lineQ
103
 
        self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
 
124
        self.webio = WebIO(self.cmdQ, self.lineQ)
104
125
        Thread.__init__(self)
105
126
 
106
127
    def execCmd(self, cmd):
107
128
        try:
108
 
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
109
 
            sys.stdout = self.stdout
110
 
            sys.stderr = self.stdout
 
129
            sys.stdin = self.webio
 
130
            sys.stdout = self.webio
 
131
            sys.stderr = self.webio
111
132
            res = eval(cmd, self.globs)
112
 
            self.stdout.flush()
 
133
            self.webio.flush()
113
134
            self.cmdQ.put({"okay":res})
114
135
            self.curr_cmd = ''
115
136
        except Exception, exc:
116
 
            self.stdout.flush()
 
137
            self.webio.flush()
117
138
            exc_classname = exc.__class__.__name__
118
139
            self.cmdQ.put({"exc": exc_classname + ": " + str(exc)})
119
140
            self.curr_cmd = ''
141
162
                    else:
142
163
                        self.execCmd(cmd)
143
164
                except Exception, exc:
144
 
                    self.stdout.flush()
 
165
                    self.webio.flush()
145
166
                    self.cmdQ.put({"exc":str(exc)})
146
167
                    self.curr_cmd = ''
147
168
            if 'block' in ln:
150
171
                    cmd = compile(ln['block'], "<web session>", 'exec');
151
172
                    self.execCmd(cmd)
152
173
                except Exception, exc:
153
 
                    self.stdout.flush()
 
174
                    self.webio.flush()
154
175
                    self.cmdQ.put({"exc":str(exc)})
155
176
                    self.curr_cmd = ''
156
177