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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

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
 
    def __init__(self, httpcode, message=None):
35
 
        self.httpcode = httpcode
36
 
        self.message = message
37
 
        self.args = (httpcode, message)
38
 
 
39
28
class IVLEJailError(Exception):
40
29
    """Exception proxying an in-jail error.
41
30
 
194
183
        # Incomplete
195
184
        return count
196
185
 
197
 
def object_to_dict(attrnames, obj):
198
 
    """Convert an object into a dictionary.
199
 
 
200
 
    This takes a shallow copy of the object.
201
 
 
202
 
    @param attrnames: Set (or iterable) of names of attributes to be copied
203
 
                      into the dictionary. (We don't auto-lookup, because this
204
 
                      function needs to be used on magical objects).
205
 
    """
206
 
    return dict((k, getattr(obj, k))
207
 
        for k in attrnames if not k.startswith('_'))
208
 
 
209
186
def safe_rmtree(path, ignore_errors=False, onerror=None):
210
187
    """Recursively delete a directory tree.
211
188
 
257
234
        os.rmdir(path)
258
235
    except os.error:
259
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)