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

« back to all changes in this revision

Viewing changes to bin/ivle-marks

  • Committer: Matt Giuca
  • Date: 2009-04-24 10:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 1196.
  • Revision ID: matt.giuca@gmail.com-20090424105425-l0odshs9xnk0u9og
Added optparse - proper options parsing.
    Takes a --semester, allowing the user to specify a semester to find a
    subject offering (defaults to the current offering).
    This value is currently ignored.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import os
28
28
import re
29
29
import csv
 
30
import optparse
30
31
from xml.dom import minidom
31
32
 
32
33
import ivle.database
37
38
    print >>sys.stderr, "Must run %s as root." % os.path.basename(sys.argv[0])
38
39
    sys.exit()
39
40
 
40
 
if len(sys.argv) <= 1:
41
 
    print >>sys.stderr, "Usage: %s subject" % os.path.basename(sys.argv[0])
42
 
    sys.exit()
 
41
usage = """usage: %prog [options] subject
 
42
(requires root)
 
43
Reports each student's marks for a given subject offering."""
 
44
 
 
45
# Parse arguments
 
46
parser = optparse.OptionParser(usage)
 
47
parser.add_option("-s", "--semester",
 
48
    action="store", dest="semester", metavar="YEAR/SEMESTER",
 
49
    help="Semester of the subject's offering (eg. 2009/1). "
 
50
    "Defaults to the currently active semester.",
 
51
    default=None)
 
52
(options, args) = parser.parse_args()
 
53
 
 
54
if len(args) < 1:
 
55
    parser.print_help()
 
56
    parser.exit()
43
57
 
44
58
# Regex for valid identifiers (subject/worksheet names)
45
59
re_ident = re.compile("[0-9A-Za-z_]+")
49
63
    m = re_ident.match(subject)
50
64
    return m is not None and m.end() == len(subject)
51
65
 
52
 
subject = sys.argv[1]
 
66
subject = args[0]
53
67
 
54
68
# Subject names must be valid identifiers
55
69
if not is_valid_subjname(subject):
56
70
    print >>sys.stderr, "Invalid subject name: %s." % repr(subject)
57
71
    sys.exit()
58
72
 
 
73
# Get the semester
 
74
if options.semester is None:
 
75
    year, semester = None, None
 
76
else:
 
77
    try:
 
78
        year, semester = options.semester.split('/')
 
79
        if len(year) == 0 or len(semester) == 0:
 
80
            raise ValueError()
 
81
    except ValueError:
 
82
        parser.error('Invalid semester (must have form "year/semester")')
 
83
 
59
84
def get_userdata(user):
60
85
    """
61
86
    Given a User object, returns a list of strings for the user data which