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

« back to all changes in this revision

Viewing changes to services/serveservice

  • Committer: William Grant
  • Date: 2010-07-28 04:13:05 UTC
  • mfrom: (1801.1.2 die-cjson-die)
  • Revision ID: grantw@unimelb.edu.au-20100728041305-xwypm3cn1l1mnki1
Port from cjson to (simple)json.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import StringIO
26
26
from optparse import OptionParser
27
27
 
28
 
import cjson
 
28
try:
 
29
    import json
 
30
except ImportError:
 
31
    import simplejson as json
29
32
 
30
33
from ivle import zip as zipmod
31
34
import ivle.conf.app.server
 
35
import ivle.mimetypes
32
36
 
33
37
def determine_file_type(filename):
34
38
    filetype = mimetypes.guess_type(filename)[0]
35
39
    if filetype is None:
36
 
         filetype = ivle.conf.mimetypes.default_mimetype
 
40
         filetype = ivle.mimetypes.DEFAULT_MIMETYPE
37
41
    return filetype
38
42
 
39
43
def throw_error(message, extra={}):
40
44
    error = {'error': message}
41
45
    error.update(extra)
42
 
    print cjson.encode(error)
 
46
    print json.dumps(error)
43
47
    sys.exit(0)
44
48
 
45
49
parser = OptionParser()
111
115
    else:
112
116
        if not download and \
113
117
           determine_file_type(filename) in ivle.conf.app.server.interpreters:
114
 
            throw_error('is-executable', {'path': filename})
 
118
            throw_error('is-executable', {'path': filename.decode('utf-8')})
115
119
 
116
 
        if (ivle.conf.app.server.blacklist_served_filetypes and \
 
120
        if not download and (
 
121
            (ivle.conf.app.server.blacklist_served_filetypes and \
117
122
                determine_file_type(filename) in \
118
123
                ivle.conf.app.server.served_filetypes_blacklist) or \
119
124
            (ivle.conf.app.server.served_filetypes_whitelist and \
120
125
                determine_file_type(filename) not in \
121
 
                ivle.conf.app.server.served_filetypes_whitelist):
 
126
                ivle.conf.app.server.served_filetypes_whitelist)):
122
127
            throw_error('forbidden')
123
128
 
124
129
if zipmode:
136
141
    zipfile = StringIO.StringIO()
137
142
    zipmod.make_zip(zipbasepath, paths, zipfile)
138
143
 
139
 
    print cjson.encode({'type': zip_mimetype,
140
 
                        'name': zipfilename})
141
 
    print zipfile.getvalue()
 
144
    print json.dumps({'type': zip_mimetype,
 
145
                      'name': zipfilename.decode('utf-8')})
 
146
 
 
147
    stream = zipfile
 
148
    stream.seek(0)
142
149
else:
143
 
    print cjson.encode({'type': determine_file_type(filename),
144
 
                        'name': os.path.basename(filename)})
145
 
    print open(filename).read()
 
150
 
 
151
    print json.dumps({'type': determine_file_type(filename),
 
152
                      'name': os.path.basename(filename).decode('utf-8')})
 
153
    stream = open(filename)
 
154
 
 
155
next = stream.read(1024)
 
156
while next:
 
157
    sys.stdout.write(next)
 
158
    next = stream.read(1024)