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

« back to all changes in this revision

Viewing changes to www/apps/fileservice/action.py

  • Committer: drtomc
  • Date: 2008-01-30 22:53:00 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:340
Mostly fix the block-of-text to the console problem. The tutorial system should call "block" rather than "chat" to send the block of text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
134
134
 
135
135
import pysvn
136
136
 
137
 
from common import (util, studpath, zip)
 
137
from common import (util, studpath)
138
138
 
139
139
# Make a Subversion client object
140
140
svnclient = pysvn.Client()
179
179
        raise ActionError("Unknown action")
180
180
    action(req, fields)
181
181
 
182
 
def actionpath_to_urlpath(req, path):
183
 
    """Determines the URL path (relative to the student home) upon which the
184
 
    action is intended to act. See actionpath_to_local.
185
 
    """
186
 
    if path is None:
187
 
        return req.path
188
 
    elif len(path) > 0 and path[0] == os.sep:
189
 
        # Relative to student home
190
 
        return path[1:]
191
 
    else:
192
 
        # Relative to req.path
193
 
        return os.path.join(req.path, path)
194
 
 
195
182
def actionpath_to_local(req, path):
196
183
    """Determines the local path upon which an action is intended to act.
197
184
    Note that fileservice actions accept two paths: the request path,
208
195
 
209
196
    Does not mutate req.
210
197
    """
211
 
    _, r = studpath.url_to_local(actionpath_to_urlpath(req, path))
 
198
    if path is None:
 
199
        path = req.path
 
200
    elif len(path) > 0 and path[0] == os.sep:
 
201
        # Relative to student home
 
202
        path = path[1:]
 
203
    else:
 
204
        # Relative to req.path
 
205
        path = os.path.join(req.path, path)
 
206
 
 
207
    _, r = studpath.url_to_local(path)
212
208
    if r is None:
213
209
        raise ActionError("Invalid path")
214
210
    return r
292
288
 
293
289
    Reads fields: 'path', 'data' (file upload)
294
290
    """
295
 
    # TODO: Read field "unpack".
296
291
    # Important: Data is "None" if the file submitted is empty.
297
292
    path = fields.getfirst('path')
298
293
    data = fields.getfirst('data')
310
305
    except OSError:
311
306
        raise ActionError("Could not write to target file")
312
307
 
313
 
def action_putfiles(req, fields):
314
 
    """Writes data to one or more files in a directory, overwriting them if
315
 
    it they exist.
316
 
 
317
 
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
318
 
    """
319
 
    # Important: Data is "None" if the file submitted is empty.
320
 
    path = fields.getfirst('path')
321
 
    data = fields.getlist('data')
322
 
    unpack = fields.getfirst('unpack')
323
 
    if unpack is None:
324
 
        unpack = False
325
 
    else:
326
 
        unpack = True
327
 
    if path is None:
328
 
        raise ActionError("Required field missing")
329
 
    path = actionpath_to_urlpath(req, path)
330
 
    goterror = False
331
 
 
332
 
    for datum in data:
333
 
        # Each of the uploaded files
334
 
        filepath = os.path.join(path, datum.filename)
335
 
        filedata = datum.file
336
 
 
337
 
        if unpack and datum.filename.lower().endswith(".zip"):
338
 
            # A zip file - unpack it instead of just copying
339
 
            # TODO: Use the magic number instead of file extension
340
 
            # Note: Just unzip into the current directory (ignore the
341
 
            # filename)
342
 
            try:
343
 
                zip.unzip(path, filedata)
344
 
            except (OSError, IOError):
345
 
                goterror = True
346
 
        else:
347
 
            # Not a zip file
348
 
            _, filepath_local = studpath.url_to_local(filepath)
349
 
            if filepath_local is None:
350
 
                raise ActionError("Invalid path")
351
 
 
352
 
            # Copy the contents of file object 'data' to the path 'path'
353
 
            try:
354
 
                dest = open(filepath_local, 'wb')
355
 
                if data is not None:
356
 
                    shutil.copyfileobj(filedata, dest)
357
 
            except OSError:
358
 
                goterror = True
359
 
 
360
 
    if goterror:
361
 
        if len(data) == 1:
362
 
            raise ActionError("Could not write to target file")
363
 
        else:
364
 
            raise ActionError(
365
 
                "Could not write to one or more of the target files")
366
 
 
367
308
def action_copy_or_cut(req, fields, mode):
368
309
    """Marks specified files on the clipboard, stored in the
369
310
    browser session. Sets clipboard for either a cut or copy operation
550
491
    "remove" : action_remove,
551
492
    "move" : action_move,
552
493
    "putfile" : action_putfile,
553
 
    "putfiles" : action_putfiles,
554
494
 
555
495
    "copy" : action_copy,
556
496
    "cut" : action_cut,