aboutsummaryrefslogtreecommitdiff
path: root/comp3311/2/rules
blob: 7f32a994e7e516f88ab4be590a04b4db0d4f8d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python3
# COMP3311 21T3 Ass2 ... print list of rules for a program or stream

import sys
import psycopg2
import re
from helpers import *

# define any local helper functions here

# Requires an array with with: name, min_req, max_req, type, definition, defby
def printAcademicRequirements(rules, db):
  print("Academic Requirements:")
  for (name, min_req, max_req, type, definition, defby) in rules:
    def_count = str.count(definition, ",")

    # There is some weirdly specific behaviour to emulate when printing. While
    # this might pass the tests, it is unlikely to continue doing so.
    # According to our lecturer we won't be tested on edge cases, so I can only
    # hope this is enough.
    if type == "DS":
      print(f"{min_req} stream(s) from {name}")
      printStreams(definition, db)

    elif type == "CC" or type == "PE":
      print(getCountString(def_count, min_req, max_req, name))
      printSubjects(definition, defby, db)

    elif type == "GE":
      print(f"{min_req} UOC of {name}")

    elif type == "FE":
      print(f"at least {min_req} UOC of Free Electives")

    else:
      print(f"UNEXPECTED {name}, {min_req}, {max_req}, {type}, {defby}")
  

### set up some globals

usage = f"Usage: {sys.argv[0]} (ProgramCode|StreamCode)"
db = None

### process command-line args

argc = len(sys.argv)
if argc < 2:
  print(usage)
  exit(1)
code = sys.argv[1]
if len(code) == 4:
  codeOf = "program"
elif len(code) == 6:
  codeOf = "stream"

try:
  db = psycopg2.connect("dbname=mymyunsw")
  if codeOf == "program":
    progInfo = getProgram(db,code)
    if not progInfo:
      print(f"Invalid program code {code}")
      exit()

    print(f"{progInfo[0]} {progInfo[1]}, {progInfo[2]} UOC, {progInfo[3] / 12.0} years")
    print(f"- offered by {progInfo[4]}");

    cur = db.cursor()
    query = """
      SELECT R.name, R.min_req, R.max_req, R.type, AOG.definition, AOG.defby
      FROM Program_Rules as PR, Rules as R, Academic_Object_Groups AS AOG
      WHERE PR.program = %s
        AND R.id = PR.rule
        AND AOG.id = R.ao_group
    """
    cur.execute(query, [code])
    printAcademicRequirements(cur.fetchall(), db)
    cur.close()


  elif codeOf == "stream":
    strmInfo = getStream(db,code)
    if not strmInfo:
      print(f"Invalid stream code {code}")
      exit()

    print(f"{code} {strmInfo[0]}");
    print(f"- offered by {strmInfo[1]}")

    cur = db.cursor()
    query = """
      SELECT R.name, R.min_req, R.max_req, R.type, AOG.definition, AOG.defby
      FROM Streams AS S, Stream_Rules AS SR, Rules AS R, Academic_Object_groups AS AOG
      WHERE S.code = %s
        AND SR.stream = S.id
        AND SR.rule = R.id
        AND AOG.id = R.ao_group
    """

    cur.execute(query, [code])
    printAcademicRequirements(cur.fetchall(), db)
    cur.close();

except Exception as err:
  print(err)
finally:
  if db:
    db.close()