1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
22
# Contains common utility functions.
26
class IVLEError(Exception):
27
"""Legacy general IVLE exception.
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.
32
def __init__(self, httpcode, message=None):
33
self.httpcode = httpcode
34
self.message = message
35
self.args = (httpcode, message)
37
class IVLEJailError(Exception):
38
"""Exception proxying an in-jail error.
40
This exception indicates an error that occurred inside an IVLE CGI script
41
inside the jail. It should never be raised directly - only by the
44
Information will be retrieved from it, and then treated as a normal
47
def __init__(self, type_str, message, info):
48
self.type_str = type_str
49
self.message = message
52
class FakeObject(object):
53
""" A representation of an object that can't be Pickled """
54
def __init__(self, type, name, attrib={}):
60
return "<Fake %s %s>"%(self.type, self.name)
63
"""Given a path, returns a tuple consisting of the top-level directory in
64
the path, and the rest of the path. Note that both items in the tuple will
65
NOT begin with a slash, regardless of whether the original path did. Also
68
Always returns a pair of strings, except for one special case, in which
69
the path is completely empty (or just a single slash). In this case the
70
return value will be (None, ''). But still always returns a pair.
78
>>> split_path("home")
80
>>> split_path("home/docs/files")
81
('home', 'docs/files')
82
>>> split_path("//home/docs/files")
83
('', 'home/docs/files')
85
path = os.path.normpath(path)
86
# Ignore the opening slash
87
if path.startswith(os.sep):
88
path = path[len(os.sep):]
89
if path == '' or path == '.':
91
splitpath = path.split(os.sep, 1)
92
if len(splitpath) == 1:
93
return (splitpath[0], '')
95
return tuple(splitpath)
97
def incomplete_utf8_sequence(byteseq):
98
"""Calculate the completeness of a UTF-8 encoded string.
100
Given a UTF-8-encoded byte sequence (str), returns the number of bytes at
101
the end of the string which comprise an incomplete UTF-8 character
104
If the string is empty or ends with a complete character OR INVALID
106
Otherwise, returns 1-3 indicating the number of bytes in the final
107
incomplete (but valid) character sequence.
109
Does not check any bytes before the final sequence for correctness.
111
>>> incomplete_utf8_sequence("")
113
>>> incomplete_utf8_sequence("xy")
115
>>> incomplete_utf8_sequence("xy\xc3\xbc")
117
>>> incomplete_utf8_sequence("\xc3")
119
>>> incomplete_utf8_sequence("\xbc\xc3")
121
>>> incomplete_utf8_sequence("xy\xbc\xc3")
123
>>> incomplete_utf8_sequence("xy\xe0\xa0")
125
>>> incomplete_utf8_sequence("xy\xf4")
127
>>> incomplete_utf8_sequence("xy\xf4\x8f")
129
>>> incomplete_utf8_sequence("xy\xf4\x8f\xa0")
134
for b in byteseq[::-1]:
138
# 0xxxxxxx (single-byte character)
141
elif b & 0xc0 == 0x80:
142
# 10xxxxxx (subsequent byte)
144
elif b & 0xe0 == 0xc0:
145
# 110xxxxx (start of 2-byte sequence)
148
elif b & 0xf0 == 0xe0:
149
# 1110xxxx (start of 3-byte sequence)
152
elif b & 0xf8 == 0xf0:
153
# 11110xxx (start of 4-byte sequence)
161
# Seen too many "subsequent bytes", invalid
165
# We never saw a "first byte", invalid
168
# We now know expect and count
170
# Complete, or we saw an invalid sequence
176
def object_to_dict(attrnames, obj):
177
"""Convert an object into a dictionary.
179
This takes a shallow copy of the object.
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).
185
return dict((k, getattr(obj, k))
186
for k in attrnames if not k.startswith('_'))