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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: William Grant
  • Date: 2010-07-28 04:13:05 UTC
  • mfrom: (1801.1.2 die-cjson-die)
  • Revision ID: grantw@unimelb.edu.au-20100728041305-xwypm3cn1l1mnki1
Port from cjson to (simple)json.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import sys
26
26
import stat
27
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
28
class IVLEJailError(Exception):
43
29
    """Exception proxying an in-jail error.
44
30
 
197
183
        # Incomplete
198
184
        return count
199
185
 
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
186
def safe_rmtree(path, ignore_errors=False, onerror=None):
213
187
    """Recursively delete a directory tree.
214
188
 
260
234
        os.rmdir(path)
261
235
    except os.error:
262
236
        onerror(os.rmdir, path, sys.exc_info())
 
237
 
 
238
def format_submission_principal(user, principal):
 
239
    """Render a list of users to fit in the offering project listing.
 
240
 
 
241
    Given a user and a list of submitters, returns 'solo' if the
 
242
    only submitter is the user, or a string of the form
 
243
    'with A, B and C' if there are any other submitters.
 
244
 
 
245
    If submitters is None, we assume that the list of members could
 
246
    not be determined, so we just return 'group'.
 
247
    """
 
248
    if principal is None:
 
249
        return 'group'
 
250
 
 
251
    if principal is user:
 
252
        return 'solo'
 
253
 
 
254
    display_names = sorted(
 
255
        member.display_name for member in principal.members
 
256
        if member is not user)
 
257
 
 
258
    if len(display_names) == 0:
 
259
        return 'solo (%s)' % principal.name
 
260
    elif len(display_names) == 1:
 
261
        return 'with %s (%s)' % (display_names[0], principal.name)
 
262
    elif len(display_names) > 5:
 
263
        return 'with %d others (%s)' % (len(display_names), principal.name)
 
264
    else:
 
265
        return 'with %s and %s (%s)' % (', '.join(display_names[:-1]),
 
266
                                        display_names[-1], principal.name)