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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: William Grant
  • Date: 2009-04-28 06:02:41 UTC
  • Revision ID: grantw@unimelb.edu.au-20090428060241-t4gnwl35maukfvfg
Move ivle.conf.mimetypes to ivle.mimetypes, and rename things in it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# Contains common utility functions.
23
23
 
24
24
import os
 
25
import datetime
 
26
 
 
27
import ivle.conf
25
28
 
26
29
class IVLEError(Exception):
27
 
    """Legacy general IVLE exception.
28
 
 
29
 
    This is the old "standard" exception class for IVLE errors. It is only
30
 
    used in fileservice, and should not be used in any new code.
 
30
    """
 
31
    This is the "standard" exception class for IVLE errors.
 
32
    It is the ONLY exception which should propagate to the top - it will then
 
33
    be displayed to the user as an HTTP error with the given code.
 
34
 
 
35
    All other exceptions are considered IVLE bugs and should not occur
 
36
    (they will display a traceback).
 
37
 
 
38
    This error should not be raised directly. Call req.throw_error.
31
39
    """
32
40
    def __init__(self, httpcode, message=None):
33
41
        self.httpcode = httpcode
35
43
        self.args = (httpcode, message)
36
44
 
37
45
class IVLEJailError(Exception):
38
 
    """Exception proxying an in-jail error.
39
 
 
 
46
    """
40
47
    This exception indicates an error that occurred inside an IVLE CGI script
41
48
    inside the jail. It should never be raised directly - only by the 
42
49
    interpreter.
59
66
    def __repr__(self):
60
67
        return "<Fake %s %s>"%(self.type, self.name)
61
68
 
 
69
def make_path(path):
 
70
    """Given a path relative to the IVLE root, makes the path relative to the
 
71
    site root using ivle.conf.root_dir. This path can be used in URLs sent to
 
72
    the client."""
 
73
    return os.path.join(ivle.conf.root_dir, path)
 
74
 
 
75
def unmake_path(path):
 
76
    """Given a path relative to the site root, makes the path relative to the
 
77
    IVLE root by removing ivle.conf.root_dir if it appears at the beginning. If
 
78
    it does not appear at the beginning, returns path unchanged. Also
 
79
    normalises the path."""
 
80
    path = os.path.normpath(path)
 
81
    root = os.path.normpath(ivle.conf.root_dir)
 
82
 
 
83
    if path.startswith(root):
 
84
        path = path[len(root):]
 
85
        # Take out the slash as well
 
86
        if len(path) > 0 and path[0] == os.sep:
 
87
            path = path[1:]
 
88
 
 
89
    return path
 
90
 
62
91
def split_path(path):
63
92
    """Given a path, returns a tuple consisting of the top-level directory in
64
93
    the path, and the rest of the path. Note that both items in the tuple will
95
124
        return tuple(splitpath)
96
125
 
97
126
def incomplete_utf8_sequence(byteseq):
98
 
    """Calculate the completeness of a UTF-8 encoded string.
99
 
 
 
127
    """
 
128
    str -> int
100
129
    Given a UTF-8-encoded byte sequence (str), returns the number of bytes at
101
130
    the end of the string which comprise an incomplete UTF-8 character
102
131
    sequence.
174
203
        return count
175
204
 
176
205
def object_to_dict(attrnames, obj):
177
 
    """Convert an object into a dictionary.
178
 
 
179
 
    This takes a shallow copy of the object.
180
 
 
181
 
    @param attrnames: Set (or iterable) of names of attributes to be copied
182
 
                      into the dictionary. (We don't auto-lookup, because this
183
 
                      function needs to be used on magical objects).
 
206
    """
 
207
    Convert an object into a dictionary. This takes a shallow copy of the
 
208
    object.
 
209
    attrnames: Set (or iterable) of names of attributes to be copied into the
 
210
        dictionary. (We don't auto-lookup, because this function needs to be
 
211
        used on magical objects).
184
212
    """
185
213
    return dict((k, getattr(obj, k))
186
214
        for k in attrnames if not k.startswith('_'))