validation.py 1.07 KB
Newer Older
Alan Marchiori's avatar
initial  
Alan Marchiori committed
1
2
import click
import config
Alan Marchiori's avatar
Alan Marchiori committed
3
import courses
Alan Marchiori's avatar
initial  
Alan Marchiori committed
4
5
def course(c, param, value):
    "ensure a valid course defined in config.__init__.py"
Alan Marchiori's avatar
Alan Marchiori committed
6
    if value not in courses.courses:
Alan Marchiori's avatar
initial  
Alan Marchiori committed
7
        raise click.BadParameter("Course must be one of {}.".format(
Alan Marchiori's avatar
Alan Marchiori committed
8
            courses.courses
Alan Marchiori's avatar
initial  
Alan Marchiori committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
        ))
    return value
def section(c, param, value):
    "ensure section is in the range 60..99"
    errstr = "Section must be a two-digit integer, like 61."
    try:
        section = int(value)
        if section < 60 or section > 99:
            raise click.BadParameter(errstr)
    except ValueError:
        raise click.BadParameter(errstr)
    return value
def semester(c, param, value):
    "ensure F## or S## where ## >= 20"
    errstr = "Semester must be the letter F or S ([F]all or [S]pring) followed by the 2 digit year (20, 21...)"
    try:
        if not value[0] in ['F', 'S']:
            raise click.BadParameter(errstr)
        year = int(value[1:])
        if year < 20 or year > 50:
            raise click.BadParameter(errstr)
    except ValueError:
        raise click.BadParameter(errstr)
    return value