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

« back to all changes in this revision

Viewing changes to lib/common/util.py

  • Committer: dcoles
  • Date: 2008-07-27 15:22:45 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:962
Serve: Fix the working directory used by interpretservice so that paths are 
reletive to the users home directory (rather than 
/opt/ivle/scriptsopt/ivle/scripts). (Fixes regression in new serve method)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
root_dir = conf.root_dir
33
33
 
 
34
class IVLEError(Exception):
 
35
    """
 
36
    This is the "standard" exception class for IVLE errors.
 
37
    It is the ONLY exception which should propagate to the top - it will then
 
38
    be displayed to the user as an HTTP error with the given code.
 
39
 
 
40
    All other exceptions are considered IVLE bugs and should not occur
 
41
    (they will display a traceback).
 
42
 
 
43
    This error should not be raised directly. Call req.throw_error.
 
44
    """
 
45
    def __init__(self, httpcode, message=None):
 
46
        self.httpcode = httpcode
 
47
        self.message = message
 
48
        self.args = (httpcode, message)
 
49
 
 
50
class IVLEJailError(Exception):
 
51
    """
 
52
    This exception indicates an error that occurred inside an IVLE CGI script
 
53
    inside the jail. It should never be raised directly - only by the 
 
54
    interpreter.
 
55
 
 
56
    Information will be retrieved from it, and then treated as a normal
 
57
    error.
 
58
    """
 
59
    def __init__(self, type_str, message, info):
 
60
        self.type_str = type_str
 
61
        self.message = message
 
62
        self.info = info
 
63
 
34
64
def make_path(path):
35
65
    """Given a path relative to the IVLE root, makes the path relative to the
36
66
    site root using conf.root_dir. This path can be used in URLs sent to the
94
124
    else:
95
125
        return tuple(splitpath)
96
126
 
 
127
def open_exercise_file(exercisename):
 
128
    """Given an exercise name, opens the corresponding XML file for reading.
 
129
    Returns None if the exercise file was not found.
 
130
    (For tutorials / worksheets).
 
131
    """
 
132
    # First normalise the path
 
133
    exercisename = os.path.normpath(exercisename)
 
134
    # Now if it begins with ".." or separator, then it's illegal
 
135
    if exercisename.startswith("..") or exercisename.startswith(os.sep):
 
136
        exercisefile = None
 
137
    else:
 
138
        exercisefile = os.path.join(conf.exercises_base, exercisename)
 
139
 
 
140
    try:
 
141
        return open(exercisefile)
 
142
    except (TypeError, IOError):    # TypeError if exercisefile == None
 
143
        return None
 
144
 
97
145
# Initialise mime types library
98
146
mimetypes.init()
99
147
for (ext, mimetype) in conf.mimetypes.additional_mime_types.items():
118
166
                return filename[filename.rindex('.')+1:].upper() + " file"
119
167
            except ValueError:
120
168
                return "File"
 
169
 
 
170
def send_terms_of_service(req):
 
171
    """
 
172
    Sends the Terms of Service document to the req object.
 
173
    This consults conf to find out where the TOS is located on disk, and sends
 
174
    that. If it isn't found, it sends a generic message explaining to admins
 
175
    how to create a real one.
 
176
    """
 
177
    try:
 
178
        req.sendfile(conf.tos_path)
 
179
    except IOError:
 
180
        req.write(
 
181
"""<h1>Terms of Service</h1>
 
182
<p><b>*** SAMPLE ONLY ***</b></p>
 
183
<p>This is the text of the IVLE Terms of Service.</p>
 
184
<p>The administrator should create a license file with an appropriate
 
185
"Terms of Service" license for your organisation.</p>
 
186
<h2>Instructions for Administrators</h2>
 
187
<p>You are seeing this message because you have not configured a Terms of
 
188
Service document.</p>
 
189
<p>When you configured IVLE, you specified a path to the Terms of Service
 
190
document (this is found in <b><tt>lib/conf/conf.py</tt></b> under
 
191
"<tt>tos_path</tt>").</p>
 
192
<p>Create a file at this location; an HTML file with the appropriately-worded
 
193
license.</p>
 
194
<p>This should be a normal XHTML file, except it should not contain
 
195
<tt>html</tt>, <tt>head</tt> or <tt>body</tt> elements - it should
 
196
just be the contents of a body element (IVLE will wrap it accordingly).</p>
 
197
<p>This will automatically be used as the license text instead of this
 
198
placeholder text.</p>
 
199
""")