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

« back to all changes in this revision

Viewing changes to ivle/util.py

  • Committer: Matt Giuca
  • Date: 2009-12-08 02:17:54 UTC
  • Revision ID: matt.giuca@gmail.com-20091208021754-xpjmt72v8ud93q9y
Added sample data infrastructure, with a little bit of sample data. The sample data is in examples/db/sample.sql. Documentation in doc/man/sample.rst details the infrastructure for importing and exporting sample data.

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
 
28
42
class IVLEJailError(Exception):
29
43
    """Exception proxying an in-jail error.
30
44
 
183
197
        # Incomplete
184
198
        return count
185
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
 
186
212
def safe_rmtree(path, ignore_errors=False, onerror=None):
187
213
    """Recursively delete a directory tree.
188
214
 
234
260
        os.rmdir(path)
235
261
    except os.error:
236
262
        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)