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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: mattgiuca
  • Date: 2008-01-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
25
 
import sys
26
 
import stat
27
 
 
28
 
class IVLEError(Exception):
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.
33
 
    """
34
 
 
35
 
    message = None
36
 
 
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):
43
 
    """Exception proxying an in-jail error.
44
 
 
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
 
 
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
 
 
121
 
def incomplete_utf8_sequence(byteseq):
122
 
    """Calculate the completeness of a UTF-8 encoded string.
123
 
 
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
199
 
 
200
 
def object_to_dict(attrnames, obj):
201
 
    """Convert an object into a dictionary.
202
 
 
203
 
    This takes a shallow copy of the object.
204
 
 
205
 
    @param attrnames: Set (or iterable) of names of attributes to be copied
206
 
                      into the dictionary. (We don't auto-lookup, because this
207
 
                      function needs to be used on magical objects).
208
 
    """
209
 
    return dict((k, getattr(obj, k))
210
 
        for k in attrnames if not k.startswith('_'))
211
 
 
212
 
def safe_rmtree(path, ignore_errors=False, onerror=None):
213
 
    """Recursively delete a directory tree.
214
 
 
215
 
    Copied from shutil.rmtree from Python 2.6, which does not follow symbolic
216
 
    links (it is otherwise unsafe to call as root on untrusted directories; do
217
 
    not use shutil.rmtree in this case, as you may be running Python 2.5).
218
 
 
219
 
    If ignore_errors is set, errors are ignored; otherwise, if onerror
220
 
    is set, it is called to handle the error with arguments (func,
221
 
    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
222
 
    path is the argument to that function that caused it to fail; and
223
 
    exc_info is a tuple returned by sys.exc_info().  If ignore_errors
224
 
    is false and onerror is None, an exception is raised.
225
 
 
226
 
    """
227
 
    if ignore_errors:
228
 
        def onerror(*args):
229
 
            pass
230
 
    elif onerror is None:
231
 
        def onerror(*args):
232
 
            raise
233
 
    try:
234
 
        if os.path.islink(path):
235
 
            # symlinks to directories are forbidden, see bug #1669
236
 
            raise OSError("Cannot call safe_rmtree on a symbolic link")
237
 
    except OSError:
238
 
        onerror(os.path.islink, path, sys.exc_info())
239
 
        # can't continue even if onerror hook returns
240
 
        return
241
 
    names = []
242
 
    try:
243
 
        names = os.listdir(path)
244
 
    except os.error, err:
245
 
        onerror(os.listdir, path, sys.exc_info())
246
 
    for name in names:
247
 
        fullname = os.path.join(path, name)
248
 
        try:
249
 
            mode = os.lstat(fullname).st_mode
250
 
        except os.error:
251
 
            mode = 0
252
 
        if stat.S_ISDIR(mode):
253
 
            safe_rmtree(fullname, ignore_errors, onerror)
254
 
        else:
255
 
            try:
256
 
                os.remove(fullname)
257
 
            except os.error, err:
258
 
                onerror(os.remove, fullname, sys.exc_info())
259
 
    try:
260
 
        os.rmdir(path)
261
 
    except os.error:
262
 
        onerror(os.rmdir, path, sys.exc_info())