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

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Module: util
19
# Author: Matt Giuca
20
# Date: 12/12/2007
21
22
# Contains common utility functions.
23
24
import os
1165.3.75 by Matt Giuca
ivle/util.py: Added safe_rmtree, copied from Python2.6 shutil.rmtree (which
25
import sys
26
import stat
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
27
28
class IVLEError(Exception):
1213 by William Grant
Drop ivle.util.make_path (replaced by Request.make_path) and fix docstrings.
29
    """Legacy general IVLE exception.
30
31
    This is the old "standard" exception class for IVLE errors. It is only
32
    used in fileservice, and should not be used in any new code.
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
33
    """
1282 by William Grant
Define IVLEError.message explicitly, as BaseException.message is deprecated and generates warnings.
34
35
    message = None
36
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
37
    def __init__(self, httpcode, message=None):
38
        self.httpcode = httpcode
39
        self.message = message
40
        self.args = (httpcode, message)
41
42
class IVLEJailError(Exception):
1213 by William Grant
Drop ivle.util.make_path (replaced by Request.make_path) and fix docstrings.
43
    """Exception proxying an in-jail error.
44
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
45
    This exception indicates an error that occurred inside an IVLE CGI script
46
    inside the jail. It should never be raised directly - only by the 
47
    interpreter.
48
49
    Information will be retrieved from it, and then treated as a normal
50
    error.
51
    """
52
    def __init__(self, type_str, message, info):
53
        self.type_str = type_str
54
        self.message = message
55
        self.info = info
56
57
class FakeObject(object):
58
    """ A representation of an object that can't be Pickled """
59
    def __init__(self, type, name, attrib={}):
60
        self.type = type
61
        self.name = name
62
        self.attrib = attrib
63
64
    def __repr__(self):
65
        return "<Fake %s %s>"%(self.type, self.name)
66
67
def split_path(path):
68
    """Given a path, returns a tuple consisting of the top-level directory in
69
    the path, and the rest of the path. Note that both items in the tuple will
70
    NOT begin with a slash, regardless of whether the original path did. Also
71
    normalises the path.
72
73
    Always returns a pair of strings, except for one special case, in which
74
    the path is completely empty (or just a single slash). In this case the
75
    return value will be (None, ''). But still always returns a pair.
76
77
    Examples:
78
79
    >>> split_path("")
80
    (None, '')
81
    >>> split_path("/")
82
    (None, '')
83
    >>> split_path("home")
84
    ('home', '')
85
    >>> split_path("home/docs/files")
86
    ('home', 'docs/files')
87
    >>> split_path("//home/docs/files")
88
    ('', 'home/docs/files')
89
    """
90
    path = os.path.normpath(path)
91
    # Ignore the opening slash
92
    if path.startswith(os.sep):
93
        path = path[len(os.sep):]
94
    if path == '' or path == '.':
95
        return (None, '')
96
    splitpath = path.split(os.sep, 1)
97
    if len(splitpath) == 1:
98
        return (splitpath[0], '')
99
    else:
100
        return tuple(splitpath)
101
1165.3.81 by Matt Giuca
ivle.util: Backported os.path.relpath from Python2.6 (required).
102
def relpath(path, start=os.path.curdir):
103
    """Return a relative version of a path.
104
    XXX Backported from Python 2.6 posixpath.py.
105
    """
106
107
    if not path:
108
        raise ValueError("no path specified")
109
110
    start_list = os.path.abspath(start).split(os.path.sep)
111
    path_list = os.path.abspath(path).split(os.path.sep)
112
113
    # Work out how much of the filepath is shared by start and path.
114
    i = len(os.path.commonprefix([start_list, path_list]))
115
116
    rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
117
    if not rel_list:
118
        return os.path.curdir
119
    return os.path.join(*rel_list)
120
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
121
def incomplete_utf8_sequence(byteseq):
1213 by William Grant
Drop ivle.util.make_path (replaced by Request.make_path) and fix docstrings.
122
    """Calculate the completeness of a UTF-8 encoded string.
123
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
124
    Given a UTF-8-encoded byte sequence (str), returns the number of bytes at
125
    the end of the string which comprise an incomplete UTF-8 character
126
    sequence.
127
128
    If the string is empty or ends with a complete character OR INVALID
129
    sequence, returns 0.
130
    Otherwise, returns 1-3 indicating the number of bytes in the final
131
    incomplete (but valid) character sequence.
132
133
    Does not check any bytes before the final sequence for correctness.
134
135
    >>> incomplete_utf8_sequence("")
136
    0
137
    >>> incomplete_utf8_sequence("xy")
138
    0
139
    >>> incomplete_utf8_sequence("xy\xc3\xbc")
140
    0
141
    >>> incomplete_utf8_sequence("\xc3")
142
    1
143
    >>> incomplete_utf8_sequence("\xbc\xc3")
144
    1
145
    >>> incomplete_utf8_sequence("xy\xbc\xc3")
146
    1
147
    >>> incomplete_utf8_sequence("xy\xe0\xa0")
148
    2
149
    >>> incomplete_utf8_sequence("xy\xf4")
150
    1
151
    >>> incomplete_utf8_sequence("xy\xf4\x8f")
152
    2
153
    >>> incomplete_utf8_sequence("xy\xf4\x8f\xa0")
154
    3
155
    """
156
    count = 0
157
    expect = None
158
    for b in byteseq[::-1]:
159
        b = ord(b)
160
        count += 1
161
        if b & 0x80 == 0x0:
162
            # 0xxxxxxx (single-byte character)
163
            expect = 1
164
            break
165
        elif b & 0xc0 == 0x80:
166
            # 10xxxxxx (subsequent byte)
167
            pass
168
        elif b & 0xe0 == 0xc0:
169
            # 110xxxxx (start of 2-byte sequence)
170
            expect = 2
171
            break
172
        elif b & 0xf0 == 0xe0:
173
            # 1110xxxx (start of 3-byte sequence)
174
            expect = 3
175
            break
176
        elif b & 0xf8 == 0xf0:
177
            # 11110xxx (start of 4-byte sequence)
178
            expect = 4
179
            break
180
        else:
181
            # Invalid byte
182
            return 0
183
184
        if count >= 4:
185
            # Seen too many "subsequent bytes", invalid
186
            return 0
187
188
    if expect is None:
189
        # We never saw a "first byte", invalid
190
        return 0
191
192
    # We now know expect and count
193
    if count >= expect:
194
        # Complete, or we saw an invalid sequence
195
        return 0
196
    elif count < expect:
197
        # Incomplete
198
        return count
1080.1.7 by matt.giuca
The new ivle.database.User class is now used in Request and usrmgt, which
199
1165.3.75 by Matt Giuca
ivle/util.py: Added safe_rmtree, copied from Python2.6 shutil.rmtree (which
200
def safe_rmtree(path, ignore_errors=False, onerror=None):
201
    """Recursively delete a directory tree.
202
203
    Copied from shutil.rmtree from Python 2.6, which does not follow symbolic
204
    links (it is otherwise unsafe to call as root on untrusted directories; do
205
    not use shutil.rmtree in this case, as you may be running Python 2.5).
206
207
    If ignore_errors is set, errors are ignored; otherwise, if onerror
208
    is set, it is called to handle the error with arguments (func,
209
    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
210
    path is the argument to that function that caused it to fail; and
211
    exc_info is a tuple returned by sys.exc_info().  If ignore_errors
212
    is false and onerror is None, an exception is raised.
213
214
    """
215
    if ignore_errors:
216
        def onerror(*args):
217
            pass
218
    elif onerror is None:
219
        def onerror(*args):
220
            raise
221
    try:
222
        if os.path.islink(path):
223
            # symlinks to directories are forbidden, see bug #1669
224
            raise OSError("Cannot call safe_rmtree on a symbolic link")
225
    except OSError:
226
        onerror(os.path.islink, path, sys.exc_info())
227
        # can't continue even if onerror hook returns
228
        return
229
    names = []
230
    try:
231
        names = os.listdir(path)
232
    except os.error, err:
233
        onerror(os.listdir, path, sys.exc_info())
234
    for name in names:
235
        fullname = os.path.join(path, name)
236
        try:
237
            mode = os.lstat(fullname).st_mode
238
        except os.error:
239
            mode = 0
240
        if stat.S_ISDIR(mode):
241
            safe_rmtree(fullname, ignore_errors, onerror)
242
        else:
243
            try:
244
                os.remove(fullname)
245
            except os.error, err:
246
                onerror(os.remove, fullname, sys.exc_info())
247
    try:
248
        os.rmdir(path)
249
    except os.error:
250
        onerror(os.rmdir, path, sys.exc_info())
1515 by Matt Giuca
Submit view: The projects list is now identical (except for radio buttons) to the view on the subjects page. It is much clearer and contains more info. The code is somewhat different, because it's a table, not a list, so I didn't abstract it. Moved a function out of subject.py to ivle.util, as it is shared by both views.
251
252
def format_submission_principal(user, principal):
253
    """Render a list of users to fit in the offering project listing.
254
255
    Given a user and a list of submitters, returns 'solo' if the
256
    only submitter is the user, or a string of the form
257
    'with A, B and C' if there are any other submitters.
258
259
    If submitters is None, we assume that the list of members could
260
    not be determined, so we just return 'group'.
261
    """
262
    if principal is None:
263
        return 'group'
264
265
    if principal is user:
266
        return 'solo'
267
268
    display_names = sorted(
269
        member.display_name for member in principal.members
270
        if member is not user)
271
272
    if len(display_names) == 0:
273
        return 'solo (%s)' % principal.name
274
    elif len(display_names) == 1:
275
        return 'with %s (%s)' % (display_names[0], principal.name)
276
    elif len(display_names) > 5:
277
        return 'with %d others (%s)' % (len(display_names), principal.name)
278
    else:
279
        return 'with %s and %s (%s)' % (', '.join(display_names[:-1]),
280
                                        display_names[-1], principal.name)