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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: William Grant
  • Date: 2010-02-25 07:34:50 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225073450-zcl8ev5hlyhbszeu
Activate the Storm C extensions if possible. Moar speed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
197
197
        # Incomplete
198
198
        return count
199
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
200
def safe_rmtree(path, ignore_errors=False, onerror=None):
213
201
    """Recursively delete a directory tree.
214
202
 
260
248
        os.rmdir(path)
261
249
    except os.error:
262
250
        onerror(os.rmdir, path, sys.exc_info())
 
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)