gasresponse.py 3.25 KB
Newer Older
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
"""
    This file is part of GAS-site.
    Copyright (C) 2016 Saikiran Srirangapalli <saikiran1096@gmail.com>

    GAS-site is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    GAS-site is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GAS-site.  If not, see <http://www.gnu.org/licenses/>.
"""
18
from tempfile import NamedTemporaryFile
19
from subprocess import Popen, PIPE
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
20 21 22 23
import re
import gradaudit.util as util


24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class ExecutionError(Exception):
    """ Thrown when there is an error executing main.lp under s(ASP) """

    def __init__(self, msg, main, err):
        self.msg = msg
        self.main = main
        self.err = err
        self.strepr = '{}\n'\
                      'Main contents:\n{}\n'\
                      'Error:\n{}'.format(msg, main, err)

    def __str__(self):
        return self.strepr


39
class GASResponse(object):
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
40 41
    """ GAS advising information """
    _missing_class_re = r'hasNotTaken\({},([a-z]+[1-4][0-9V][0-9][0-9])\)'
42
    _missing_hours_re = r'needsHours\({},([^)]+),([0-9]+)\)'
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
43 44
    _missing_electives_re = r'needsElectiveHours\({},([0-9]+)\)'
    _grad = r'ableToGraduate\({}\)'
45

Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
46 47
    def __init__(self, transcript):
        self.transcript = transcript
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
48

Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
49
        main_contents = util.gen_main(transcript)
50 51
        answer_set = GASResponse._run(main_contents)
        advice = GASResponse._extract_advice(answer_set, transcript.name)
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
52 53 54 55 56 57 58 59 60

        self.missing_classes = advice[0]
        self.missing_hours = advice[1]
        self.missing_electives = advice[2]
        self.can_grad = advice[3]

    @classmethod
    def _run(cls, main_contents):
        """ Run the program under s(ASP) and return the answer set """
61 62 63
        with NamedTemporaryFile() as fil:
            fil.write(main_contents)
            fil.flush()
64 65 66 67 68 69 70 71 72 73 74

            # Command to run audit
            audit = ['sasp', fil.name]

            # Execute the audit, capture stdout, stderr
            run = Popen(audit, stdout=PIPE, stderr=PIPE)
            (answer_set, audit_err) = run.communicate()

            if run.returncode != 0:
                msg = 'Error executing audit'
                raise ExecutionError(msg, main_contents, audit_err)
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
75 76

        return answer_set
77

Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
78 79 80
    @classmethod
    def _extract_advice(cls, answer_set, name):
        """ Parses answer set for advising information """
81 82 83 84
        class_re = cls._missing_class_re.format(name)
        hours_re = cls._missing_hours_re.format(name)
        electives_re = cls._missing_electives_re.format(name)
        grad = cls._grad.format(name)
85

Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
86 87 88 89 90 91
        classes = re.findall(class_re, answer_set)
        hours = re.findall(hours_re, answer_set)

        electives = re.findall(electives_re, answer_set)
        if electives == []:
            electives = 0
92
        else:
Saikiran Srirangapalli's avatar
Saikiran Srirangapalli committed
93 94 95 96 97
            electives = int(electives[0])

        can_grad = grad in answer_set

        return (classes, hours, electives, can_grad)