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

820 by mattgiuca
lib: Added new package pulldown_subj, a collection of modules designed to
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# Module: Subject Pulldown
19
# Author: Matt Giuca
20
# Date:   7/7/2008
21
22
# Pluggable subject pulldown module.
23
# Grabs a student's subject list from a source specified in conf.py.
24
25
# Each module should have a get_subjects(login) function, which takes login
26
# and returns a list of (subject, semester) pairs (both strings), or None
27
# if the user can't be found. May also raise a SubjectError which is fatal.
28
29
import os
30
import sys
31
32
from subjecterror import SubjectError
33
import conf
34
35
def get_subjects(login):
36
    """
37
    Looks up the student in whatever modules are available, using login.
38
    If successful, returns a list of (subject, semester) pairs (both strings).
39
    Raises a SubjectError if unsuccessful.
40
    """
41
    for modname, m in subj_modules:
42
        result = m(login)
43
        if result is not None:
44
            return result
45
    return []
46
47
# Allow imports to get files from this directory.
48
# Get the directory that this module (authenticate) is in
49
plugpath = os.path.split(sys.modules[__name__].__file__)[0]
50
# Add it to sys.path
51
sys.path.append(plugpath)
52
53
# Create a global variable "subj_modules", a list of (name, function object)s.
54
# This list consists of null_subj, plus the "get_subject" functions of all the
55
# plugin subject pulldown modules.
56
57
subj_modules = []
58
for modname in conf.subject_pulldown_modules.split(','):
59
    try:
60
        mod = __import__(modname)
61
    except ImportError:
62
        raise SubjectError("Internal error: "
63
            "Can't import subject pulldown module %s"
64
            % repr(modname))
65
    except ValueError:
66
        # If auth_modules is "", we may get an empty string - ignore
67
        continue
68
    try:
69
        subjfunc = mod.get_subjects
70
    except AttributeError:
71
        raise SubjectError("Internal error: Subject pulldown module %r has no "
72
            "'get_subjects' function" % modname)
73
    subj_modules.append((modname, subjfunc))