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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: dcoles
  • Date: 2008-07-02 04:34:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:800
Docs: Updated docs to show debootstrap dependency and relect changes to jail 
building.

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
111
 
            res = eval(cmd, self.globs, self.locls)
112
 
            self.stdout.flush()
 
129
            sys.stdin = self.webio
 
130
            sys.stdout = self.webio
 
131
            sys.stderr = self.webio
 
132
            res = eval(cmd, self.globs)
 
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()
117
 
            self.cmdQ.put({"exc":str(exc)})
 
137
            self.webio.flush()
 
138
            exc_classname = exc.__class__.__name__
 
139
            self.cmdQ.put({"exc": exc_classname + ": " + str(exc)})
118
140
            self.curr_cmd = ''
119
141
 
120
142
    def run(self):
121
143
        self.globs = {}
122
144
        self.globs['__builtins__'] = globals()['__builtins__']
123
 
        self.locls = {}
124
145
        self.curr_cmd = ''
125
146
        compiler = codeop.CommandCompiler()
126
147
 
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
 
190
211
    if len(sys.argv) >= 4:
191
212
        # working_dir
192
213
        os.chdir(sys.argv[3])
 
214
        # Make python's search path follow the cwd
 
215
        sys.path[0] = ''
193
216
        os.environ['HOME'] = sys.argv[3]
194
217
 
195
218
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)