~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-03 04:20:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:803
Setup: Modularised setup.py so it is now no longer over 1000 lines. This should 
allow us to get in there and tidy up each module much easier. Also removed 
updatejails since this functionality seems to be duplicated with remakeuser.py 
and remakealluser.py scripts.

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
 
34
50
def make_path(path):
35
51
    """Given a path relative to the IVLE root, makes the path relative to the
36
52
    site root using conf.root_dir. This path can be used in URLs sent to the
94
110
    else:
95
111
        return tuple(splitpath)
96
112
 
 
113
def open_exercise_file(exercisename):
 
114
    """Given an exercise name, opens the corresponding XML file for reading.
 
115
    Returns None if the exercise file was not found.
 
116
    (For tutorials / worksheets).
 
117
    """
 
118
    # First normalise the path
 
119
    exercisename = os.path.normpath(exercisename)
 
120
    # Now if it begins with ".." or separator, then it's illegal
 
121
    if exercisename.startswith("..") or exercisename.startswith(os.sep):
 
122
        exercisefile = None
 
123
    else:
 
124
        exercisefile = os.path.join(conf.exercises_base, exercisename)
 
125
 
 
126
    try:
 
127
        return open(exercisefile)
 
128
    except (TypeError, IOError):    # TypeError if exercisefile == None
 
129
        return None
 
130
 
97
131
# Initialise mime types library
98
132
mimetypes.init()
99
133
for (ext, mimetype) in conf.mimetypes.additional_mime_types.items():
118
152
                return filename[filename.rindex('.')+1:].upper() + " file"
119
153
            except ValueError:
120
154
                return "File"
 
155
 
 
156
def send_terms_of_service(req):
 
157
    """
 
158
    Sends the Terms of Service document to the req object.
 
159
    This consults conf to find out where the TOS is located on disk, and sends
 
160
    that. If it isn't found, it sends a generic message explaining to admins
 
161
    how to create a real one.
 
162
    """
 
163
    try:
 
164
        req.sendfile(conf.tos_path)
 
165
    except IOError:
 
166
        req.write(
 
167
"""<h1>Terms of Service</h1>
 
168
<p><b>*** SAMPLE ONLY ***</b></p>
 
169
<p>This is the text of the IVLE Terms of Service.</p>
 
170
<p>The administrator should create a license file with an appropriate
 
171
"Terms of Service" license for your organisation.</p>
 
172
<h2>Instructions for Administrators</h2>
 
173
<p>You are seeing this message because you have not configured a Terms of
 
174
Service document.</p>
 
175
<p>When you configured IVLE, you specified a path to the Terms of Service
 
176
document (this is found in <b><tt>lib/conf/conf.py</tt></b> under
 
177
"<tt>tos_path</tt>").</p>
 
178
<p>Create a file at this location; an HTML file with the appropriately-worded
 
179
license.</p>
 
180
<p>This should be a normal XHTML file, except it should not contain
 
181
<tt>html</tt>, <tt>head</tt> or <tt>body</tt> elements - it should
 
182
just be the contents of a body element (IVLE will wrap it accordingly).</p>
 
183
<p>This will automatically be used as the license text instead of this
 
184
placeholder text.</p>
 
185
""")