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

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: mattgiuca
  • Date: 2008-02-28 08:10:34 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:611
"New File" now works. (This is a MUCH better replacement for having to go to
the editor and save a file).
listing.js: Added action_newfile.
browser.js: Calls action_newfile when "New File" is chosen.
fileservice_lib/action: Now the "putfile" action accepts blank data (which is
    how newfile works - also fixes a bug where the editor can't save an empty
    file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import common.chat
18
18
 
19
19
class Interrupt(Exception):
20
 
    def __init__(self):
21
 
        Exception.__init__(self, "Interrupted!")
 
20
    pass
22
21
 
23
22
class ExpiryTimer(object):
24
23
    def __init__(self, idle):
48
47
        ln = self.lineQ.get()
49
48
        if 'chat' in ln:
50
49
            return ln['chat']
51
 
        if 'interrupt' in ln:
52
 
            raise Interrupt()
53
50
 
54
51
class StdoutToWeb(object):
55
52
    def __init__(self, cmdQ, lineQ):
58
55
        self.remainder = ''
59
56
 
60
57
    def write(self, stuff):
61
 
        self.remainder = self.remainder + stuff
62
 
 
63
 
        # if there's less than 128 bytes, buffer
64
 
        if len(self.remainder) < 128:
65
 
            return
66
 
 
67
 
        # if there's lots, then send it in 1/2K blocks
68
 
        while len(self.remainder) > 512:
69
 
            blk = self.remainder[0:512]
70
 
            self.cmdQ.put({"output":blk})
71
 
            expiry.ping()
72
 
            ln = self.lineQ.get()
73
 
            self.remainder = self.remainder[512:]
74
 
 
75
 
        # Finally, split the remainder up into lines, and ship all the
76
 
        # completed lines off to the server.
77
 
        lines = self.remainder.split("\n")
 
58
        # Split the content up into lines, and ship all the completed
 
59
        # lines off to the server.
 
60
        lines = stuff.split("\n")
 
61
        lines[0] = self.remainder + lines[0]
78
62
        self.remainder = lines[-1]
79
63
        del lines[-1]
80
64
 
96
80
            if 'interrupt' in ln:
97
81
                raise Interrupt()
98
82
 
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
 
 
120
83
class PythonRunner(Thread):
121
84
    def __init__(self, cmdQ, lineQ):
122
85
        self.cmdQ = cmdQ
123
86
        self.lineQ = lineQ
124
 
        self.webio = WebIO(self.cmdQ, self.lineQ)
 
87
        self.stdout = None
125
88
        Thread.__init__(self)
126
89
 
127
90
    def execCmd(self, cmd):
128
91
        try:
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()
 
92
            sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
 
93
            self.stdout = StdoutToWeb(self.cmdQ, self.lineQ)
 
94
            sys.stdout = self.stdout
 
95
            sys.stderr = self.stdout
 
96
            res = eval(cmd, self.globs, self.locls)
 
97
            self.stdout.flush()
134
98
            self.cmdQ.put({"okay":res})
135
99
            self.curr_cmd = ''
136
100
        except Exception, exc:
137
 
            self.webio.flush()
138
 
            exc_classname = exc.__class__.__name__
139
 
            self.cmdQ.put({"exc": exc_classname + ": " + str(exc)})
 
101
            self.stdout.flush()
 
102
            self.cmdQ.put({"exc":str(exc)})
140
103
            self.curr_cmd = ''
141
104
 
142
105
    def run(self):
143
106
        self.globs = {}
144
107
        self.globs['__builtins__'] = globals()['__builtins__']
 
108
        self.locls = {}
145
109
        self.curr_cmd = ''
146
110
        compiler = codeop.CommandCompiler()
147
111
 
162
126
                    else:
163
127
                        self.execCmd(cmd)
164
128
                except Exception, exc:
165
 
                    self.webio.flush()
 
129
                    self.stdout.flush()
166
130
                    self.cmdQ.put({"exc":str(exc)})
167
131
                    self.curr_cmd = ''
168
132
            if 'block' in ln:
171
135
                    cmd = compile(ln['block'], "<web session>", 'exec');
172
136
                    self.execCmd(cmd)
173
137
                except Exception, exc:
174
 
                    self.webio.flush()
 
138
                    self.stdout.flush()
175
139
                    self.cmdQ.put({"exc":str(exc)})
176
140
                    self.curr_cmd = ''
177
141
 
211
175
    if len(sys.argv) >= 4:
212
176
        # working_dir
213
177
        os.chdir(sys.argv[3])
214
 
        # Make python's search path follow the cwd
215
 
        sys.path[0] = ''
216
 
        os.environ['HOME'] = sys.argv[3]
217
178
 
218
179
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)