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

« back to all changes in this revision

Viewing changes to ivle/pulldown_subj/__init__.py

  • Committer: William Grant
  • Date: 2009-05-27 06:49:25 UTC
  • Revision ID: grantw@unimelb.edu.au-20090527064925-avjig8opo22t1lur
Drop ivle.conf usage from ivle.pulldown_subj.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import datetime
32
32
 
33
33
from subjecterror import SubjectError
34
 
import ivle.conf
35
34
 
36
35
from ivle.database import Subject, Offering, Semester
37
36
 
38
 
def get_subjects(login):
 
37
def get_subjects(config, login):
39
38
    """
40
39
    Looks up the student in whatever modules are available, using login.
41
40
    If successful, returns a list of (subject, semester) pairs (both strings).
42
41
    Raises a SubjectError if unsuccessful.
43
42
    """
44
 
    for modname, m in subj_modules:
 
43
    for modname, m in get_pulldown_modules(config):
45
44
        result = m(login)
46
45
        if result is not None:
47
46
            return result
48
47
    return []
49
48
 
50
 
def enrol_user(store, user, year=None):
 
49
def enrol_user(config, store, user, year=None):
51
50
    """
52
51
    Looks up the student in whatever modules are available.
53
52
    The pulldown does not tell us what year to enrol the student for, so the
67
66
        year = unicode(datetime.datetime.now().year)
68
67
 
69
68
    count = 0
70
 
    for subject, semester in get_subjects(user.login):
 
69
    for subject, semester in get_subjects(config, user.login):
71
70
        offering = store.find(Offering,
72
71
                              Subject.code == subject,
73
72
                              Offering.subject_id == Subject.id,
83
82
        count += 1
84
83
    return count
85
84
 
86
 
# Allow imports to get files from this directory.
87
 
# Get the directory that this module (authenticate) is in
88
 
plugpath = os.path.split(sys.modules[__name__].__file__)[0]
89
 
# Add it to sys.path
90
 
sys.path.append(plugpath)
91
 
 
92
 
# Create a global variable "subj_modules", a list of (name, function object)s.
93
 
# This list consists of null_subj, plus the "get_subject" functions of all the
94
 
# plugin subject pulldown modules.
95
 
 
96
 
subj_modules = []
97
 
for modname in ivle.conf.subject_pulldown_modules.split(','):
98
 
    try:
99
 
        mod = __import__(modname)
100
 
    except ImportError:
101
 
        raise SubjectError("Internal error: "
102
 
            "Can't import subject pulldown module %s"
103
 
            % repr(modname))
104
 
    except ValueError:
105
 
        # If auth_modules is "", we may get an empty string - ignore
106
 
        continue
107
 
    try:
108
 
        subjfunc = mod.get_subjects
109
 
    except AttributeError:
110
 
        raise SubjectError("Internal error: Subject pulldown module %r has no "
111
 
            "'get_subjects' function" % modname)
112
 
    subj_modules.append((modname, subjfunc))
 
85
def get_pulldown_modules(config):
 
86
    """Get the subject pulldown modules defined in the configuration.
 
87
 
 
88
    Returns a list of (name, function object)s. The list consists of
 
89
    the "get_subject" functions of all the plugin subject pulldown modules.
 
90
    """
 
91
 
 
92
    oldpath = sys.path
 
93
    # Allow imports to get files from this directory.
 
94
    # Get the directory that this module (authenticate) is in
 
95
    plugpath = os.path.split(sys.modules[__name__].__file__)[0]
 
96
    # Add it to sys.path
 
97
    sys.path.append(plugpath)
 
98
 
 
99
    subj_modules = []
 
100
    for modname in config['auth']['subject_pulldown_modules']:
 
101
        try:
 
102
            mod = __import__(modname)
 
103
        except ImportError:
 
104
            raise SubjectError("Internal error: "
 
105
                "Can't import subject pulldown module %s"
 
106
                % repr(modname))
 
107
        except ValueError:
 
108
            # If auth_modules is "", we may get an empty string - ignore
 
109
            continue
 
110
        try:
 
111
            subjfunc = mod.get_subjects
 
112
        except AttributeError:
 
113
            raise SubjectError("Internal error: Subject pulldown module %r has no "
 
114
                "'get_subjects' function" % modname)
 
115
        subj_modules.append((modname, subjfunc))
 
116
 
 
117
    # Restore the old path, without this directory in it.
 
118
    sys.path = oldpath
 
119
    return subj_modules