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

« back to all changes in this revision

Viewing changes to lib/common/util.py

  • Committer: wagrant
  • Date: 2008-07-24 04:45:12 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:945
specialhome: Rebrand personal to mywork (by majority), refactor a bit,
             and improve error handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
        self.message = message
48
48
        self.args = (httpcode, message)
49
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
 
50
64
def make_path(path):
51
65
    """Given a path relative to the IVLE root, makes the path relative to the
52
66
    site root using conf.root_dir. This path can be used in URLs sent to the
110
124
    else:
111
125
        return tuple(splitpath)
112
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
 
113
145
# Initialise mime types library
114
146
mimetypes.init()
115
147
for (ext, mimetype) in conf.mimetypes.additional_mime_types.items():