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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: William Grant
  • Date: 2009-12-14 04:09:57 UTC
  • Revision ID: me@williamgrant.id.au-20091214040957-lpj8koijlkgs616n
So like Python, yet so unlike it, configobj's list syntax is Different®.

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
 
200
212
def safe_rmtree(path, ignore_errors=False, onerror=None):
201
213
    """Recursively delete a directory tree.
202
214
 
248
260
        os.rmdir(path)
249
261
    except os.error:
250
262
        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)